Solution for Day 09
This commit is contained in:
parent
08d6eb34a4
commit
89090cb0f4
13
day_09/first.rb
Normal file
13
day_09/first.rb
Normal 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
1000
day_09/input
Normal file
File diff suppressed because it is too large
Load Diff
25
day_09/second.rb
Normal file
25
day_09/second.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user