From 86081b2b0b39d30d0e085d2926a5ab66931b6fb5 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 10 Dec 2024 21:18:43 -0800 Subject: [PATCH] [2024][ruby][11.2] --- 2024/ruby/day_11.rb | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/2024/ruby/day_11.rb b/2024/ruby/day_11.rb index eba46bc..3f6c8cc 100644 --- a/2024/ruby/day_11.rb +++ b/2024/ruby/day_11.rb @@ -1,18 +1,21 @@ stones = DATA.read.scan(/\d+/).map(&:to_i) -25.times { - stones = stones.flat_map {|n| - if n.zero? - [1] - elsif (s = n.to_s.size) && s.even? - [n.to_s[0...s/2], n.to_s[s/2..]].map(&:to_i) - else - [n * 2024] - end - } +blink = Hash.new {|h, k| + n, times = k + h[k] = if times.zero? + 1 + elsif n.zero? + h[[1, times-1]] + elsif (s = n.to_s.size) && s.even? + a, b = [n.to_s[0...s/2], n.to_s[s/2..]].map(&:to_i) + h[[a, times-1]] + h[[b, times-1]] + else + h[[n*2024, times-1]] + end } -pp stones.length +pp stones.sum { blink[[_1, 25]] } +pp stones.sum { blink[[_1, 75]] } __END__ -1750884 193 866395 7 1158 31 35216 0 +125 17