#!/usr/bin/perl use v5.35.0; use Memoize; memoize('stones_after'); my $count = 0; while (<>) { while (/(\d+)/g) { $count += stones_after(75, $1); } } say $count; sub stones_after($blinks, $engraving) { return 1 unless $blinks > 0; --$blinks; return stones_after($blinks, 1) if $engraving == 0; my $len = length($engraving); return stones_after($blinks, $engraving * 2024) if $len % 2; $len /= 2; return stones_after($blinks, 0 + substr($engraving, 0, $len)) + stones_after($blinks, 0 + substr($engraving, $len)); }