aoc2015

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

commit 5f83de19e59cae7a3e622289f30ad8e8b4ec2137
parent edbebfe40dc62daf364ca6596ee43310d49e372c
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Wed, 19 Oct 2022 21:29:00 +0000

solve part 2 of day06

Diffstat:
Aday06/day06-2.pl | 37+++++++++++++++++++++++++++++++++++++
Dday06/day06.pl | 36------------------------------------
2 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/day06/day06-2.pl b/day06/day06-2.pl @@ -0,0 +1,37 @@ +#!/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 $/; <> } ); + +use List::Util qw( sum ); + +my @strings = split /\n/, $input; +my %lights; +foreach my $x ( 0 .. 999 ) { + foreach my $y ( 0 .. 999 ) { + $lights{ $x, $y } = 0; + } +} + +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 } > 0 ); + $lights{ $x, $y } += 1 if ( $instructions eq 'on' ); + $lights{ $x, $y } += 2 if ( $instructions eq 'toggle' ); + } + } +} + +say "part 2: ", sum values %lights; diff --git a/day06/day06.pl b/day06/day06.pl @@ -1,36 +0,0 @@ -#!/usr/local/bin/perl -# day 2015-06 - -use strict; -use warnings; -use v5.32; -use lib '.'; -#use Day05 qw( is_nice_1 is_nice_2 ); - -@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 - } -} -#say $_ foreach ( @strings ); -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; -say "part 2: ";