Cleanup and ready for day 10

This commit is contained in:
Daniel Flanagan 2020-12-09 09:44:13 -06:00
parent ab3eec6687
commit 3e89255cdb
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
4 changed files with 9 additions and 6 deletions

View file

@ -9,5 +9,4 @@ proc solve_for_day(n: int) {.used.} =
when isMainModule:
# solve_all()
# solve_for_day(1)
solve_for_day(9)
solve_for_day(10)

View file

@ -4,7 +4,6 @@ let targetSum = 2020
proc findComplement*(nums: openArray[int], complement = targetSum): Option[(int, int)] =
var targets = initHashSet[int]()
echo nums
for n in nums:
if targets.contains(complement - n): return some(((complement - n), n))
else: targets.incl n

7
2020/src/day10.nim Normal file
View file

@ -0,0 +1,7 @@
import streams
proc part1*(s: Stream): int =
0
proc part2*(s: Stream): int =
0

View file

@ -5,11 +5,11 @@ const WINDOW = 25
proc part1*(s: Stream): int =
var rollingWindow = initDeque[int](WINDOW)
for i in s.asInts:
echo i
if rollingWindow.len < WINDOW:
rollingWindow.addFirst(i)
continue
else:
# TODO: this toSeq every iteration is probably nasty
let maybe = toSeq(rollingWindow).findComplement(i)
if maybe.isSome:
rollingWindow.addFirst(i)
@ -24,11 +24,9 @@ proc part2*(s: Stream): int =
var mn = n
var mx = 0
var sum = nums[i]
echo i, " i: ", nums[i]
mn = min(nums[i], mn)
mx = max(nums[i], mx)
for j in i+1..<nums.len:
echo j, " j: ", nums[j], " -- ", sum
mn = min(nums[j], mn)
mx = max(nums[j], mx)
if sum == n: return mn+mx