aoc2015

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

commit b7256a030153d7fed87f12a9fd861a1ec6dfeda9
parent 9627aa19c21ef74417fb11b8e83daa084c18710f
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 10 Nov 2022 22:39:31 +0000

solve part 1 of day18

Diffstat:
Mday18/Day18.pm | 32++++++++++++++++++++++++--------
Mday18/day18.pl | 9++++-----
2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/day18/Day18.pm b/day18/Day18.pm @@ -5,7 +5,7 @@ use warnings; use v5.32; use Exporter qw( import ); -our @EXPORT = qw( init_lights flash_lights display_lights ); +our @EXPORT = qw( init_lights flash_lights display_lights count_lights ); sub setup_neighbors { @@ -49,13 +49,13 @@ sub update_light foreach my $neighbor ( @neighbors ) { my $neighbor_row = $row + $neighbor->[0]; my $neighbor_col = $col + $neighbor->[1]; - say "------------"; - say "row $row + neighbor_row $neighbor->[0] = $neighbor_row"; - say "col $col + neighbor_col $neighbor->[1] = $neighbor_col"; - say "light is <$lights[ $neighbor_row ][ $neighbor_col ]>"; +# say "------------"; +# say "row $row + neighbor_row $neighbor->[0] = $neighbor_row"; +# say "col $col + neighbor_col $neighbor->[1] = $neighbor_col"; +# say "light is <$lights[ $neighbor_row ][ $neighbor_col ]>"; $neighbors_on++ if ( $lights[ $neighbor_row ][ $neighbor_col ] eq '#' ); } - say "light $row, $col has $neighbors_on neighbors that are on"; +# say "light $row, $col has $neighbors_on neighbors that are on"; if ( $lights[ $row ][ $col ] eq '#' ) { return ( $neighbors_on == 2 || $neighbors_on == 3 ) ? '#' : '.'; } else { @@ -66,21 +66,37 @@ sub update_light sub flash_lights { my $current_state = shift; - my @next_state = @{ $current_state }; + # 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; my $rows = scalar @{ $current_state }; 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 ); } + push @{ $next_state[ $row ] } => '.'; # add trailing column back in } + push @next_state => [ ('.') x $columns ]; # add trailing row back in return \@next_state; } -sub display_lights +sub display_lights # for debuggin purposes only { say "@{ $_ }" foreach ( @{ shift() } ); } +sub count_lights +{ + my $lights = shift; + my $count = 0; + foreach my $row ( @{ $lights } ) { + foreach my $col ( @{ $row } ) { + $count++ if ( $col eq '#' ); + } + } + return $count; +} + 1; diff --git a/day18/day18.pl b/day18/day18.pl @@ -10,17 +10,16 @@ use List::Util qw( max ); @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); -my $STEPS = 1; +my $STEPS = 100; my $state = init_lights( $input ); say "Initial state:"; -display_lights( $state ); +#display_lights( $state ); foreach ( 1 .. $STEPS ) { $state = flash_lights( $state ); - say "After step $_:"; - display_lights( $state ); } -say "part 1: "; + +say "part 1: ", count_lights( $state ); say "part 2: ";