aoc2021

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

commit 353e09d4a93cd8a84a3a363f058290f6a4a1dc0c
parent 44c1283941f94fc997dc5625192ae7a89456106f
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon, 27 Dec 2021 01:12:00 +0000

update day11-1.pl to provide working solution for Day 11 Part 1

Diffstat:
Mday11-1.pl | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 76 insertions(+), 1 deletion(-)

diff --git a/day11-1.pl b/day11-1.pl @@ -3,10 +3,11 @@ use strict; use warnings; use v5.22; -use Math::BigInt; # Day 11, Part 1 +use constant FLASH_LIMIT => 9; + sub get_filehandle { if (@ARGV !=1) { die "Usage: $0 [input-filename]"; @@ -17,6 +18,17 @@ sub get_filehandle { return $filehandle; } +sub print_octopuses { + my ($oct_ref, $rows, $columns) = @_; + my @octopuses = @{$oct_ref}; + foreach my $row (0 .. $rows - 2) { + foreach my $column (0 .. $columns - 2) { + print $octopuses[$row][$column]; + } + print "\n"; + } + print "\n"; +} my $filehandle = get_filehandle(); # add extra column of negative infinity @@ -25,3 +37,66 @@ my $columns = @{$octopuses[0]}; # add extra row of negative infinity push @octopuses => [ ("-inf") x $columns ]; my $rows = @octopuses; +my @neighbors; +foreach my $nr (-1 .. 1) { + foreach my $nc (-1 .. 1) { + push @neighbors => [$nr, $nc] unless ($nr == 0 && $nc == 0); + } +} +my $flash_count = 0; + +print_octopuses(\@octopuses, $rows, $columns); +#say $rows, " x ", $columns; +#say "@$_" foreach @neighbors; +foreach my $step (1 .. 100) { + #say "after step $step"; + my @queue = (); + my %flash_state = (); +# 10 iterate through each octopus and increment its counter by 1 + foreach my $row (0 .. $rows - 2) { + foreach my $column (0 .. $columns - 2) { + $octopuses[$row][$column]++; +# 20 iterate through each octopus and see whether its energy level +# is greater than 9 + if ($octopuses[$row][$column] > FLASH_LIMIT && + !exists $flash_state{$row, $column}) { +# 30 for any octupus whose energy level is greater than 9 AND which +# has not yet flashed in this step: +# - update its state in a hash indicating that it has flashed +# - increment the flash counter +# - add its position to a queue which we will iterate through to +# increase the energy level of adjacent octupuses + $flash_state{$row, $column} = 1; + $flash_count++; + push @queue => [ $row, $column ]; + } + } + } + while (@queue) { + my $octopus = shift @queue; + my $r = $octopus->[0]; + my $c = $octopus->[1]; + foreach my $neighbor (@neighbors) { + my $neighbor_row = $r + $neighbor->[0]; + my $neighbor_col = $c + $neighbor->[1]; + $octopuses[$neighbor_row][$neighbor_col]++; + if ($octopuses[$neighbor_row][$neighbor_col] > FLASH_LIMIT && + !exists $flash_state{$neighbor_row, $neighbor_col}) { + $flash_state{$neighbor_row, $neighbor_col} = 1; + $flash_count++; + push @queue => [ $neighbor_row, $neighbor_col ]; + } + } + } + foreach my $row (0 .. $rows -2) { + foreach my $column (0 .. $columns - 2) { + if (exists $flash_state{$row, $column} && + $flash_state{$row, $column} == 1) { + $octopuses[$row][$column] = 0; + } + } + } + #print_octopuses(\@octopuses, $rows, $columns); +} + +say "part 1 flash counts = $flash_count";