Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick
brad

Lost Combo

My mom's been losing keys and passwords left and right lately. The last one she forgot was the combination for our key box outside. It's a 4 digit (0-9) code, where order doesn't matter---- 4 keys press in, then you pull down a switch to test it. My mom is offering Cole $10 if he can figure out the code, thinking it next to impossible. Remembering my statistics, I realized it wasn't really that hard at all: there are only 210 possibilities. Nine lines of Perl later, I emailed Cole a list of possible combos and he's out there now in a lawn chair trying them all. I wanted $5 for my effort, but he's a stingy bastard.

foreach $a (0..9) {
  foreach $b (grep { $_ != $a } (0..9)) {
    foreach $c (grep { $_ != $a && $_ != $b } (0..9)) {
      foreach $d (grep { $_ != $a && $_ != $b && $_ != $c } (0..9)) {
        my $sorted = join(", ", sort { $a <=> $b } ($a,  $b, $c, $d));
        unless ($used{$sorted}++) {
        print "$sorted\n";
} } } } }
Well, that's 8 if you don't count the #!/usr/bin/perl.

Update! My brother found it! He just came and gave me a dollar. Woohoo! Time to quit my day job! (wait ... I don't have a day job...)

Tags: perl, tech
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 14 comments