#!/usr/bin/perl use v5.35.0; use List::Util 'sum'; # Code mine, algorithm not. Based on a reddit comment here: # https://www.reddit.com/r/adventofcode/comments/1hbqt2e/comment/m1j6c5s/ my $stones = {}; while (<>) { while (/(\d+)/g) { $$stones{$1}++; } } for (1 .. 75) { my $new_stones = {}; while (my ($engraving, $count) = each %$stones) { if ($engraving == 0) { $$new_stones{1} += $count; } else { my $len = length($engraving); if ($len % 2 == 0) { $len /= 2; $$new_stones{0 + substr($engraving, 0, $len)} += $count; $$new_stones{0 + substr($engraving, $len)} += $count; } else { $$new_stones{$engraving * 2024} += $count; } } } $stones = $new_stones; # blink! } say sum(values %$stones)