]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 3: greedy algorithm with regexes
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 3 Dec 2025 05:37:16 +0000 (06:37 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 3 Dec 2025 05:37:16 +0000 (06:37 +0100)
2025/05.pl [new file with mode: 0755]
2025/06.pl [new file with mode: 0755]

diff --git a/2025/05.pl b/2025/05.pl
new file mode 100755 (executable)
index 0000000..e841b90
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/perl -w
+
+use v5.42;
+
+my $sum;
+ROW:
+while (<>) {
+       for my $x (reverse 1 .. 9) {
+               for my $y (reverse 1 .. 9) {
+                       if (/$x.*$y/) {
+                               $sum += "$x$y";
+                               next ROW;
+                       }
+               }
+       }
+}
+
+say $sum;
diff --git a/2025/06.pl b/2025/06.pl
new file mode 100755 (executable)
index 0000000..f2a5fde
--- /dev/null
@@ -0,0 +1,20 @@
+#!/usr/bin/perl -w
+
+use v5.42;
+use List::Util qw(sum);
+
+my $len = 12;
+
+sub search {
+       my ($d, $str) = @_;
+       
+       return $str if length $str == $len;
+
+       for my $n (reverse 1 .. 9) {
+               my $re = $n . ('.' x ($len - 1 - length $str));
+               next if $d !~ /$re/;
+               return search($d =~ s/^.*?$n//r, "$str$n");
+       }
+}
+
+say sum map { chomp; search($_, '') } <>;