Solution for Day 09

This commit is contained in:
Patrick Michl 2020-12-09 11:05:16 +01:00
parent 08d6eb34a4
commit 89090cb0f4
3 changed files with 1038 additions and 0 deletions

13
day_09/first.rb Normal file
View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
input = File.readlines('./input').map(&:to_i)
preamble = 25
input.each_with_index do |_line, i|
next if i < preamble
unless input[i - preamble, preamble].combination(2).find { |a, b| a + b == input[i] }
puts input[i]
return
end
end

1000
day_09/input Normal file

File diff suppressed because it is too large Load Diff

25
day_09/second.rb Normal file
View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
input = File.readlines('./input').map(&:to_i)
preamble = 25
invalid_num = 21_806_024
input.each_with_index do |_line, i|
next if i < preamble
res = []
range = input[i - preamble, preamble]
range.each_index do |j|
res << range[i..-1] if range[i..-1]&.size.to_i > 1
preamble.times do |k|
res << range[j, k] if range[j, k]&.size > 1
end
end
out = res.uniq.select { |elems| elems.inject(:+) == invalid_num }
next if out.empty?
puts out.flatten.min + out.flatten.max
break
end