Skip to main content
added 145 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 43 39 35 bytes

"<~"l4"<~"q4/{:L4Ue]256b85b'N4Ue]256b85b'!f+}/LN,)<"~>"

Try it online in the [CJam interpreter].

How it works

"<~"      e# Push that string.
l4q4/       e# Read aall lineinput from STDIN and split it into chunks of length 4.
{         e# For each chunk:
  :LN      e#   Save it in LN.
  4Ue]    e#   Right-pad it with 0's to a length of 4.
  256b85b e#   Convert from base 256 to base 85.
  '!f+    e#   Add '!' to each base-85 digit.
}/        e#
LN,)       e# Push the length of the last unpadded chunk, plus 1.
<         e# Keep that many chars of the last encoded chunk.
"~>"      e# Push that string.

If the input was empty, N,) will apply to the string "<~". Since N initially holds a single character, the output will be correct.

We don't have to deal with zz or pad the encoded chunks to length 5, since the input will contain only printable ASCII characters.

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b'!f%2B%7D%2FL%2Chttp://cjam.aditsu.net/#code=%22%3C~%22q4%2F%7B%3AN4Ue%5D256b85b'!f%2B%7D%2FN%2C)%3C%22~%3E%22&input=Programming%20Puzzles

CJam, 43 39 35 bytes

"<~"l4/{:L4Ue]256b85b'!f+}/L,)<"~>"

Try it online in the [CJam interpreter].

How it works

"<~"      e# Push that string.
l4/       e# Read a line from STDIN and split it into chunks of length 4.
{         e# For each chunk:
  :L      e#   Save it in L.
  4Ue]    e#   Right-pad it with 0's to a length of 4.
  256b85b e#   Convert from base 256 to base 85.
  '!f+    e#   Add '!' to each base-85 digit.
}/        e#
L,)       e# Push the length of the last unpadded chunk, plus 1.
<         e# Keep that many chars of the last encoded chunk.
"~>"      e# Push that string.

We don't have to deal with z or pad the encoded chunks to length 5, since the input will contain only printable ASCII characters.

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b'!f%2B%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

CJam, 43 39 35 bytes

"<~"q4/{:N4Ue]256b85b'!f+}/N,)<"~>"

Try it online in the [CJam interpreter].

How it works

"<~"      e# Push that string.
q4/       e# Read all input from STDIN and split it into chunks of length 4.
{         e# For each chunk:
  :N      e#   Save it in N.
  4Ue]    e#   Right-pad it with 0's to a length of 4.
  256b85b e#   Convert from base 256 to base 85.
  '!f+    e#   Add '!' to each base-85 digit.
}/        e#
N,)       e# Push the length of the last unpadded chunk, plus 1.
<         e# Keep that many chars of the last encoded chunk.
"~>"      e# Push that string.

If the input was empty, N,) will apply to the string "<~". Since N initially holds a single character, the output will be correct.

We don't have to deal with z or pad the encoded chunks to length 5, since the input will contain only printable ASCII characters.

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22q4%2F%7B%3AN4Ue%5D256b85b'!f%2B%7D%2FN%2C)%3C%22~%3E%22&input=Programming%20Puzzles

added 695 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 43 3939 35 bytes

"<~"l4/{:L4Ue]256b85b5Ue['L4Ue]256b85b'!f+}/L,)<"~>"

Try it online in the [CJam interpreter].

How it works

"<~"      e# Push that string.
l4/       e# Read a line from STDIN and split it into chunks of length 4.
{         e# For each chunk:
  :L      e#   Save it in L.
  4Ue]    e#   Right-pad it with 0's to a length of 4.
  256b85b e#   Convert from base 256 to base 85.
  '!f+    e#   Add '!' to each base-85 digit.
}/        e#
L,)       e# Push the length of the last unpadded chunk, plus 1.
<         e# Keep that many chars of the last encoded chunk.
"~>"      e# Push that string.

We don't have to deal with z or pad the encoded chunks to length 5, since the input will contain only printable ASCII characters.

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b5Ue%5B'!f%2B%7D%2FL%2Chttp://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b'!f%2B%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

CJam, 43 39 bytes

"<~"l4/{:L4Ue]256b85b5Ue['!f+}/L,)<"~>"

Try it online in the [CJam interpreter].

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b5Ue%5B'!f%2B%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

CJam, 43 39 35 bytes

"<~"l4/{:L4Ue]256b85b'!f+}/L,)<"~>"

Try it online in the [CJam interpreter].

How it works

"<~"      e# Push that string.
l4/       e# Read a line from STDIN and split it into chunks of length 4.
{         e# For each chunk:
  :L      e#   Save it in L.
  4Ue]    e#   Right-pad it with 0's to a length of 4.
  256b85b e#   Convert from base 256 to base 85.
  '!f+    e#   Add '!' to each base-85 digit.
}/        e#
L,)       e# Push the length of the last unpadded chunk, plus 1.
<         e# Keep that many chars of the last encoded chunk.
"~>"      e# Push that string.

We don't have to deal with z or pad the encoded chunks to length 5, since the input will contain only printable ASCII characters.

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b'!f%2B%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

edited body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 4343 39 bytes

"<~"q4"<~"l4/{:L4Ue]256b_85b5Ue['L4Ue]256b85b5Ue['!f+'z?f+}/L,)<"~>"

Try it online in the [CJam interpreter].

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22q4%2F%7B%3AL4Ue%5D256b_85b5Ue%5B'!f%2B'z%3F%7D%2FL%2Chttp://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b5Ue%5B'!f%2B%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

CJam, 43 bytes

"<~"q4/{:L4Ue]256b_85b5Ue['!f+'z?}/L,)<"~>"

Try it online in the [CJam interpreter].

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22q4%2F%7B%3AL4Ue%5D256b_85b5Ue%5B'!f%2B'z%3F%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

CJam, 43 39 bytes

"<~"l4/{:L4Ue]256b85b5Ue['!f+}/L,)<"~>"

Try it online in the [CJam interpreter].

[CJam interpreter]: http://cjam.aditsu.net/#code=%22%3C~%22l4%2F%7B%3AL4Ue%5D256b85b5Ue%5B'!f%2B%7D%2FL%2C)%3C%22~%3E%22&input=Programming%20Puzzles

Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading