aoc2021

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

commit e13810840718404f4a19fb0a28afd909dbe3bf75
parent 86abecef2c273ee73f272cd71ab52f95317728f1
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 23 Dec 2021 16:43:48 +0000

update day10-1.2.pl to provide working solution for Day 10 Part 1

Diffstat:
Mday10-1.2.pl | 83+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 39 insertions(+), 44 deletions(-)

diff --git a/day10-1.2.pl b/day10-1.2.pl @@ -18,57 +18,52 @@ sub get_filehandle { my $filehandle = get_filehandle(); chomp( my @input = ( <$filehandle> ) ); -my @symbols = qw( braces brackets parentheses comparison ); -my %symbol_count; -my %count_map = ( - '[' => ['braces', 1], - ']' => ['braces', -1], - '{' => ['brackets', 1], - '}' => ['brackets', -1], - '(' => ['parentheses', 1], - ')' => ['parentheses', -1], - '<' => ['comparison', 1], - '>' => ['comparison', -1], +my %closer_for = ( + ']' => '[', + '}' => '{', + ')' => '(', + '>' => '<', ); -foreach my $line (@input) { - %symbol_count = map { $_ => 0 } @symbols; # reset count for each line - my $inner_most; # inner most opening character - say $line; - -# $symbol is each character (e.g. '[', '}', '(', '>') in the line. -# The value of $count_map{$symbol} is a reference to a two-element array. -# The first element of the array ([0]) is the name of matching open/close -# character (e.g. "braces", "brackets", etc.) The second element of that -# array ([1]) is an integer which indicates whether it is an opening -# character (1) or closing character (-1). We can reference these array -# values by dereferencing: -# $count_map{$symbol}->[0] or $count_map{$symbol}->[1] -# These values are used to update the count of each symbol stored in -# %symbol_count. - - foreach my $symbol (split //, $line) { - say "$symbol $count_map{$symbol}->[0]\t$count_map{$symbol}->[1]"; - $symbol_count{$count_map{$symbol}->[0]} += $count_map{$symbol}->[1]; -# if the symbol is an opening character, note that in $inner_most - $inner_most = $count_map{$symbol}->[0] if ($count_map{$symbol}->[0] == 1); - #print Dumper (\%symbol_count); - } - print Dumper (\%symbol_count); -} - -my @openers = qw/ [ { ( < /; -my @closers = qw/ ] } ) > /; -say "@openers"; -say "@closers"; +my %opener_for = ( + '[' => ']', + '{' => '}', + '(' => ')', + '<' => '>', +); +my %error_score = ( + ']' => 57, + '}' => 1197, + ')' => 3, + '>' => 25137, +); +my $error_score = 0; foreach my $line (@input) { my @chunk; say $line; foreach my $symbol (split //, $line) { - if (grep { /$symbol/ } @openers) { - say "$symbol is an opener"; +# 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 (grep { /$symbol/ } @closers) { +# 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}; + last; # go to next line + } } } } + +say "error score is $error_score";