commit edbebfe40dc62daf364ca6596ee43310d49e372c
parent a5e9e320339d7d58f34d99e30a986fe1b3fca8f1
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 19 Oct 2022 21:21:27 +0000
cleanup code
Diffstat:
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/day06/day06-1.pl b/day06/day06-1.pl
@@ -0,0 +1,34 @@
+#!/usr/local/bin/perl
+# day 2015-06
+
+use strict;
+use warnings;
+use v5.32;
+use lib '.';
+
+@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;