Perl 239 + 2 = 241 bytes
This is a mostly binary solution, so as usual, this is a program to generate it:
generator.pl
use MIME::Base64;
$data = <<EOD;
JF89bGMgcGFjayB1NjAsJ8MqbtCBITBInQmKJcpWt4c2oWhLBPwjAkyBIiUwjBI3Ql9c9GoWtJZbuTd5
ZbuWhPwjAkyBIyQjDBFXQlLpGtKJWhP2t4c2r7mhFQjAqdM2t4eTjjk2aJcpWtKJWrb77ms6Kbpc2qKZ
6E1rmtKJWo7JYcmorNZWhM6+U1LpGobLGt4c2osqZ6NGobpGp4eTk0wiTBIXQlLpGr7mtKIdGhPwjBIT
nQkyBISUwjBFXQtKJWhPxAjBLHR7KdNLJWh5r8ms9MpYbTIFQyc7eS8zLTovCkFGSUpUVyAvO2V2YWw=
EOD
print decode_base64($data);
Accepts input from STDIN, space separated, two extra bytes for the -na
option. Sample usage:
$ perl generator.pl > out.pl
$ stat -c %s out.pl
239
$ echo bank house | perl -na out.pl
There was a stream
(There was a stream)
Just a teeny-weeny stream
(Just a teeny-weeny stream)
And the stream was on its way...
Where the moon shines high
In the clear blue sky
And all was bright and gay.
And on that stream
(And on that stream)
There was a little bank
(There was a little bank)
Just a teeny-weeny bank
(Just a teeny-weeny bank)
And the bank was on the stream
And the stream was on its way...
Where the moon shines high
In the clear blue sky
And all was bright and gay.
And on that bank
(And on that bank)
There was a little house
(There was a little house)
Just a teeny-weeny house
(Just a teeny-weeny house)
And the house was on the bank
And the bank was on the stream
And the stream was on its way...
Where the moon shines high
In the clear blue sky
And all was bright and gay.
Method
Consider the following UU-Encoded string:
\PRINT($A,$B="8HERE:WAS:A:$L$_","3($B)3",$C="7UST:A:TEENY-WEENY:$_","3($C)",,$5="
\4ND:THE:$_:WAS:ON:$5","ITS:WAY...39HERE:THE:MOON:SHINES:HIGH36N:THE:CLEAR:BLUE:$
\SKY34ND:ALL:WAS:BRIGHT:AND:GAY.33"),$A="4ND:ON:THAT:$_",$A.="3($A)3",$5="THE:$_$
5",$L='LITTLE:'FOR:STREAM,@5#
After applying a lc,y/3-:/\nAFIJTW /
it becomes this:
\print($a,$b="There was a $l$_","
($b)
",$c="Just a teeny-weeny $_","
($c)",,$F="
\And the $_ was on $F","its way...
Where the moon shines high
In the clear blue $
\sky
And all was bright and gay.
"),$a="And on that $_",$a.="
($a)
",$F="the $_$
F",$l='little 'for stream,@F#
Which is basically the 269 byte solution below.
There's a few problems with this method, though. UU-Encoded strings are limited to characters 33-96, which excludes space, newline, and { | } ~
, as well as all lower-case letters. The solution I chose, is to apply lc
after the pack u
, and then translate 8 characters: space, newline, and the 6 upper-cased I needed. This leaves an 18 byte footprint, which is fairly excessive, but it produces the desired result.
The other problem is that each line needs to begin with the same character (here I chose \
corresponding to a decoded length of 60 (92 - 32), which is an encoded length of 80), except for the last line, which needs to begin with the character reflecting its length. Here I ended up with 21 (padding with a #
to be evenly divisible by 3), corresponding to 5
. No problem, I'm replacing 5 anyway, so I chose it as one of my variables. Perl seems to be incredibly lenient about where you can place backslashes. This program even begins with one.
It is possible to instruct perl to use a other characters to begin each line, such as M
(the default value), but none seem quite as flexible as a backslash. As an aside, this method should be equally as effective in Ruby. If I had any skill in the language, I might be tempted to try it.
For reference, my previous solutions remain below.
Perl 267 + 2 = 269 bytes
(print$a,$b="There was a $l$_","
($b)
",$c="Just a teeny-weeny $_","
($c)",$d="
And the $_ was on $d",'its way...
Where the moon shines high
In the clear blue sky
And all was bright and gay.
'),$a="And on that $_",$a.="
($a)
",$d="the $_$d",$l='little 'for stream,@F
Accepts input from STDIN, space separated, two extra bytes for the -na
option. Sample usage:
$ echo bank house | perl -na stream-song.pl
PHP 286 bytes
No real attempt at compression, just output reusage.
<?for($w=stream;$w;$a="And on that $w",$a.="
($a)
",$l='little ',$d="the $w$d",$w=next($argv))echo$a,$b="There was a $l$w","
($b)
",$c="Just a teeny-weeny $w","
($c)",$d="
And the $w was on $d",'its way...
Where the moon shines high
In the clear blue sky
And all was bright and gay.
';
Accepts input as command line arguments. Sample usage:
$ php stream-song.php bank house