aoc2021

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

commit e94c1074ed21dcdda1de546d7c30fc095561662b
parent 6be072db3d40df7bc79e7d24a6ef2e7879871a2d
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 23 Dec 2021 18:58:10 +0000

remove prior attempts at solution contained in day10-1.2.pl and day10-1.pl

Diffstat:
Dday10-1.2.pl | 69---------------------------------------------------------------------
Dday10-1.pl | 57---------------------------------------------------------
2 files changed, 0 insertions(+), 126 deletions(-)

diff --git a/day10-1.2.pl b/day10-1.2.pl @@ -1,69 +0,0 @@ -#!/usr/bin/env perl - -# Day 10 Part 1 -use strict; -use warnings; -use v5.22; -use Data::Dumper; - -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 $filehandle = get_filehandle(); -chomp( my @input = ( <$filehandle> ) ); -my %closer_for = ( - ']' => '[', - '}' => '{', - ')' => '(', - '>' => '<', -); -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 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}; - last; # go to next line - } - } - } -} - -say "error score is $error_score"; diff --git a/day10-1.pl b/day10-1.pl @@ -1,57 +0,0 @@ -#!/usr/bin/env perl - -# Day 10 Part 1 -use strict; -use warnings; -use v5.22; -use Data::Dumper; - -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 $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], -); -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); -}