From c81a40b1cc6ed9635a2b1230e45374bbf869bc29 Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Sun, 15 Dec 2019 22:26:17 -0800 Subject: [PATCH] 2019: Day 16 Part 2 --- 2019/day16/part2.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2019/day16/part2.rb diff --git a/2019/day16/part2.rb b/2019/day16/part2.rb new file mode 100644 index 0000000..74b87f1 --- /dev/null +++ b/2019/day16/part2.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +def run(text) + numbers = text.strip.split('').map(&:to_i) * 10_000 + offset = numbers[0, 7].join.to_i + arr = numbers[offset..-1] + + 100.times do + # All coefficients are + # * 0: when before output index + # * 1: when at or after output index + # The pattern is long enough that there are no -1's or later 0's. + updated = [] + total = arr.sum + arr.each do |elt| + updated << total.abs % 10 + total -= elt + end + arr = updated + end + arr[0, 8].join +end + +input = File.read(File.join(__dir__, 'input')) +puts run(input)