As a kid, one of my favorite summer games by the poolside was working out cryptosums. So in a bored moment recently I decided to write my own cryptosum generator. I was curious to know just how few characters one really needed to generate such puzzles, so I tried my hand at a little golf:
# 288 characters (excluding trailing \n) sub l{length$_[0]};sub p{$x=int rand($n=10**$d);$y=int rand($n);$z=$x+ +$y;@a=();for(0..9){while(defined $a[$n=rand(10)]){};$a[$n]=chr(65+$_) +;}$n=2+l$z;$"='';print map{eval"y/0-9/@a/";$_}(' 'x($n- l$x),"$x + ",' 'x($n-2- l$y),"$y ",'-'x$n," $z ");}$d=shift||4;$q=shift||1;p$d for(1..$q);
I'm not much of a golfer (either on the green or in code), but I find I learn a lot by watching others play the game. Would you help me lower the score on this? I'm not very good at decoding dense obfuscated code and my bit math is atrocious, so I would be grateful if you would also explain the tricks you used to get rid of characters.
For those interested in golfing, the above cryptosum generator has the following specification:
The program should run under 5.8.8 (I have an old machine) and have two parameters on the command line: max_digits_in_addends number_of_puzzles_to_generate. Thus perl Cryptosum.pm 5 4 would print out four puzzles each with no more than 5 digits in the addends. (the result may have more digits). And would look like this:
BGBHD + ACGIJ -------- EHJECB JAECD + BFIEF -------- CAGCGF GJBEJ + HGJIF ------- IBFCA BJAGH + DCBFB -------- BFACGI
For each sum printed:
#Sum looks like this: ABC + DDDD ------- AEABB
Best, beth
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Golfing cryptosums (218=>176=>167)
by BrowserUk (Patriarch) on Aug 09, 2009 at 13:23 UTC | |
by ELISHEVA (Prior) on Aug 09, 2009 at 13:56 UTC | |
by grizzley (Chaplain) on Aug 10, 2009 at 11:15 UTC | |
|
Re: Golfing cryptosums
by ysth (Canon) on Aug 09, 2009 at 19:27 UTC | |
by Anonymous Monk on Aug 10, 2009 at 04:00 UTC | |
by ELISHEVA (Prior) on Aug 10, 2009 at 18:43 UTC | |
by Anonymous Monk on Aug 11, 2009 at 08:32 UTC |