aoc2015

Advent of Code 2015 solutions in Perl.
git clone git://git.samirparikh.com/aoc2015
Log | Files | Refs | README

day06-1.pl (908B) - 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 my @strings = split /\n/, $input;
     12 my %lights;
     13 foreach my $x ( 0 .. 999 ) {
     14     foreach my $y ( 0 .. 999 ) {
     15         $lights{ $x, $y } = -1; # set to off
     16     }
     17 }
     18 
     19 foreach ( @strings ) {
     20     my ( $instructions, $from, $to ) =
     21         m/(off|on|toggle)\s(\d+,\d+) through (\d+,\d+)/;
     22     my ( $from_x, $from_y ) = split /,/, $from;
     23     my ( $to_x, $to_y )     = split /,/, $to;
     24     for ( my $x = $from_x; $x < $to_x + 1; $x++ ) {
     25         for ( my $y = $from_y; $y < $to_y + 1; $y++ ) {
     26             $lights{ $x, $y }  = -1 if ( $instructions eq 'off' );
     27             $lights{ $x, $y }  = 1  if ( $instructions eq 'on' );
     28             $lights{ $x, $y } *= -1 if ( $instructions eq 'toggle' );
     29         }
     30     }
     31 }
     32 
     33 say "part 1: ", scalar grep { $_ > 0 } values %lights;