#!/usr/local/bin/perl # day 2015-06 use strict; use warnings; use v5.32; @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); my @strings = split /\n/, $input; my %lights; foreach my $x ( 0 .. 999 ) { foreach my $y ( 0 .. 999 ) { $lights{ $x, $y } = -1; # set to off } } foreach ( @strings ) { my ( $instructions, $from, $to ) = m/(off|on|toggle)\s(\d+,\d+) through (\d+,\d+)/; my ( $from_x, $from_y ) = split /,/, $from; my ( $to_x, $to_y ) = split /,/, $to; for ( my $x = $from_x; $x < $to_x + 1; $x++ ) { for ( my $y = $from_y; $y < $to_y + 1; $y++ ) { $lights{ $x, $y } = -1 if ( $instructions eq 'off' ); $lights{ $x, $y } = 1 if ( $instructions eq 'on' ); $lights{ $x, $y } *= -1 if ( $instructions eq 'toggle' ); } } } say "part 1: ", scalar grep { $_ > 0 } values %lights;