]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 10: nice text-based substitutions
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 10 Dec 2021 05:26:15 +0000 (06:26 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 10 Dec 2021 05:26:15 +0000 (06:26 +0100)
19.pl [new file with mode: 0755]
20.pl [new file with mode: 0755]

diff --git a/19.pl b/19.pl
new file mode 100755 (executable)
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 (executable)
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];
+