1
0
Fork 0

Day eleven

This commit is contained in:
Andrew Tomaka 2024-12-11 21:25:03 -05:00
parent 57715c25ee
commit 7042623609
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
2 changed files with 37 additions and 0 deletions

1
11/input Normal file
View file

@ -0,0 +1 @@
4 4841539 66 5279 49207 134 609568 0

36
11/main.rb Executable file
View file

@ -0,0 +1,36 @@
#!/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