advent-of-code/2020/src/day9.nim

36 lines
886 B
Nim
Raw Normal View History

2020-12-10 12:37:10 -06:00
import streams, sequtils, options, deques, input_helpers, day1
2020-12-09 09:37:54 -06:00
const WINDOW = 25
proc part1*(s: Stream): int =
var rollingWindow = initDeque[int](WINDOW)
for i in s.asInts:
if rollingWindow.len < WINDOW:
rollingWindow.addFirst(i)
continue
else:
2020-12-09 09:44:13 -06:00
# TODO: this toSeq every iteration is probably nasty
2020-12-09 09:37:54 -06:00
let maybe = toSeq(rollingWindow).findComplement(i)
if maybe.isSome:
rollingWindow.addFirst(i)
rollingWindow.popLast
else: return i
proc part2*(s: Stream): int =
let n = s.part1
s.setPosition 0
let nums = toSeq(s.asInts)
for i in 0..<nums.len:
var mn = n
var mx = 0
var sum = nums[i]
mn = min(nums[i], mn)
mx = max(nums[i], mx)
for j in i+1..<nums.len:
mn = min(nums[j], mn)
mx = max(nums[j], mx)
if sum == n: return mn+mx
if sum > n: continue
sum += nums[j]