From 704262360982fef682da21f3456d875427377d21 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Wed, 11 Dec 2024 21:25:03 -0500 Subject: [PATCH] Day eleven --- 11/input | 1 + 11/main.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 11/input create mode 100755 11/main.rb diff --git a/11/input b/11/input new file mode 100644 index 0000000..6142429 --- /dev/null +++ b/11/input @@ -0,0 +1 @@ +4 4841539 66 5279 49207 134 609568 0 \ No newline at end of file diff --git a/11/main.rb b/11/main.rb new file mode 100755 index 0000000..0be8776 --- /dev/null +++ b/11/main.rb @@ -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