Day eleven
This commit is contained in:
parent
57715c25ee
commit
7042623609
2 changed files with 37 additions and 0 deletions
1
11/input
Normal file
1
11/input
Normal file
|
@ -0,0 +1 @@
|
|||
4 4841539 66 5279 49207 134 609568 0
|
36
11/main.rb
Executable file
36
11/main.rb
Executable 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
|
Loading…
Reference in a new issue