aoc2021

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

commit e37d98bd6da6cacb94d183c5f6480b837bc5fe7a
parent 8e4bbcf6b7e31c7932b59fbb55ca67b4283c5961
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon,  6 Dec 2021 15:15:21 +0000

got logic to find column winner working

Diffstat:
Mday04-1.pl | 34++++++++++++++++++++++++----------
1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/day04-1.pl b/day04-1.pl @@ -94,7 +94,7 @@ sub check_for_winner { $total += $state->{$board}{$row}{$column}; } if ($total == 5) { - say "found winner"; + say "found row winner"; #$found_winner = 1; return 1; } @@ -104,6 +104,17 @@ sub check_for_winner { say @{${$row}[$_]}; } } + foreach (0 .. 4) { + my $total = 0; + foreach my $row (@$board) { + #say "$board\t$_\t$row->[$_]\t$state->{$board}{$row}{$row->[$_]}"; + $total += $state->{$board}{$row}{$row->[$_]}; + } + if ($total == 5) { + say "found column winner"; + return 1; + } + } } return 0; # no winner found } @@ -133,18 +144,21 @@ my $filehandle = get_filehandle(); my @numbers = get_numbers($filehandle); my @boards = initialize_boards($filehandle); my %state = initialize_state(\@boards); - my $turn = 0; my $winner_found = 0; -#until ($winner_found) { -# $turn++; -# say "turn: $turn"; -# play_turn(\@numbers, \@boards, \%state); -# $winner_found = check_for_winner(\@boards, \%state); -#} -check_for_horiz_winner(\@boards, \%state); -#print_board_state(\@boards, \%state); +until ($winner_found) { + $turn++; + say "turn: $turn"; + play_turn(\@numbers, \@boards, \%state); + $winner_found = check_for_winner(\@boards, \%state); + if ((scalar(@numbers)) == 0) { + say "Out of numbers. No winner found."; + last; + } +} +#check_for_horiz_winner(\@boards, \%state); +print_board_state(\@boards, \%state); #p(@boards); #p(%state);