aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol2.pl (683B) [raw]


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 my @adapters;
      6 while (<ARGV>) {
      7     chomp;
      8     push(@adapters, $_);
      9 }
     10 
     11 # reverse!
     12 @adapters = sort { $b <=> $a } (@adapters, 0);
     13 
     14 # Consider 1 2 3 4 5 6
     15 # set n(9) = 1
     16 # 6 -> n(9)        # 1
     17 # 5 -> n(6)        # 1
     18 # 4 -> n(5) + n(6) # 2
     19 # 3 -> n(4) + n(5) + n(6) # 4
     20 # 2 -> n(3) + n(4) + n(5) # 7
     21 # 1 -> n(2) + n(3) + n(4) # 13
     22 
     23 my %H;
     24 $H{$adapters[0] + 3} = 1; # device
     25 while (@adapters) {
     26     my $n = shift(@adapters);
     27     # how many things can you get to?
     28     my $total;
     29     for (my $i = $n + 1; $i <= $n + 3; $i++) {
     30         if (exists($H{$i})) {
     31             $total += $H{$i};
     32         }
     33     }
     34     $H{$n} = $total;
     35 }
     36 print "$H{0}\n";