Last active
December 11, 2024 15:33
-
-
Save mscha/f285e3d052b828ff015cd59d467802dc to your computer and use it in GitHub Desktop.
Advent of Code 2024 day 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env raku | |
use v6.d; | |
$*OUT.out-buffer = False; # Autoflush | |
# Advent of Code 2024 day 11 -- https://adventofcode.com/2024/day/11 | |
sub split-in-half($n) { $n.comb(/\w ** { $n.chars div 2 }/)».Int } | |
use experimental :cached; | |
multi blink($stone, $times) is cached | |
{ | |
return 1 if $times == 0; | |
my @new-stones = do given $stone { | |
when 0 { 1 } | |
when .chars %% 2 { split-in-half($_) } | |
default { $_ × 2024 } | |
} | |
return blink(@new-stones, $times-1).sum; | |
} | |
multi blink(@stones, $times) { @stones».&blink($times).sum } | |
sub MAIN(IO() $inputfile where *.f = 'aoc11.input') | |
{ | |
my @stones = $inputfile.words».Int; | |
say "Part 1: ", blink(@stones, 25); | |
say "Part 2: ", blink(@stones, 75); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment