How much can you golf a program to print "An Old Irish Blessing" without using unprintable ASCII characters?
That restricts us to ASCII characters 32-126 (better still, 32,65-90,97-122 and 13(CR))
For reference that version of the proverb is 205 characters long (excl. newlines). I'm curious if anything beats Huffman-encoding incl. spaces.
-
5\$\begingroup\$ (wondering if anyone else sees a need for a 'no-encodings' tag?) \$\endgroup\$smci– smci2012年01月21日 09:16:11 +00:00Commented Jan 21, 2012 at 9:16
-
\$\begingroup\$ @Peter: it must use ASCII, and is restricted to characters 32-127. Sound ok? \$\endgroup\$smci– smci2012年01月21日 09:41:06 +00:00Commented Jan 21, 2012 at 9:41
-
2\$\begingroup\$ Seems both reasonable and needed. Tagged. \$\endgroup\$J B– J B2012年01月21日 10:40:49 +00:00Commented Jan 21, 2012 at 10:40
-
\$\begingroup\$ I guess someone will compress the bitstring into the ASCII range 32..127 then unpack it, but that's frowned upon. Can anyone get below 100% length otherwise? \$\endgroup\$smci– smci2012年01月21日 12:36:29 +00:00Commented Jan 21, 2012 at 12:36
-
1\$\begingroup\$ Do note that ASCII 127 is not printable. \$\endgroup\$J B– J B2012年01月21日 12:45:10 +00:00Commented Jan 21, 2012 at 12:45
6 Answers 6
Golfscript, 198 characters
[]"-!9 4(% 2/!$<)3A50H/ -%%4 9/5
XXXN7).X\"?!,7I3>4UU2I!#+
[[[S35.;(`A[2- 50/.]]]&]%
YO2!U3F|,`/&4ZZZZZK)%,3ドル
!.$ }4),ze-%%Z!'!k\n-!9 '/S(/]hh )o4(%GG,/7 /&A)3;!v"{.32>{.58<{32+}{56-~1$>2<}if}*+}/+
Output:
$ ruby golfscript.rb blessing.gs
MAY THE ROAD RISE UP TO MEET YOU
MAY THE WIND BE ALWAYS AT YOUR BACK
MAY THE SUN SHINE WARM UPON YOUR FACE
THE RAINS FALL SOFT UPON YOUR FIELDS
AND UNTIL WE MEET AGAIN
MAY GOD HOLD YOU IN THE HOLLOW OF HIS HAND
The poem is compressed to 158 characters by using part of the ASCII range for back-references (of fixed length 2). The coding scheme is as follows:
[10] : newline (unencoded)
[32] : space (unencoded)
[33..57] : literal (33=A, 34=B, ... 57=Y, Z not needed)
[58..126] : back-reference relative to the end of the decompressed string so far.
The remaining 40 characters make up the decompression code which can probably be golfed a little further, since this is my first attempt at Golfscript.
Case-sensitive version, 207 characters
Same concept, but trading some of the back-reference range for lower case letters at the cost of some compression.
[]"-AY THE ROAD RISaUPhO MEET YOU
xxxnWINxB_ALWiS^TuuRiACK
{{{sSUN[HINa{RM UPON}}}F}E
4HoRAuS fLL SOFTzzzzzkIELDS
!NDlNTIwWE MEEzAGAIN
-AY 'OsHOLxYOU k THEggLOW OF (ISeAND"{.32>{.90<{32+}{88-~1$>2<}if}*+}/+
Output:
$ ruby golfscript.rb blessing-casesensitive.gs
May the road rise up to meet you
May the wind be always at your back
May the sun shine warm upon your face
The rains fall soft upon your fields
And until we meet again
May God hold you in the hollow of His hand
-
\$\begingroup\$
{}{stuff}ifcan be reduced to!{stuff}*. With33<{}{stuff}ifyou can save one character more by changing the test to32>{stuff}*. Also, you can save that penultimate''because you'll have an empty string on the stack underneath the array, from the empty stdin. \$\endgroup\$Peter Taylor– Peter Taylor2012年01月21日 15:37:02 +00:00Commented Jan 21, 2012 at 15:37 -
\$\begingroup\$ And
55\-\.@can be shortened to56-~1$.>is an "order" operator, so the array and index can be in either order. However, simply changing\.@to1$causes problems because it's preceded by-and gets tokenised as55 \ -1 $. \$\endgroup\$Peter Taylor– Peter Taylor2012年01月21日 15:51:34 +00:00Commented Jan 21, 2012 at 15:51 -
1\$\begingroup\$ Nice, but how much extra does it cost to make it case-sensitive? \$\endgroup\$smci– smci2012年01月21日 21:30:25 +00:00Commented Jan 21, 2012 at 21:30
-
\$\begingroup\$ @smci: Using some of the back-reference range for lower case letters means I can't reach as far back, so the compressed length becomes 167 for a total of 207 characters, which isn't as bad as I thought, but still just barely too long. \$\endgroup\$hammar– hammar2012年01月21日 22:05:43 +00:00Commented Jan 21, 2012 at 22:05
Perl, 214 characters
s//01road rise2 to 34
01w5d be always at4r back
01s7 sh5e warm284r face
The ra5s fall soft284r fields
And 7til we 3 aga5
0God 6d4 5 1 6low of His hand/;s/\d/('May ','the ',' up',meet,' you',in,hol,un,on)[$&]/eg;say
-
\$\begingroup\$ 2 chars shorter if you add encoding 9 for ' fa' \$\endgroup\$smci– smci2012年01月21日 12:53:22 +00:00Commented Jan 21, 2012 at 12:53
-
\$\begingroup\$ @smci:Thanks, but it's giving the same count. \$\endgroup\$Toto– Toto2012年01月21日 13:04:33 +00:00Commented Jan 21, 2012 at 13:04
-
\$\begingroup\$ True, my mistake. \$\endgroup\$smci– smci2012年01月21日 13:09:53 +00:00Commented Jan 21, 2012 at 13:09
Python, 263 chars
t="""01road rise2 to34
01w5d be always at4r back
01s7 sh5e warm284r9ce
The ra5s9ll soft284r fields
And 7til we3 aga5
0God 6d4 5 16low of His hand"""
for k,v in enumerate("May ,the , up, meet, you,in,hol,un,on, fa".split(',')):
t = t.replace(str(k),v)
print t
which was an 'improvement' on my 261-char quick hack, obviously inferior to a simple print.
s="""12road rise up to34
12wind be always at4r back
12sun shine warm54r face
The rains fall soft54r fields
And until we3 again
1God hold4 in 2hollow of His hand"""
for k,v in {1:"May ",2:"the ",3:" meet",4:" you",5:" upon"}.items():
s = s.replace(str(k),v)
print s
enumerate() takes almost as many chars in creating the dict/sequence, on a short example like this.
-
\$\begingroup\$ I think this is poor in that it takes more characters to generate the blessing than printing the blessing by itself (which is 221 chars) \$\endgroup\$Blazer– Blazer2012年01月23日 06:03:05 +00:00Commented Jan 23, 2012 at 6:03
-
\$\begingroup\$ @Blazer, I remarked exactly that ('obviously inferior to a simple print'). I also remarked that bitpacking would get it smaller (before overhead), since the message has only 28 symbols, thus 4.8 bits of entropy per character. Can you do better (in Python)? \$\endgroup\$smci– smci2012年01月23日 12:14:57 +00:00Commented Jan 23, 2012 at 12:14
-
\$\begingroup\$ I posted my answer @smci \$\endgroup\$Blazer– Blazer2012年01月24日 09:00:51 +00:00Commented Jan 24, 2012 at 9:00
PHP, 203
Base64 is ASCII only, but I'd still consider this slightly exploitive.
<?=gzinflate(base64_decode('TY5BDsMwCATvecV+hcQkRsWmMras/P8jpW5V9bhidodCN3pmNKOEJs4YT3RDYe64bWzlC0ypCTuDdNLtoHVt2Ol4/BgfFZ6lBk2txJLVD3XSwdvSkFSPqAq3s/8jwpp8o7CM2kUx+fMEXdFZissSsml6FyB1KSOrTdiJLI4c9Rc'));
-
\$\begingroup\$ Case-sensitive? \$\endgroup\$smci– smci2012年01月24日 01:39:41 +00:00Commented Jan 24, 2012 at 1:39
-
\$\begingroup\$ That's 171 chars for the string + 32 overhead. I guess this string is too small and non-redundant to break even. \$\endgroup\$smci– smci2012年01月26日 20:45:24 +00:00Commented Jan 26, 2012 at 20:45
Python, 224 chars
print'''1road rise up to meet you
1wind be always at your back
1sun shine warm upon your face
The rains fall soft upon your fields
And until we meet again
May God hold you in the hollow of His hand'''.replace('1','May the ')
Instead of wasting characters on for loops or multiple replaces, I only have one replace with the single most common combinations of words May the
Io, 217 bytes
Compressing the string leads to a longer length...
"May the road rise up to meet you
May the wind be always at your back
May the sun shine warm upon your face
The rains fall soft upon your fields
And until we meet again
May God hold you in the hollow of His hand"print
Explore related questions
See similar questions with these tags.