2008
06.07

Tee Off

So I was listening to Car Talk on the way to work today (on a Saturday, bleh) after my run (more on that later) and I made special note of the puzzler. Usually the puzzler can be very tricky, but not this week, for me at least. They haven’t posted it yet, but it goes something along these lines:

Tommy went to a farmers market one day. He accidentally kicked over a big basket of eggs and broke every single one of them. Feeling bad, he asked the woman how many there were so he could compensate her appropriately. [author's note: this part gets a little fuzzy to remember] She says that she didn’t count them but had put them in paper bags earlier. When she put 2 in each bag, she had 1 left over (not placed in a bag). So she decided to put 3 in each bag, but still had 1 left over. Then she tried 4, 5, and eventally 6, but they all had 1 remaining egg, but 7 eggs per bag worked without any extra eggs. What is the minimum number of eggs Tommy has to pay for?

So obviously I wrote a perl script in my head while driving to calculate it. I figured I’d test it out when I got to work, but I had 30 minutes until I arrived. I decided to mentally golf my script to come up with the least amount of characters needed to provide the answer. I golfed out 2 more answers and came up with the following:

$_++ until $_%2==1 && $_%3==1 && $_%4==1 && $_%5==1 && $_%6==1 && $_%7==0;
print "Minimum number of eggs: $_\n";

It’s pathetically 2 lines of code, but 1 is a user friendly line to print the answer. I was starting to wonder if I could golf any more of that code down, but after reading some actual perl golf scripts, I came to the conclusion that I clearly a) do not know perl well enough and b) am not clever enough. So I think I am going to embark on trying to golf this down as much as possible. If you remove the unneccessary spaces and print statement, it’s exactly 63 characters.

If anyone has any suggestions, please drop a comment. I would really like to learn to play better golf and develop a much better understanding of perl. Also, if anyone wants to golf this out in another language, feel free to drop a comment (use the code tag).

Update: Refer to the on-going game at perlgolf.

No Comment.

Add Your Comment