I propose a golf for the shortest code to generate the 100 possible combinations for a Master Lock combination lock when the last number is known.[1]

When the last number is known, the first number has the same remainder as the last number when divided by 4. The second number is one of the possible first numbers plus 2 (wrapping to 0 at 40). The range of numbers is 0 to 39.

Example output if the last number is 19:

3-5-19 3-9-19 3-13-19 ... 39-33-19 39-37-19 39-1-19

My current attempt is posted below. Enjoy :)

(I suspect further restrictions can be made to the key space to reduce the number of possible combinations (ie first and last number can't be the same) but this initial iteration does not consider these.)

[1] With reference to:
http://www.dontforward.com/masterlock.htm
http://www.fusor.us/lockpick.html

My attempt, 81 characters ...
master_lock_combo(19); sub master_lock_combo { #0 1 2 3 4 5 #12345678901234567890123456789012345678901234567890 @x=grep{$_[0]%4==$_%4}0..39;for $i(@x){for (map{($_+2)%40}@x){print"$i-$_-$_[0]\n"}} }

Replies are listed 'Best First'.
Re: (Golf) Master lock combinations
by Transient (Hermit) on Jun 23, 2005 at 19:50 UTC
    I've been workin all day on this, lol, hope it suffices..(79 chars)
    sub master_lock_combo { # 1 2 3 4 5 #2345678901234567890123456789012345678901234567890 for$x(0..9){print join('-',$_[0]%4+$x*4, ($_[0]+2)%4+$_*4,$_[0])."\n" for(0..9)} }
Re: (Golf) Master lock combinations
by kaif (Friar) on Jun 23, 2005 at 22:40 UTC

    Just golfing your code further gets me 72:

    #0 1 2 3 4 5 6 +7 #123456789012345678901234567890123456789012345678901234567890123456789 +012 for$i(@x=grep$_[0]%4==$_%4,0..39){map{print"$i-",($_+2)%40,"-$_[0]\n"} +@x}

    Update: I think I have had a Masterlock with the first number equal to the last.

      Another minor refinement:

      #0 1 2 3 4 5 6 #123456789012345678901234567890123456789012345678901234567890123456789 for$i(@x=map$_*4+$_[0]%4,0..9){map{print"$i-",($_+2)%40,"-$_[0]\n"}@x}

      Hugo

        A further minor refinement, that exploits the "-" seperator:

        #0 1 2 3 4 5 6 #123456789012345678901234567890123456789012345678901234567890123456789 for$i(@x=map$_*4+$_[0]%4,0..9){map{print$i,2-$_%40,"-$_[0]\n"}@x}

        The other solutions posted that use map to construct the list, rather than grep an existing list, is a nice touch.

Re: (Golf) Master lock combinations
by thospel (Hermit) on Jul 01, 2005 at 21:50 UTC
    This looks like a working 58:
    //+map{$'-$_-2&3|"@_"-$'&3||print"$'-$_-@_ "}@;for@;=0..39
Re: (Golf) Master lock combinations
by chipmonkey (Initiate) on Jun 27, 2005 at 22:51 UTC
    Interesting to note these can be enumerated by concatenating the zero-padded 2-digit fields, i.e.
    $a=sprintf "%02d%02d%02d",$_[0]%4,(2+$_[0])%40%4,$_[0];
    then
    for(1..100){$a+=$_%10?400:36400;}
    In all my attempts so far though the list representation is more compact since it's verbose to set up and format the string. Thought it was cool though, will give it more thought.