From fd980bac3eaa239f9477c07099abd96165c58336 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 10 Dec 2021 06:26:15 +0100 Subject: [PATCH] Day 10: nice text-based substitutions --- 19.pl | 22 ++++++++++++++++++++++ 20.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 19.pl create mode 100755 20.pl 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]; + -- 2.43.5