#!/usr/bin/env ruby require "debug" input = (ARGV.first.nil? ? DATA : ARGF) .read .split(/\s+/) .map(&:to_i) @cache = Hash.new def recursive_transform(stone, blinks = 0) if blinks.zero? return 1 elsif @cache[[blinks, stone]] return @cache[[blinks, stone]] elsif stone.zero? res = recursive_transform(1, blinks - 1) elsif stone.digits.size.even? stone = stone.to_s mid = stone.size / 2 res = [ recursive_transform(stone[...mid].to_i, blinks - 1), recursive_transform(stone[mid..].to_i, blinks - 1) ].sum else res = recursive_transform(stone * 2024, blinks - 1) end @cache[[blinks, stone]] = res end p input.map { recursive_transform(_1, 75) }.sum __END__ 125 17