aoc2015

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

commit 477ebae20e16f9e83a0eb5bece67257c5e5aacf6
parent b7256a030153d7fed87f12a9fd861a1ec6dfeda9
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 10 Nov 2022 23:42:17 +0000

solve part 2 of day18

Diffstat:
Mday18/Day18.pm | 13+++++++++++--
Mday18/day18.pl | 19+++++++++++--------
2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/day18/Day18.pm b/day18/Day18.pm @@ -66,6 +66,7 @@ sub update_light sub flash_lights { my $current_state = shift; + my $part = shift; # need to declare a clean, new array, completely separate from $current_state # so as not to alter the current state when updating the new state my @next_state; @@ -73,7 +74,15 @@ sub flash_lights my $columns = scalar @{ $current_state->[ 0 ] }; foreach my $row ( 0 .. $rows - 2 ) { foreach my $col ( 0 .. $columns - 2 ) { - $next_state[ $row ][ $col ] = update_light( $row, $col, $current_state ); + if ( ( $part == 2 ) && + ( $row == 0 && $col == 0 || + $row == 0 && $col == $columns - 2 || + $row == $rows - 2 && $col == 0 || + $row == $rows - 2 && $col == $columns - 2 ) ) { + $next_state[ $row ][ $col ] = '#'; + } else { + $next_state[ $row ][ $col ] = update_light( $row, $col, $current_state ); + } } push @{ $next_state[ $row ] } => '.'; # add trailing column back in } @@ -82,7 +91,7 @@ sub flash_lights return \@next_state; } -sub display_lights # for debuggin purposes only +sub display_lights # for debugging purposes only { say "@{ $_ }" foreach ( @{ shift() } ); } diff --git a/day18/day18.pl b/day18/day18.pl @@ -8,18 +8,21 @@ use lib '.'; use Day18; use List::Util qw( max ); +$| = 1; + @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); my $STEPS = 100; -my $state = init_lights( $input ); - -say "Initial state:"; -#display_lights( $state ); +my $state_1 = init_lights( $input ); +my $state_2 = init_lights( $input ); foreach ( 1 .. $STEPS ) { - $state = flash_lights( $state ); + print "."; + $state_1 = flash_lights( $state_1, 1 ); + $state_2 = flash_lights( $state_2, 2 ); } - -say "part 1: ", count_lights( $state ); -say "part 2: "; +print "\n"; +#display_lights($state_2); +say "part 1: ", count_lights( $state_1 ); +say "part 2: ", count_lights( $state_2 );