From: Jan "Yenya" Kasprzak Date: Fri, 10 Dec 2021 05:26:15 +0000 (+0100) Subject: Day 10: nice text-based substitutions X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc2021.git;a=commitdiff_plain;h=fd980bac3eaa239f9477c07099abd96165c58336 Day 10: nice text-based substitutions --- diff --git a/19.pl b/19.pl new file mode 100755 index 0000000..419030b --- /dev/null +++ b/19.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl -w + +use v5.16; + +my %score_of = ( + ')' => 3, + ']' => 57, + '}' => 1197, + '>' => 25137, +); + +my $sum; +while (<>) { + chomp; + 1 while s/\(\)|\[\]|\{\}|\<\>//; + s/^[\(\[\{\<]*//; + next if !length; + $sum += $score_of{substr($_, 0,1)}; +} + +say $sum; + diff --git a/20.pl b/20.pl new file mode 100755 index 0000000..30f517f --- /dev/null +++ b/20.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl -w + +use v5.16; + +my %score_of = ( + '(' => 1, + '[' => 2, + '{' => 3, + '<' => 4, +); + +my @sums; +while (<>) { + chomp; + my $sum = 0; + 1 while s/\(\)|\[\]|\{\}|\<\>//; + next if /[\)\]\}\>]/; + next if !length; + for my $c (reverse split //) { + $sum *= 5; + $sum += $score_of{$c}; + } + push @sums, $sum; +} + +@sums = sort { $a <=> $b } @sums; +say $sums[@sums/2]; +