aoc2021

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

commit a3ada4a29f4d46e2ad47e985e1d99cd908b0046e
parent ee74ace205e37586ff7985753f7f1bdf8fccf5b2
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon,  6 Dec 2021 15:53:53 +0000

update day04-1.pl to provide a working solution to Day 04, Part 1!

Diffstat:
Mday04-1.pl | 37+++++++++++++++++++++++++++++--------
1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/day04-1.pl b/day04-1.pl @@ -3,9 +3,9 @@ use strict; use warnings; use v5.22; -use Data::Dumper; -use JSON; -use Data::Printer; +#use Data::Dumper; +#use JSON; +#use Data::Printer; sub get_filehandle { if (@ARGV !=1) { @@ -53,8 +53,8 @@ sub initialize_state { } sub play_turn { - my ($numbers, $boards, $state) = @_; - my $number = shift(@$numbers); + my ($number, $boards, $state) = @_; + #my $number = shift(@$numbers); say "playing number $number"; foreach my $board (@$boards) { # $board is ref to board array foreach my $row (@$board) { # $row is ref to board row array @@ -118,10 +118,28 @@ sub check_for_winner { return 0; # no winner found } +sub calculate_score { + my ($number, $board, $state) = @_; + my $score = 0; + foreach my $row (@$board) { # $row is ref to board row array + foreach my $column (@$row) { # $column is element of row array + if ($state->{$board}{$row}{$column} == 0) { + $score += $column; + } +# if ($column == $number) { +# $state->{$board}{$row}{$column} = 1; +# } + } + } + #} + return $score *= $number; +} + # Advent of Code 2021 Day 04 Part 1 # initialize game variables my $filehandle = get_filehandle(); my @numbers = get_numbers($filehandle); +my $number; my @boards = initialize_boards($filehandle); my %state = initialize_state(\@boards); my $turn = 0; @@ -135,17 +153,20 @@ my $winner_found = 0; until ($winner_found) { $turn++; say "turn: $turn"; - play_turn(\@numbers, \@boards, \%state); + $number = shift( @numbers ); + play_turn($number, \@boards, \%state); $winner_found = check_for_winner(\@boards, \%state); if ((scalar(@numbers)) == 0) { say "Out of numbers. No winner found."; last; } } - say "\$winner_found = $winner_found"; +my $score = calculate_score($number, $winner_found, \%state); +say "winning score is $score"; + #check_for_horiz_winner(\@boards, \%state); -print_board_state(\@boards, \%state); +#print_board_state(\@boards, \%state); #p(@boards); #p(%state);