This commit is contained in:
2023-12-12 00:44:21 -05:00
parent d5f41153b7
commit 379fc7f2e9
2 changed files with 24 additions and 0 deletions

13
2023/day9/p1.rb Normal file
View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
inputs = []
File.foreach('./2023/day9/data') do |line|
inputs << line.split(' ').map(&:to_i)
end
def add_derivative(inputs)
return 0 if inputs.all?(&:zero?)
inputs[-1] + add_derivative(inputs.map.with_index { |x, i| i.zero? ? x : x - inputs[i - 1] }[1..inputs.length])
end
puts inputs.map { |input| add_derivative input }.sum

11
2023/day9/p2.rb Normal file
View File

@@ -0,0 +1,11 @@
inputs = []
File.foreach('./2023/day9/data') do |line|
inputs << line.split(' ').map(&:to_i)
end
def add_derivative(inputs)
return 0 if inputs.all?(&:zero?)
inputs[-1] - add_derivative(inputs.map.with_index { |x, i| i.zero? ? x : inputs[i - 1] - x }[1..inputs.length])
end
puts inputs.map { |input| add_derivative input.reverse }.sum