commit 94cc980d75d836c67af1cdb95b36d315d42f4376
parent 3a494046ad22ec3cc42ea78637cf4fa58cfde020
Author: Samir Parikh <noreply@samirparikh.com>
Date: Sun, 5 Dec 2021 15:55:28 +0000
cleanup after refactoring into initial subroutines
Diffstat:
M | day04-1.pl | | | 60 | +++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 31 insertions(+), 29 deletions(-)
diff --git a/day04-1.pl b/day04-1.pl
@@ -7,47 +7,49 @@ use Data::Dumper;
use JSON;
use Data::Printer;
-if (@ARGV !=1) {
- die "Usage: $0 [input-filename]";
+sub get_filehandle {
+ if (@ARGV !=1) {
+ die "Usage: $0 [input-filename]";
+ }
+ my $input_filename = $ARGV[0];
+ open my $filehandle, '<', $input_filename or
+ die "Could not open input file $input_filename: $!";
+ return $filehandle;
}
-my $input_filename = $ARGV[0];
-open my $filehandle, '<', $input_filename or
- die "Could not open input file $input_filename: $!";
-
-chomp( my $numbers = ( <$filehandle> ) );
-chomp( my @input = ( <$filehandle> ) );
-
-# Advent of Code 2021 Day 04 Part 1
+sub get_numbers {
+ my $fh = shift;
+ chomp( my $numbers = ( <$fh> ) );
+ return (split( ",", $numbers));
+}
-my @numbers = split( ",", $numbers );
-#say foreach (@input);
-my @boards;
-my @new_board;
-say @new_board;
-foreach (@input) {
- #say $_;
- if (/\A\s*\Z/) {
- push(@boards, [@new_board]) if (@new_board);
- say "blank line here";
- @new_board = ();
- } else {
- say $_;
- push(@new_board, [split]);
+sub initialize_boards {
+ my $fh = shift;
+ chomp( my @input = ( <$fh> ) );
+ my (@boards, @new_board);
+ foreach (@input) {
+ if (/\A\s*\Z/) {
+ push(@boards, [@new_board]) if (@new_board);
+ @new_board = ();
+ } else {
+ push(@new_board, [split]);
+ }
}
+ push(@boards, \@new_board);
+ return (@boards);
}
-push(@boards, \@new_board);
-say $boards[1][1][1];
-#print Dumper (\@boards);
-#print to_json( \@boards, {pretty => 1});
+# Advent of Code 2021 Day 04 Part 1
+my $filehandle = get_filehandle();
+my @numbers = get_numbers($filehandle);
+my @boards = initialize_boards($filehandle);
+
p(@boards);
my %state;
foreach my $board (@boards) {
foreach my $row (@$board) {
foreach my $column (@$row) {
- #print "$column - ";
$state{$board}{$row}{$column} = 0;
}
}