From 2688950560f3e5a8f4ebce18f343454939bc95fd Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 8 Dec 2020 21:34:39 -0800 Subject: [PATCH] Add day9 solution. --- day9.cr | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 day9.cr diff --git a/day9.cr b/day9.cr new file mode 100644 index 0000000..e44d741 --- /dev/null +++ b/day9.cr @@ -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)