]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 11: dynamic programming
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 11 Dec 2025 06:47:56 +0000 (07:47 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 11 Dec 2025 06:47:56 +0000 (07:47 +0100)
2025/21.pl [new file with mode: 0755]
2025/22.pl [new file with mode: 0755]

diff --git a/2025/21.pl b/2025/21.pl
new file mode 100755 (executable)
index 0000000..f965ab3
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/perl -w
+
+use v5.42;
+
+my %g = map { my @x = /\w+/g; shift(@x) => [ @x ] } <>;
+my $sum;
+my @q = 'you';
+while (my $p = shift @q) {
+       if ($p eq 'out') {
+               $sum++;
+               next;
+       }
+       push @q, $_ for $g{$p}->@*;
+}
+say $sum;
diff --git a/2025/22.pl b/2025/22.pl
new file mode 100755 (executable)
index 0000000..5b7c191
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/perl -w
+
+use v5.42;
+
+my %g = map { my @x = /\w+/g; shift(@x) => [ @x ] } <>;
+
+my @paths;
+for my $node (keys %g) {
+       $paths[0]{$node}{$_} = 1 for $g{$node}->@*;
+}
+
+for my $i (1 .. keys %g) {
+       for my $node (keys %g) {
+               for my $next ($g{$node}->@*) {
+                       $paths[$i]{$node}{$_} += $paths[$i-1]{$next}{$_}
+                               for keys $paths[$i-1]{$next}->%*;
+               }
+       }
+}
+
+my %all;
+for my $p (@paths) {
+       for my $node (keys %$p) {
+               $all{$node}{$_} += $p->{$node}{$_}
+                       for keys $p->{$node}->%*;
+       }
+}
+
+no warnings 'uninitialized'; # only one direction between dac and fft exists
+say $all{svr}{fft} * $all{fft}{dac} * $all{dac}{out}
+  + $all{svr}{dac} * $all{dac}{fft} * $all{fft}{out};