Cleanup and ready for day 10
This commit is contained in:
parent
ab3eec6687
commit
3e89255cdb
|
@ -9,5 +9,4 @@ proc solve_for_day(n: int) {.used.} =
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
# solve_all()
|
# solve_all()
|
||||||
# solve_for_day(1)
|
solve_for_day(10)
|
||||||
solve_for_day(9)
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ let targetSum = 2020
|
||||||
|
|
||||||
proc findComplement*(nums: openArray[int], complement = targetSum): Option[(int, int)] =
|
proc findComplement*(nums: openArray[int], complement = targetSum): Option[(int, int)] =
|
||||||
var targets = initHashSet[int]()
|
var targets = initHashSet[int]()
|
||||||
echo nums
|
|
||||||
for n in nums:
|
for n in nums:
|
||||||
if targets.contains(complement - n): return some(((complement - n), n))
|
if targets.contains(complement - n): return some(((complement - n), n))
|
||||||
else: targets.incl n
|
else: targets.incl n
|
||||||
|
|
7
2020/src/day10.nim
Normal file
7
2020/src/day10.nim
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import streams
|
||||||
|
|
||||||
|
proc part1*(s: Stream): int =
|
||||||
|
0
|
||||||
|
|
||||||
|
proc part2*(s: Stream): int =
|
||||||
|
0
|
|
@ -5,11 +5,11 @@ const WINDOW = 25
|
||||||
proc part1*(s: Stream): int =
|
proc part1*(s: Stream): int =
|
||||||
var rollingWindow = initDeque[int](WINDOW)
|
var rollingWindow = initDeque[int](WINDOW)
|
||||||
for i in s.asInts:
|
for i in s.asInts:
|
||||||
echo i
|
|
||||||
if rollingWindow.len < WINDOW:
|
if rollingWindow.len < WINDOW:
|
||||||
rollingWindow.addFirst(i)
|
rollingWindow.addFirst(i)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
# TODO: this toSeq every iteration is probably nasty
|
||||||
let maybe = toSeq(rollingWindow).findComplement(i)
|
let maybe = toSeq(rollingWindow).findComplement(i)
|
||||||
if maybe.isSome:
|
if maybe.isSome:
|
||||||
rollingWindow.addFirst(i)
|
rollingWindow.addFirst(i)
|
||||||
|
@ -24,11 +24,9 @@ proc part2*(s: Stream): int =
|
||||||
var mn = n
|
var mn = n
|
||||||
var mx = 0
|
var mx = 0
|
||||||
var sum = nums[i]
|
var sum = nums[i]
|
||||||
echo i, " i: ", nums[i]
|
|
||||||
mn = min(nums[i], mn)
|
mn = min(nums[i], mn)
|
||||||
mx = max(nums[i], mx)
|
mx = max(nums[i], mx)
|
||||||
for j in i+1..<nums.len:
|
for j in i+1..<nums.len:
|
||||||
echo j, " j: ", nums[j], " -- ", sum
|
|
||||||
mn = min(nums[j], mn)
|
mn = min(nums[j], mn)
|
||||||
mx = max(nums[j], mx)
|
mx = max(nums[j], mx)
|
||||||
if sum == n: return mn+mx
|
if sum == n: return mn+mx
|
||||||
|
|
Loading…
Reference in a new issue