]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 11: not very clean part 2
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 11 Dec 2024 07:14:18 +0000 (08:14 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 11 Dec 2024 07:14:18 +0000 (08:14 +0100)
2024/21.pl [new file with mode: 0755]
2024/22.pl [new file with mode: 0755]

diff --git a/2024/21.pl b/2024/21.pl
new file mode 100755 (executable)
index 0000000..0b1cd34
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my @st = <> =~ /\d+/g;
+
+for (1 .. 25) {
+       my @new;
+       for my $s (@st) {
+               my $l = length $s;
+               if (!$s) {
+                       push @new, 1;
+               } elsif ($l % 2 == 0) {
+                       push @new, 0+substr($s, 0, $l/2),
+                               0+substr($s, $l/2);
+               } else {
+                       push @new, 2024*$s;
+               }
+       }
+       @st = @new;
+}
+
+say $#st;
diff --git a/2024/22.pl b/2024/22.pl
new file mode 100755 (executable)
index 0000000..5819be5
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my @st = <> =~ /\d+/g;
+
+sub step {
+       my (@st) = @_;
+       my @new;
+       for my $s (@st) {
+               my $l = length $s;
+               if (!$s) {
+                       push @new, 1;
+               } elsif ($l % 2 == 0) {
+                       push @new, 0+substr($s, 0, $l/2),
+                               0+substr($s, $l/2);
+               } else {
+                       push @new, 2024*$s;
+               }
+       }
+       @new;
+}
+
+my %seen;
+my %cache5;
+my @q = @st;
+while (defined (my $s = shift @q)) {
+       next if $seen{$s}++;
+       my @rv = ($s);
+       @rv = step(@rv) for 1 .. 5;
+       for my $r (@rv) {
+               if (!$cache5{$s}{$r}++) {
+                       if (!defined $cache5{$r}) {
+                               $cache5{$r} = {};
+                               push @q, $r;
+                       }
+               }
+       }
+}
+
+my %cache = %cache5;
+for (1 .. 14) {
+       my %ncache;
+       for my $s (keys %cache) {
+               for my $d (keys %{ $cache{$s} }){
+                       for my $e (keys %{ $cache5{$d} }) {
+                               $ncache{$s}{$e} += $cache{$s}{$d} * $cache5{$d}{$e};
+                       }
+               }
+       }
+       %cache = %ncache;
+}
+
+my $sum;
+for my $s (@st) {
+       for my $d (keys %{ $cache{$s} }) {
+               $sum += $cache{$s}{$d};
+       }
+}
+say $sum;