aoc2015

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

commit 80baa846db2f86143cd57ed47d67deb0992e1a98
parent 7d94ac6d2ab0efa4014245fe620c1ff549d66596
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 10 Nov 2022 14:57:47 +0000

start to solve part 1 of day18

Diffstat:
Aday18/Day18.pm | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Aday18/day18.pl | 32++++++++++++++++++++++++++++++++
2 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/day18/Day18.pm b/day18/Day18.pm @@ -0,0 +1,50 @@ +package Day18; + +use strict; +use warnings; +use v5.32; +use Exporter qw( import ); + +our @EXPORT = qw( init_lights update_lights ); + +sub init_lights +{ + my @lights; + + # Define array @lights to hold initial state of lighting configuration. + # Input to subroutine is string which contains puzzle input. This string + # also contains mulitple end of line separators ( \n ). Break up each + # line using split // into an array. Pad the array with a trailing 0 to + # serve as a boundary when we iterate through the array later on. We do + # this by taking advantage that the -1 index ( $lights[ -1 ] ) refers to + # the last element in the array. This is important when doing comparisons + # on the first element of the array as we will wrap back to the last + # element which is just the extra padding. + push @lights => [ ( split // ), '.' ] foreach ( split /\n/, shift ); + + # 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] } ]; + return \@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 update_lights +{ + my @lights = @{ shift() }; + my $rows = scalar @lights; + my $columns = scalar @{ $lights[ 0 ] }; + my @neighbors = @{ setup_neighbors() }; +} + +1; diff --git a/day18/day18.pl b/day18/day18.pl @@ -0,0 +1,32 @@ +#!/usr/local/bin/perl +# day 2015-14 + +use strict; +use warnings; +use v5.32; +use lib '.'; +use Day18; +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 $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() }; + +foreach ( 1 .. $STEPS ) +{ +# foreach my $row ( 0 .. $rows - 2 ) { +# foreach my $column ( 0 .. $columns - 2 ) { + $state = update_lights( $state ); +} +say "part 1: "; +say "part 2: ";