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


In reply to Golfing cryptosums by ELISHEVA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.