]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 3: shorter code for part 2
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 3 Dec 2025 06:14:44 +0000 (07:14 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 3 Dec 2025 06:14:44 +0000 (07:14 +0100)
2025/06.pl

index f2a5fdeadb2317e8e8cff437584e9820e0b41adf..fd27cc9cec3576e15fd899364a643c46449cbf9c 100755 (executable)
@@ -3,18 +3,14 @@
 use v5.42;
 use List::Util qw(sum);
 
-my $len = 12;
-
-sub search {
-       my ($d, $str) = @_;
-       
-       return $str if length $str == $len;
+sub search($in, $res, $len) {
+       return $res if !$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");
+               my $re = $n . ('.' x $len);
+               next if $in !~ /$re/;
+               return search($in =~ s/^.*?$n//r, "$res$n", $len);
        }
 }
 
-say sum map { chomp; search($_, '') } <>;
+say sum map { chomp; search($_, '', 12) } <>;