]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 13: linear equations
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 13 Dec 2024 12:09:40 +0000 (13:09 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 13 Dec 2024 12:09:40 +0000 (13:09 +0100)
2024/25.pl [new file with mode: 0755]
2024/26.pl [new file with mode: 0755]

diff --git a/2024/25.pl b/2024/25.pl
new file mode 100755 (executable)
index 0000000..be0ca50
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my $cost;
+while (1) {
+       my ($ax, $ay) = <> =~/\d+/g;
+       my ($bx, $by) = <> =~/\d+/g;
+       my ($px, $py) = <> =~/\d+/g;
+
+       for my $na (1 .. 100) {
+               last if $ax * $na > $px;
+               last if $ay * $na > $py;
+               next if ($px - $ax * $na) % $bx;
+               next if ($py - $ay * $na) % $by;
+               next if ($px - $ax * $na) / $bx != ($py - $ay * $na) / $by;
+               my $c = $na*3 + ($px - $ax * $na) / $bx;
+               $cost += $c;
+
+               last;
+       }
+       last if !defined <>;
+}
+
+say $cost;
diff --git a/2024/26.pl b/2024/26.pl
new file mode 100755 (executable)
index 0000000..4b6b4a3
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my $cost;
+while (1) {
+       my ($ax, $ay) = <> =~/\d+/g;
+       my ($bx, $by) = <> =~/\d+/g;
+       my ($px, $py) = <> =~/\d+/g;
+       $px += 10000000000000;
+       $py += 10000000000000;
+
+       my $nb = ($px*$ay - $py*$ax) / ($bx*$ay - $ax*$by);
+       my $na = ($px - $nb*$bx) / $ax;
+
+       $cost += 3*$na+$nb
+               if $na == int($na) && $nb == int($nb);
+
+       last if !defined <>;
+}
+say $cost;