day06-2.pl (940B) - raw
1 #!/usr/local/bin/perl 2 # day 2015-06 3 4 use strict; 5 use warnings; 6 use v5.32; 7 8 @ARGV = "input" unless @ARGV; 9 chomp( my $input = do { local $/; <> } ); 10 11 use List::Util qw( sum ); 12 13 my @strings = split /\n/, $input; 14 my %lights; 15 foreach my $x ( 0 .. 999 ) { 16 foreach my $y ( 0 .. 999 ) { 17 $lights{ $x, $y } = 0; 18 } 19 } 20 21 foreach ( @strings ) { 22 my ( $instructions, $from, $to ) = 23 m/(off|on|toggle)\s(\d+,\d+) through (\d+,\d+)/; 24 my ( $from_x, $from_y ) = split /,/, $from; 25 my ( $to_x, $to_y ) = split /,/, $to; 26 for ( my $x = $from_x; $x < $to_x + 1; $x++ ) { 27 for ( my $y = $from_y; $y < $to_y + 1; $y++ ) { 28 $lights{ $x, $y } -= 1 if ( $instructions eq 'off' 29 && $lights{ $x, $y } > 0 ); 30 $lights{ $x, $y } += 1 if ( $instructions eq 'on' ); 31 $lights{ $x, $y } += 2 if ( $instructions eq 'toggle' ); 32 } 33 } 34 } 35 36 say "part 2: ", sum values %lights;