aoc2021

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

commit 7ce68a84a25ccd87fa83d215b351da6a671dccda
parent 5ace50df951bb672c4388992a442a7a66da24630
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 23 Dec 2021 02:17:43 +0000

initial commit for day10-1.pl

Diffstat:
Aday10-1.pl | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+), 0 deletions(-)

diff --git a/day10-1.pl b/day10-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +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); +}