commit 7b099f812ed51fb7c1f74efb91e3e71e8efc1579
parent 09dcac8c2592d88d35dbacda0800573f912de707
Author: Samir Parikh <noreply@samirparikh.com>
Date: Mon, 6 Dec 2021 20:38:40 +0000
get day04-2.pl to provide working solution to Day 04 Part 2
Diffstat:
1 file changed, 5 insertions(+), 8 deletions(-)
diff --git a/day04-2.pl b/day04-2.pl
@@ -91,9 +91,7 @@ sub check_for_winner {
$total += $state->{$board}{$row}{$column};
}
if ($total == 5) {
- #say "found row winner";
push (@winning_board_ref, $board);
- #return $board;
last;
}
}
@@ -107,9 +105,7 @@ sub check_for_winner {
$total += $state->{$board}{$row}{$row->[$_]};
}
if ($total == 5) {
- #say "found column winner";
push (@winning_board_ref, $board);
- #return $board;
last;
}
}
@@ -139,12 +135,13 @@ my @boards = initialize_boards($filehandle);
my %state = initialize_state(\@boards);
my $turn = 0;
my @winner_found;
+my $final_board;
-while ( scalar ( @boards ) > 1 ) {
+while ( scalar ( @boards ) ) {
@winner_found = ();
$turn++;
- printf "turn: %2d, playing number %2d\n", $turn, $number;
$number = shift( @numbers );
+ printf "turn: %2d, playing number %2d\n", $turn, $number;
play_turn($number, \@boards, \%state);
@winner_found = check_for_winner(\@boards, \%state);
if ( @winner_found ) {
@@ -157,9 +154,9 @@ while ( scalar ( @boards ) > 1 ) {
}
unless (@numbers) { # if we run out of numbers
say "Out of numbers. No winner found.";
- #last;
exit;
}
+ $final_board = $boards[0] if ( scalar( @boards ) == 1 );
}
-my $score = calculate_score($number, $boards[0], \%state);
+my $score = calculate_score($number, $final_board, \%state);
say "winning score is $score";