commit fd1575bfe691d992219363068cd341e90a85eef6
parent a3ada4a29f4d46e2ad47e985e1d99cd908b0046e
Author: Samir Parikh <noreply@samirparikh.com>
Date: Mon, 6 Dec 2021 15:59:45 +0000
cleanup day04-1.pl
Diffstat:
1 file changed, 5 insertions(+), 21 deletions(-)
diff --git a/day04-1.pl b/day04-1.pl
@@ -3,9 +3,6 @@
use strict;
use warnings;
use v5.22;
-#use Data::Dumper;
-#use JSON;
-#use Data::Printer;
sub get_filehandle {
if (@ARGV !=1) {
@@ -54,8 +51,7 @@ sub initialize_state {
sub play_turn {
my ($number, $boards, $state) = @_;
- #my $number = shift(@$numbers);
- say "playing number $number";
+ printf "playing number %2d\n", $number;
foreach my $board (@$boards) { # $board is ref to board array
foreach my $row (@$board) { # $row is ref to board row array
foreach my $column (@$row) { # $column is element of row array
@@ -67,6 +63,7 @@ sub play_turn {
}
}
+# for debugging purposes only
sub print_board_state {
my ($boards, $state) = @_;
foreach my $board (@$boards) { # $board is ref to board array
@@ -85,7 +82,6 @@ sub print_board_state {
sub check_for_winner {
my ($boards, $state) = @_;
- #my $found_winner = 0;
# check for row wins
foreach my $board (@$boards) { # $board is ref to board array
foreach my $row (@$board) { # $row is ref to board row array
@@ -95,8 +91,7 @@ sub check_for_winner {
$total += $state->{$board}{$row}{$column};
}
if ($total == 5) {
- say "found row winner";
- #$found_winner = 1;
+ #say "found row winner";
return $board;
}
}
@@ -110,7 +105,7 @@ sub check_for_winner {
$total += $state->{$board}{$row}{$row->[$_]};
}
if ($total == 5) {
- say "found column winner";
+ #say "found column winner";
return $board;
}
}
@@ -126,12 +121,8 @@ sub calculate_score {
if ($state->{$board}{$row}{$column} == 0) {
$score += $column;
}
-# if ($column == $number) {
-# $state->{$board}{$row}{$column} = 1;
-# }
}
}
- #}
return $score *= $number;
}
@@ -152,7 +143,7 @@ my $winner_found = 0;
until ($winner_found) {
$turn++;
- say "turn: $turn";
+ printf "turn: %2d, ", $turn;
$number = shift( @numbers );
play_turn($number, \@boards, \%state);
$winner_found = check_for_winner(\@boards, \%state);
@@ -161,12 +152,5 @@ until ($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);
-
-#p(@boards);
-#p(%state);