]> www.fi.muni.cz Git - aoc2020.git/commitdiff
Day 18. Regexes!
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 18 Dec 2020 05:54:05 +0000 (06:54 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 18 Dec 2020 05:54:05 +0000 (06:54 +0100)
35.pl [new file with mode: 0755]
36.pl [new file with mode: 0755]

diff --git a/35.pl b/35.pl
new file mode 100755 (executable)
index 0000000..5582de3
--- /dev/null
+++ b/35.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $sum = 0;
+while (<>) {
+       chomp;
+       1 while
+               s/\((\d+)\)/$1/ ||
+               s/(\d+)\s*([\+\*])\s*(\d+)/"$1$2$3"/ee;
+       $sum += $_;
+}
+print "sum=$sum\n";
diff --git a/36.pl b/36.pl
new file mode 100755 (executable)
index 0000000..68eb4f9
--- /dev/null
+++ b/36.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $sum = 0;
+while (<>) {
+       chomp;
+       1 while
+               s/\((\d+)\)/$1/ ||
+               s/(\([^\(\)]*?)(\d+)\s*\+\s*(\d+)([^\(\)]*\))/$1.($2+$3).$4/e ||
+               s/(\([^\(\)]*?)(\d+)\s*\*\s*(\d+)([^\(\)]*\))/$1.($2*$3).$4/e ||
+               s/(\d+)\s*([\+])\s*(\d+)/"$1$2$3"/ee ||
+               s/(\d+)\s*([\*])\s*(\d+)/"$1$2$3"/ee;
+       $sum += $_;
+}
+print "sum=$sum\n";