commit 0c04f3a8d45e912ae13fa676a295705b3cdf678e
parent e94c1074ed21dcdda1de546d7c30fc095561662b
Author: Samir Parikh <noreply@samirparikh.com>
Date: Thu, 23 Dec 2021 19:04:30 +0000
clean up code for day10.pl
Diffstat:
1 file changed, 2 insertions(+), 16 deletions(-)
diff --git a/day10.pl b/day10.pl
@@ -1,10 +1,9 @@
#!/usr/bin/env perl
-# Day 10 Part 1
+# Day 10 Parts 1 and 2
use strict;
use warnings;
use v5.22;
-use Data::Dumper;
sub get_filehandle {
if (@ARGV !=1) {
@@ -47,48 +46,35 @@ my @completion_scores = ();
foreach my $line (@input) {
my @chunk;
my $state = "incomplete";
- print $line;
foreach my $symbol (split //, $line) {
# if the symbol is an opening symbol, add it to the @chunk array
if ( $symbol =~ m/\[|\{|\(|\</ ) {
push @chunk => $symbol;
- #print "$symbol is an opener. ";
- #say "current chunk is @chunk";
}
# if the symbol is a closing symbol...
if ( $symbol =~ m/\]|\}|\)|\>/ ) {
- #say "$symbol is a closer";
# ...check if the symbol is a closing symbol for the last opened one
if ($closer_for{$symbol} eq $chunk[-1]) {
- #say "$symbol is a matching closer";
# pop off last opening symbol from the @chunk array
pop @chunk;
- #say "current chunk is @chunk";
# otherwise, report an error
} else {
- #say "Expected $opener_for{$chunk[-1]}, but found $symbol instead.";
$error_score += $error_score{$symbol};
$state = "corrupted";
last; # go to next line
}
}
}
- #say "\t$state";
if ($state eq "incomplete") {
my $completion_score = 0;
- print "\t$state\t";
foreach (reverse @chunk) {
- print ;
$completion_score = (5 * $completion_score) +
$completion_score{$opener_for{$_}};
}
- print "\n";
push @completion_scores => $completion_score;
- } else {
- say "\t$state";
}
}
+
@completion_scores = sort { $a <=> $b } @completion_scores;
say "part 1 $error_score";
-#say "part 2 @completion_scores";
say "part 2 ", $completion_scores[(scalar @completion_scores) / 2];