commit 9627aa19c21ef74417fb11b8e83daa084c18710f
parent 80baa846db2f86143cd57ed47d67deb0992e1a98
Author: Samir Parikh <noreply@samirparikh.com>
Date: Thu, 10 Nov 2022 20:40:31 +0000
work in progress
Diffstat:
2 files changed, 56 insertions(+), 26 deletions(-)
diff --git a/day18/Day18.pm b/day18/Day18.pm
@@ -5,7 +5,18 @@ use warnings;
use v5.32;
use Exporter qw( import );
-our @EXPORT = qw( init_lights update_lights );
+our @EXPORT = qw( init_lights flash_lights display_lights );
+
+sub setup_neighbors
+{
+ my @neighbors;
+ foreach my $nr (-1 .. 1) {
+ foreach my $nc (-1 .. 1) {
+ push @neighbors => [$nr, $nc] unless ($nr == 0 && $nc == 0);
+ }
+ }
+ return \@neighbors;
+}
sub init_lights
{
@@ -24,27 +35,52 @@ sub init_lights
# Add an extra row at the end to serve as a boundary when we do the neighbor
# comparisons later on.
- push @lights => [ '.' x scalar @{ $lights[0] } ];
+ push @lights => [ ('.') x scalar @{ $lights[0] } ];
return \@lights;
}
-sub setup_neighbors
+sub update_light
{
- my @neighbors;
- foreach my $nr (-1 .. 1) {
- foreach my $nc (-1 .. 1) {
- push @neighbors => [$nr, $nc] unless ($nr == 0 && $nc == 0);
+ my $row = shift;
+ my $col = shift;
+ my @lights = @{ shift() };
+ my @neighbors = @{ setup_neighbors() };
+ my $neighbors_on = 0;
+ 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 ]>";
+ $neighbors_on++ if ( $lights[ $neighbor_row ][ $neighbor_col ] eq '#' );
+ }
+ say "light $row, $col has $neighbors_on neighbors that are on";
+ if ( $lights[ $row ][ $col ] eq '#' ) {
+ return ( $neighbors_on == 2 || $neighbors_on == 3 ) ? '#' : '.';
+ } else {
+ return ( $neighbors_on == 3 ) ? '#' : '.';
+ }
+}
+
+sub flash_lights
+{
+ my $current_state = shift;
+ my @next_state = @{ $current_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 );
}
}
- return \@neighbors;
+
+ return \@next_state;
}
-sub update_lights
+sub display_lights
{
- my @lights = @{ shift() };
- my $rows = scalar @lights;
- my $columns = scalar @{ $lights[ 0 ] };
- my @neighbors = @{ setup_neighbors() };
+ say "@{ $_ }" foreach ( @{ shift() } );
}
1;
diff --git a/day18/day18.pl b/day18/day18.pl
@@ -10,23 +10,17 @@ use List::Util qw( max );
@ARGV = "input" unless @ARGV;
chomp( my $input = do { local $/; <> } );
-
-# initialize variables to declare number of steps, arrays to hold current state
-# of lights, the next state, and index offsets for the neighbors, s well as
-# variables to hold number of rows and columns.
-my $STEPS = 4;
+my $STEPS = 1;
my $state = init_lights( $input );
-#my @current_state = @{ init_lights( $input ) };
-#my @next_state;
-#my $rows = scalar @current_state;
-#my $columns = scalar @{ $current_state[ 0 ] };
-#my @neighbors = @{ setup_neighbors() };
+
+say "Initial state:";
+display_lights( $state );
foreach ( 1 .. $STEPS )
{
-# foreach my $row ( 0 .. $rows - 2 ) {
-# foreach my $column ( 0 .. $columns - 2 ) {
- $state = update_lights( $state );
+ $state = flash_lights( $state );
+ say "After step $_:";
+ display_lights( $state );
}
say "part 1: ";
say "part 2: ";