Skip to content

Commit

Permalink
Year 2024 Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Dec 12, 2024
1 parent 27b869e commit 18fa77e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2024/day11.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
stones = STDIN.read.split.map(&:to_i)

def stone_count(stone, iterations, memo)
return 1 if iterations.zero?
return memo[[stone, iterations]] unless memo[[stone, iterations]].nil?

result = (
case
when stone == 0
stone_count(1, iterations - 1, memo)
when stone.to_s.size.even?
stone.to_s.chars.each_slice(stone.to_s.size / 2).map(&:join).map(&:to_i).map do |num|
stone_count(num, iterations - 1, memo)
end.sum
else
stone_count(stone * 2024, iterations - 1, memo)
end
)
memo[[stone, iterations]] = result
result
end

memo = {}
puts stones.map { |stone| stone_count(stone, 25, memo) }.sum
puts stones.map { |stone| stone_count(stone, 75, memo) }.sum
1 change: 1 addition & 0 deletions 2024/examples/day11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17

0 comments on commit 18fa77e

Please sign in to comment.