From 18fa77e8254322788f3bfca3c7361356095cfe6d Mon Sep 17 00:00:00 2001 From: Tudor Pavel Date: Thu, 12 Dec 2024 22:18:18 +0200 Subject: [PATCH] Year 2024 Day 11 --- 2024/day11.rb | 25 +++++++++++++++++++++++++ 2024/examples/day11.txt | 1 + 2 files changed, 26 insertions(+) create mode 100644 2024/day11.rb create mode 100644 2024/examples/day11.txt diff --git a/2024/day11.rb b/2024/day11.rb new file mode 100644 index 0000000..902a32e --- /dev/null +++ b/2024/day11.rb @@ -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 diff --git a/2024/examples/day11.txt b/2024/examples/day11.txt new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/2024/examples/day11.txt @@ -0,0 +1 @@ +125 17