perlmeditation
eyepopslikeamosquito
<P>
<blockquote>
<P>
<I>
I can get down to 300 bytes without using compression schemes or code packing
in to an eval (perl). I think the 176 was using a string compression of the code.
</I>
</P>
<P align="right">
<small>
-- <a href="http://codegolf.com/boards/conversation/view/112">"SplinterFL" comments</a> on the <a href="http://codegolf.com/99-bottles-of-beer">codegolf 99 bottles of beer game</a>
</small>
</P>
<P>
<I>
What I can't fathom is how someone could possibly
solve this problem in 134 characters in PHP, the way eyepopslikeamosquito did.
That is insane my friend.
Probably ran a Perl solution through PHP's shell command functions.
</I>
</P>
<P align="right">
<small>
-- <a href="http://blachan.com/blog/code-golf-saving-time/">Abdulla Arif comments</a> on the <a href="http://codegolf.com/saving-time">codegolf savingtime game</a>
</small>
</P>
<P>
<I>
Don't assume. It makes an "ass" out of "u" and "me".
</I>
</P>
<P align="right">
<small>
-- David Brent in <a href="http://www.imdb.com/title/tt0290978/quotes">The Office</a>
</small>
</P>
</blockquote>
</P>
<P>
Though I can't comment on the 176 char bottles of beer solution that "SplinterFL"
was referring to,
please note that my 168 char solution uses just a humble sub
and a lowly print statement inside a simple for loop.
No need for any "string compression of the code".
</P>
<P>
As for my 134 character PHP saving time solution, I'm happy to reveal that
it does not "run a Perl solution through PHP's shell command functions".
It's just plain old PHP code.
No dirty tricks.
Just the usual blood, toil, tears and sweat.
</P>
<P>
Why on Earth do beginning golfers indulge in such unfounded speculations?
I don't know but a possible answer can be found by
remembering one of [id://720219|Beth]'s many insightful responses
where she [id://775947|perceptively noted] that:
<blockquote>
<I>
In the late 1990's Justin Kruger and David Dunning did a series
of studies demonstrating that the less skillful had a tendency
to overrate their abilities and fail to recognize expertise in others.
</I>
</blockquote>
</P>
<P>
Anyway, after reading Abdulla's 234 char PHP solution
to the codegolf
<a href="http://codegolf.com/saving-time">Saving Time</a>
challenge, namely:
<CODE>
<?fscanf(STDIN,'%d:%d',$h,$m);$s=array_combine(str_split('0b1a29384756'),array_fill(0,12,'o'));$s[$h=dechex($h>11?$h-12:$h)]='h';$s[$m=dechex($m>59?0:$m/5)]=$h==$m?'x':'m';vprintf("%9s
%5s%8s
%s%14s
%s%16s
%s%14s
%5s%8s
%9s",$s)
?>
</CODE>
I refined his interesting approach
into a (shortened) 132 stroke Perl solution as follows:
<CODE>
<>=~/:/;@y=(o)x12;$y[$`%12]=h;$y[$'/5]=~y/oh/mx/;
printf'%9s
%5s%8s
%s%14s
%s%16s
%s%14s
%5s%8s
%9s',map{$y[$_],$y[11-$_]}0..5
</CODE>
</P>
<readmore>
<P>
To clarify precisely what problem this code is solving, and to make it easier to test
possible solutions, I've written a test program as follows:
<CODE>
# test.pl
use strict;
use warnings;
my $testprog = shift or die "usage: $0 program-file\n";
my $datafile = 'tt.txt';
my $cmd = "perl $testprog <$datafile";
sub build_file {
my $contents = shift;
open(my $fh, '>', $datafile) or die "open '$datafile': $!";
print $fh "$contents\n";
close($fh);
}
print "Testing $testprog, size=", -s $testprog, " bytes.\n";
my @testdata = (
[ '00:00', <<'EXPECTED' ],
x
o o
o o
o o
o o
o o
o
EXPECTED
[ '23:59', <<'EXPECTED' ],
o
x o
o o
o o
o o
o o
o
EXPECTED
[ '21:35', <<'EXPECTED' ],
o
o o
o o
h o
o o
m o
o
EXPECTED
[ '04:59', <<'EXPECTED' ],
o
m o
o o
o o
o h
o o
o
EXPECTED
[ '12:00', <<'EXPECTED' ],
x
o o
o o
o o
o o
o o
o
EXPECTED
[ '03:03', <<'EXPECTED' ],
m
o o
o o
o h
o o
o o
o
EXPECTED
[ '21:08', <<'EXPECTED' ],
o
o m
o o
h o
o o
o o
o
EXPECTED
[ '23:05', <<'EXPECTED' ],
o
h m
o o
o o
o o
o o
o
EXPECTED
[ '16:24', <<'EXPECTED' ],
o
o o
o o
o o
o x
o o
o
EXPECTED
[ '13:06', <<'EXPECTED' ],
o
o x
o o
o o
o o
o o
o
EXPECTED
[ '18:00', <<'EXPECTED' ],
m
o o
o o
o o
o o
o o
h
EXPECTED
[ '22:26', <<'EXPECTED' ],
o
o o
h o
o o
o o
o m
o
EXPECTED
[ '20:10', <<'EXPECTED' ],
o
o o
o m
o o
h o
o o
o
EXPECTED
);
for my $r (@testdata) {
print $r->[0], "\n";
build_file($r->[0]);
my $got = `$cmd`;
chomp($got);chomp($got);chomp($got);$got.="\n";
$got eq $r->[1] or die "expected:\n$r->[1]got:\n$got";
}
print "successful\n";
</CODE>
</P>
<P>
You can test possible solutions by running:
<CODE>
perl test.pl mysolution.pl
</CODE>
</P>
<P>
Note that the 132 stroke Perl solution given above is just a random example solution
to give you a working solution to this problem.
Better approaches are available.
</P>
<P>
Please feel free to golf this code.
Or speculate on the general approach I took to get down to 101 strokes.
If you do happen to speculate correctly, I'd be happy to congratulate you
and confirm a correct speculation.
I'm writing this node for fun and to see if the above two wildly-off-the-mark speculations
are really representative ... or if most golfers can speculate rather
better than that. :-)
</P>
<P><B>References</B></P>
<P>
Update: After this node was written, I described most
of the interesting solutions I'm aware of for both
the Saving Time and 99 Bottles of Beer golf games at:
<ul>
<li> [id://811919] (Saving Time: Perl)
<li> [id://814900] (Saving Time: Python, Ruby, PHP)
<li> [id://962854] (More Saving Time: Perl)
<li> [id://903641] (99 Bottles of Beer: Perl, Python, Ruby)
</ul>
</P>
</readmore>