Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

When One Golfer Speculates On What Another Is Doing...

by eyepopslikeamosquito (Canon)
on Aug 01, 2009 at 03:07 UTC ( #785052=perlmeditation: print w/ replies, xml ) Need Help??

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.

-- "SplinterFL" comments on the codegolf 99 bottles of beer game

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.

-- Abdulla Arif comments on the codegolf savingtime game

Don't assume. It makes an "ass" out of "u" and "me".

-- David Brent in The Office

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".

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.

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 Beth's many insightful responses where she perceptively noted that:

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.

Anyway, after reading Abdulla's 234 char PHP solution to the codegolf Saving Time challenge, namely:

<?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=dech +ex($m>59?0:$m/5)]=$h==$m?'x':'m';vprintf("%9s %5s%8s %s%14s %s%16s %s%14s %5s%8s %9s",$s) ?>
I refined his interesting approach into a (shortened) 132 stroke Perl solution as follows:
<>=~/:/;@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

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:

# 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";

You can test possible solutions by running:

perl test.pl mysolution.pl

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.

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. :-)

References

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:

Comment on When One Golfer Speculates On What Another Is Doing...
Select or Download Code
Re: When One Golfer Speculates On What Another Is Doing...
by Anonymous Monk on Aug 01, 2009 at 03:45 UTC
Re: When One Golfer Speculates On What Another Is Doing...
by dreadpiratepeter (Priest) on Aug 01, 2009 at 12:33 UTC
    Your attribution for the assume quote is off. He is quoting the original.
    Never Assume. It makes an "ass" out of "u" and "me".
    - Felix Unger, "The Odd Couple"


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://785052]
Approved by planetscape
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-08-28 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls