Skip to content

Instantly share code, notes, and snippets.

@mscha
Created December 11, 2024 14:22
Show Gist options
  • Save mscha/c30edf0f2674620b2ab370cc3e1f433a to your computer and use it in GitHub Desktop.
Save mscha/c30edf0f2674620b2ab370cc3e1f433a to your computer and use it in GitHub Desktop.
Advent of Code 2024 day 11 - first attempt (too slow for part 2)
#!/usr/bin/env raku
use v6.d;
$*OUT.out-buffer = False; # Autoflush
# Advent of Code 2024 day 11 -- https://adventofcode.com/2024/day/11
class MagicStones
{
has Int @.stones;
method blink
{
loop (my $i = 0; $i ≤ @!stones.end; $i++) {
given @!stones[$i] {
when 0 {
$_ = 1;
}
when .chars %% 2 {
my $half = .chars div 2;
my ($left, $right) = .comb(/\w ** {.chars div 2}/);
@!stones.splice($i, 1, +$left, +$right);
$i++;
}
default {
$_ ×= 2024;
}
}
}
}
}
sub MAIN(IO() $inputfile where *.f = 'aoc11.input', Bool :v(:$verbose) = False)
{
my $stones = MagicStones.new(:stones($inputfile.words».Int));
for 1..25 -> $b {
$stones.blink;
say "Blink $b: $stones.stones.elems() stones; ", $stones.stones if $verbose;
}
say "Part 1: $stones.stones.elems() stones.";
# Unsurprisingly, this becomes way too slow
for 26..75 -> $b {
$stones.blink;
say "Blink $b: $stones.stones.elems() stones; ", $stones.stones if $verbose;
}
say "Part 1: $stones.stones.elems() stones.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment