6
\$\begingroup\$

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.

\$\endgroup\$
8
  • 5
    \$\begingroup\$ (wondering if anyone else sees a need for a 'no-encodings' tag?) \$\endgroup\$
    – smci
    Jan 21 '12 at 9:16
  • \$\begingroup\$ @Peter: it must use ASCII, and is restricted to characters 32-127. Sound ok? \$\endgroup\$
    – smci
    Jan 21 '12 at 9:41
  • 2
    \$\begingroup\$ Seems both reasonable and needed. Tagged. \$\endgroup\$
    – J B
    Jan 21 '12 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
    Jan 21 '12 at 12:36
  • 1
    \$\begingroup\$ Do note that ASCII 127 is not printable. \$\endgroup\$
    – J B
    Jan 21 '12 at 12:45
4
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ {}{stuff}if can be reduced to !{stuff}*. With 33<{}{stuff}if you can save one character more by changing the test to 32>{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\$ Jan 21 '12 at 15:37
  • \$\begingroup\$ @PeterTaylor: Awesome, thanks :) \$\endgroup\$
    – hammar
    Jan 21 '12 at 15:46
  • \$\begingroup\$ And 55\-\.@ can be shortened to 56-~1$. > is an "order" operator, so the array and index can be in either order. However, simply changing \.@ to 1$ causes problems because it's preceded by - and gets tokenised as 55 \ -1 $. \$\endgroup\$ Jan 21 '12 at 15:51
  • 1
    \$\begingroup\$ Nice, but how much extra does it cost to make it case-sensitive? \$\endgroup\$
    – smci
    Jan 21 '12 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
    Jan 21 '12 at 22:05
2
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ 2 chars shorter if you add encoding 9 for ' fa' \$\endgroup\$
    – smci
    Jan 21 '12 at 12:53
  • \$\begingroup\$ @smci:Thanks, but it's giving the same count. \$\endgroup\$
    – Toto
    Jan 21 '12 at 13:04
  • \$\begingroup\$ True, my mistake. \$\endgroup\$
    – smci
    Jan 21 '12 at 13:09
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\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
    Jan 23 '12 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
    Jan 23 '12 at 12:14
  • \$\begingroup\$ I posted my answer @smci \$\endgroup\$
    – Blazer
    Jan 24 '12 at 9:00
1
\$\begingroup\$

PHP, 203

Base64 is ASCII only, but I'd still consider this slightly exploitive.

<?=gzinflate(base64_decode('TY5BDsMwCATvecV+hcQkRsWmMras/P8jpW5V9bhidodCN3pmNKOEJs4YT3RDYe64bWzlC0ypCTuDdNLtoHVt2Ol4/BgfFZ6lBk2txJLVD3XSwdvSkFSPqAq3s/8jwpp8o7CM2kUx+fMEXdFZissSsml6FyB1KSOrTdiJLI4c9Rc'));

Proof it works.

\$\endgroup\$
2
  • \$\begingroup\$ Case-sensitive? \$\endgroup\$
    – smci
    Jan 24 '12 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
    Jan 26 '12 at 20:45
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.