aoc

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

sol2.pl (653B) [raw]


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 
      6 # Read it all in
      7 open (my $fh, '<', 'input') or die "$!";
      8 my $i = 0;
      9 my @M = ();
     10 for my $l (<$fh>) {
     11     chomp $l;
     12     $M[$i] = [split('', $l)];
     13     $i++;
     14 }
     15 
     16 # Do work on the matrix
     17 my @slopes = (
     18     [1,1],
     19     [3,1],
     20     [5,1],
     21     [7,1],
     22     [1,2],
     23 );
     24 
     25 my @N;
     26 for (my $k = 0; $k < scalar @slopes; $k++) {
     27     my ($r, $d) = @{$slopes[$k]};
     28     my $j = 0;
     29     for (my $i = 0; $i < scalar @M; $i += $d) {
     30         if ($M[$i]->[$j] eq '#') {
     31             $N[$k]++;
     32         }
     33         $j = ($j + $r) % scalar @{$M[0]};
     34     }
     35 }
     36 
     37 print join(",", @N) . "\n";
     38 my $total = 1;
     39 $total *= $_ for @N;
     40 print "$total\n";