Add day9 solution.

This commit is contained in:
Danila Fedorin 2020-12-08 21:34:39 -08:00
parent af8170a3e6
commit 2688950560
1 changed files with 37 additions and 0 deletions

37
day9.cr Normal file
View File

@ -0,0 +1,37 @@
require "advent"
INPUT = input(2020, 9).lines.map(&.to_i64)
def is_sum(is, from, to, n)
if to <= from
return n == 0_i64
end
return is_sum(is, from, to-1, n) || is_sum(is, from, to-1, n-is[to-1])
end
def part1(input)
is = input
i = 25
loop do
return is[i] unless is_sum(is, i-25, i, is[i])
i += 1
end
end
def part2(input, i)
input.each_with_index do |e, j|
next if e == i
acc = i-e
k = j
while acc > 0
k += 1
acc -= input[k]
end
if acc == 0
min, max = input[j..k].minmax
return min+max
end
end
end
p1 = part1(INPUT.clone)
puts part2(INPUT.clone, p1)