]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 4: finally regexes!!!
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 4 Dec 2021 09:59:29 +0000 (10:59 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 4 Dec 2021 09:59:29 +0000 (10:59 +0100)
07.pl [new file with mode: 0755]
08.pl [new file with mode: 0755]

diff --git a/07.pl b/07.pl
new file mode 100755 (executable)
index 0000000..c304d7c
--- /dev/null
+++ b/07.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my @drawn = split/[,\s]/, scalar <>;
+scalar <>;
+
+$/ = "\n\n";
+
+my @boards = <>;
+
+say $boards[0];
+
+for my $d (@drawn) {
+       for my $board (@boards) {
+               $board =~ s/\b$d\b/' ' x length $d/e;
+               if ($board =~ /              /
+                       || $board =~ /\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}/xms) {
+                       say $board;
+                       $board =~ s/(?<=\d)\s+(?=\d)/+/gxms;
+                       say "$board";
+                       say eval "$d*($board)";
+                       exit;
+               }
+       }
+}
+
diff --git a/08.pl b/08.pl
new file mode 100755 (executable)
index 0000000..d1f74b4
--- /dev/null
+++ b/08.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my @drawn = split/[,\s]/, scalar <>;
+scalar <>;
+
+$/ = "\n\n";
+
+my @boards = <>;
+
+say $boards[0];
+
+my $remaining = @boards;
+
+for my $d (@drawn) {
+       for my $board (@boards) {
+               $board =~ s/\b$d\b/' ' x length $d/e;
+               if ($board =~ /              /
+                       || $board =~ /\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}/xms) {
+                       if (--$remaining) {
+                               $board = 'x';
+                               next;
+                       }
+                       say $board;
+                       $board =~ s/(?<=\d)\s+(?=\d)/+/gxms;
+                       say "$board";
+                       say eval "$d*($board)";
+                       exit;
+               }
+       }
+}
+