10
\$\begingroup\$

Using one programming language write 5 single-line programs, each of which outputs a different line of the first verse to Harry Nilsson's 1972 song "Coconut" when run individually:

Brother bought a coconut, he bought it for a dime
His sister had another one, she paid it for a lime
She put the lime in the coconut, she drank them both up
She put the lime in the coconut, she drank them both up
She put the lime in the coconut, she drank them both up

(full lyrics)

The last 3 lines have the same lyrics, so your last 3 programs might be as well.

There are 5 factorial or 120 ways these 5 single-line programs can be arranged one per line in a single 5-line program. You must optimize your single-line programs such that for as many of these 120 combinations as possible the 5-line program outputs the entire verse in its proper order, exactly as it appears above.

Example

The simplest answer would have 5 single line print statements, the last 3 identical:

print('Brother bought a coconut, he bought it for a dime')
print('His sister had another one, she paid it for a lime')
print('She put the lime in the coconut, she drank them both up')
print('She put the lime in the coconut, she drank them both up')
print('She put the lime in the coconut, she drank them both up')

The output to the 5-line programs will be the verse in its proper order as long as the first two single-liners are in their correct places. Only 6 of the 120 possible arrangements accomplish this.

Scoring

The submission with the most working cases out of all 120 wins. Tie breaker goes to the set of programs with the lowest cumulative byte count (newlines not counted). The example has 309 bytes.

Details

  • Your programs may not read their own source code. Nor should they read other external files or require a network connection.

  • Output to stdout or closest alternative. You may also output to a file with the name of your choice.

  • "Single-line programs" are strings that contain any characters except line terminators (you'll probably just need to worry about newlines) .

  • When composing the single-liners into the 5-liners you should add exactly 4 newlines, one between each adjacent pair of single-liners. You may optionally add a trailing newline.

\$\endgroup\$
  • \$\begingroup\$ "Write 5 programs which each output a different line..." isn't that impossible? \$\endgroup\$ – feersum Jan 24 '15 at 0:44
  • \$\begingroup\$ @feersum The lines are different. Three of them just happen to have the same text. \$\endgroup\$ – Calvin's Hobbies Jan 24 '15 at 0:54
  • \$\begingroup\$ What if I have a Java file containing 5 classes, each with a main method? Then which should be run, when running the program? \$\endgroup\$ – feersum Jan 24 '15 at 1:13
  • \$\begingroup\$ @feersum Well what does your compiler do? I'm not sure what Java does when multiple classes in the same file both have main. \$\endgroup\$ – Calvin's Hobbies Jan 24 '15 at 1:36
  • 2
    \$\begingroup\$ The way I remember this one is that one Coca-Cola ad where they "put the lime in the Coke"... \$\endgroup\$ – Joe Z. Feb 6 '15 at 19:57
9
\$\begingroup\$

CJam, 120 valid permutations, 334 299 290 bytes

"She put the lime in the coconut, she drank them both up"N+
"Brother bought a coconut, he bought it for a dime"N+]$~
"She put the lime in the coconut, she drank them both up"N+
"His sister had another one, she paid it for a lime"N+]$~
"She put the lime in the coconut, she drank them both up"N+

Pretty simple logic:

  • Put each line of the song in each of the 5 programs
  • Wrap everything on stack in an array
  • Sort the array
  • Unwrap the array so that the next program can use the same logic
  • In each step, we have the partial lyrics in an array. Luckily, sorting the array gives the correct order of the lyrics.

UPDATE: Turns out that you don't need the wrap-sort-unwrap cycle after the 3 similar and appearing at the end lines of the song. I can explain why if its not that apparent :)

\$\endgroup\$
8
\$\begingroup\$

Prelude, 120 permutations, 2045 2035 bytes

9-9-9-9-9-9-9-9-9-5-97+993++09-9-9-9-9-9-9-1-8992++96+209-9-9-9-9-9-9-1-94+58992++09-9-9-9-9-9-9-1-92+95+199+409-9-9-9-9-9-9-1-58991++09-9-9-9-9-9-9-1-09-9-9-9-9-7-992++993++95+96+396+309-9-9-9-9-9-9-1-58992++09-9-9-9-9-9-9-1-95+909-9-9-9-9-9-9-1-594+993+09-9-9-9-9-9-9-1-58992++09-9-9-9-9-9-9-1-992++993++97+09-9-9-9-9-9-9-1-5809-4-                                                                          (9+9+9+9+9+9+9+9+9+9+6+!)
9-9-9-9-9-9-9-9-9-5-594+9409-9-9-9-9-9-9-1-109-9-9-9-9-9-9-1-99+96+609-9-9-9-9-9-9-1-992++909-9-9-9-9-9-9-1-992++87993++96+209-9-9-9-9-9-9-1-5809-9-9-9-9-9-9-1-09-9-9-9-9-7-992++993++95+96+396+309-9-9-9-9-9-9-1-109-9-9-9-9-9-9-1-992++87993++96+209-9-9-9-9-9-9-1-99+58992++96+99+09-9-9-3-                                             (9+9+9+9+9+9+9+9+9+9+6+!)
9-9-9-9-9-9-9-9-9-5-97+993++09-9-9-9-9-9-9-1-8992++96+209-9-9-9-9-9-9-1-94+58992++09-9-9-9-9-9-9-1-92+95+199+409-9-9-9-9-9-9-1-58991++09-9-9-9-9-9-9-1-09-9-9-9-9-7-992++993++95+96+396+309-9-9-9-9-9-9-1-58992++09-9-9-9-9-9-9-1-95+909-9-9-9-9-9-9-1-594+993+09-9-9-9-9-9-9-1-58992++09-9-9-9-9-9-9-1-992++993++97+09-9-9-9-9-9-9-1-5809-4-                                                 (9+9+9+9+9+9+9+9+9+9+6+!)
9-9-9-9-9-9-9-9-9-5-594+993+09-9-9-9-9-9-9-1-109-9-9-9-9-9-9-1-99+96+609-9-9-9-9-9-9-1-992++909-9-9-9-9-9-9-1-49197+09-9-9-9-9-9-9-1-58991++09-9-9-9-9-9-9-1-09-9-9-9-9-7-595+96+09-9-9-9-9-9-9-1-99+58992++96+95+109-9-9-9-9-9-9-1-41809-9-9-9-9-9-9-1-99+5992++991++9991++09-9-9-9-9-9-9-1-991++909-9-6-                                                           (9+9+9+9+9+9+9+9+9+9+6+!)
9-9-9-9-9-9-9-9-9-5-97+993++09-9-9-9-9-9-9-1-8992++96+209-9-9-9-9-9-9-1-94+58992++09-9-9-9-9-9-9-1-92+95+199+409-9-9-9-9-9-9-1-58991++09-9-9-9-9-9-9-1-09-9-9-9-9-7-992++993++95+96+396+309-9-9-9-9-9-9-1-58992++09-9-9-9-9-9-9-1-95+909-9-9-9-9-9-9-1-594+993+09-9-9-9-9-9-9-1-58992++09-9-9-9-9-9-9-1-992++993++97+09-9-9-9-9-9-9-1-5809-4-                                                                                                   (9+9+9+9+9+9+9+9+9+9+6+!)

This has ultimately convinced me that Prelude and CJam need a child. If it wasn't for Prelude's very limited instruction set, this submission would actually be very competitive, if it wouldn't even beat CJam.

The basic idea is the following

"push line 4"               print
"push line 1"print
"push line 3"          print
"push line 2"     print
"push line 5"                    print

In Prelude, each line is its own "voice" with its own stack, and these voices are executed in parallel. That is, in the full program, each line will just individually construct the string and at print it at the end. But due to the offsets of the print statements, the order of the lines doesn't matter at all for the order of the output, since execution is left-to-right, not top-to-bottom.

Now, why are the lines so long? Prelude only stores numbers on its stack. Furthermore, it can only push a single-digit number at a time, so larger numbers have to be constructed from addition and subtraction (there is no multiplication either or other arithmetic either). So the largest part of code is simply calculating and pushing the right character codes. To shorten this a bit, I offset them by 96. At the end, to print, I loop until the stack is empty (i.e. a 0 is encountered), add 96 to each number, and print it.

Note that the strings are pushed in reverse, since they're printed from last to first character.

This assumes the Python interpreter with NUMERIC_OUTPUT = False (which is how the spec defines I/O).

I used the following CJam code to generate the string construction:

"Brother bought a coconut, he bought it for a dime
"W%{i96-_0<{zLa*9/:,'-*'-+0\+}{La*9/:,_,'+*W<}?}%

Lastly, just for fun, why I think this would be really short if Prelude had a concept of strings:

"pu htob meht knard ehs ,tunococ eht ni emil eht tup ehS"N+!
"emid a rof ti thguob eh ,tunococ a thguob rehtorB"N+!
"pu htob meht knard ehs ,tunococ eht ni emil eht tup ehS"N+!
"emil a rof ti diap ehs ,eno rehtona dah retsis siH"N+!
"pu htob meht knard ehs ,tunococ eht ni emil eht tup ehS"N+!

Now the print statements ! are already in the right order, so I wouldn't even need to offset them. I think I'll get down to implementing this some time.

\$\endgroup\$
7
\$\begingroup\$

Ruby, 120 valid permutations, 430 bytes

$*[1]='His sister had another one, she paid it for a lime';a||=at_exit{puts($*-[p])}
$*[0]='Brother bought a coconut, he bought it for a dime';a||=at_exit{puts($*)}
$*[2]='She put the lime in the coconut, she drank them both up';a||=at_exit{puts($*-[p])}
$*[3]='She put the lime in the coconut, she drank them both up';a||=at_exit{puts($*-[p])}
$*[4]='She put the lime in the coconut, she drank them both up';a||=at_exit{puts($*-[p])}

Works in any order, or individually. Each line modifies global state, then sets a hook to be executed at the end of the program unless the hook has already been set.

\$\endgroup\$
  • \$\begingroup\$ The question says " 5 single-line programs". Does not this mean each line should be in a separate program? Not all in one? \$\endgroup\$ – bacchusbeale Jan 24 '15 at 10:30
  • 1
    \$\begingroup\$ Just a question of formatting. Each line works as its own program. \$\endgroup\$ – histocrat Jan 24 '15 at 13:36
6
\$\begingroup\$

><>, 120 permutations, 703 bytes

<    /a"emid a rof ti thguob eh ,tunococ a thguob rehtorB"0a.#.!50padpacpabpaapa9pa8pa7p97p87p77p67p57p95p84p73p62p51"/////\\\\\>l0=?;o"
<   / a"emil a rof ti diap ehs ,eno rehtona dah retsis siH"09.#.!50padpacpabpaapa9pa8pa7p97p87p77p67p57p95p84p73p62p51"/////\\\\\>l0=?;o"
<  /  a"pu htob meht knard ehs ,tunococ eht ni emil eht tup ehS"08.#.!50padpacpabpaapa9pa8pa7p97p87p77p67p57p95p84p73p62p51"/////\\\\\>l0=?;o"
< /   a"pu htob meht knard ehs ,tunococ eht ni emil eht tup ehS"07.#.!50padpacpabpaapa9pa8pa7p97p87p77p67p57p95p84p73p62p51"/////\\\\\>l0=?;o"
</    a"pu htob meht knard ehs ,tunococ eht ni emil eht tup ehS"06.#.!50padpacpabpaapa9pa8pa7p97p87p77p67p57p95p84p73p62p51"/////\\\\\>l0=?;o"

The one-liner restriction was pretty tough for a 2D language, so I had to find a way to make the most of the . teleport instruction.

The initial < makes program flow leftward, wrapping around and executing

"o;?=0l>\\\\\\/////"15p26p37p48p59p75p76p77p78p79p7ap8ap9apaapbapcapdap

This is a series of p (put) instructions which places chars until the board looks like this:

<    /<lyrics1>0a.#.!50<blah>
<   / <lyrics2>09.#.!50<blah>
<  /  <lyrics3>08.#.!50<blah>
< /   <lyrics4>07.#.!50<blah>
</    <lyrics5>06.#.!50<blah>
 /     \
  /    \
   /   \
    /  \
     / \
       >l0=?;o

The #.!50 then flips program flow back to rightward again, before jumping to line 5. What follows is this:

  • Following the / mirror on line 5 finds lyrics 5, which is pushed onto the stack. We then teleport to line 6.
  • Following the / mirror on line 6 finds lyrics 4, which is pushed onto the stack. We then teleport to line 7.
  • Following the / mirror on line 7 finds lyrics 3...

This happens until we push lyrics 1, at which point we teleport to line 10. This is a loop to print the whole stack until it is empty.

If only one line of the program is present, then the \ mirrors do the same job as the teleports on each lyric line.

If the program is scrambled, then the explanation above still applies, so the program works for all permutations!

\$\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.