use 5.20.0; my $grid = []; while(<>) { warn $_; / (?on|off|toggle) .*? (?\d+), (?\d+) .*? (?\d+), (?\d+) /x; for my $x ( $+{from_x}..$+{to_x} ) { for my $y ( $+{from_y}..$+{to_y} ) { $grid->[$x][$y] = $+{command} eq 'on' ? 1 :$+{command} eq 'off' ? 0 : !$grid->[$x][$y]; } } } use List::AllUtils qw/ sum /; say sum map { $_ ? @$_ : () } @$grid;