aoc2021/day1/p2.nim
Joey Yakimowich-Payne c257ed1410 Rename day1
2021-12-03 20:17:54 -07:00

38 lines
675 B
Nim

import os
import sequtils
import std/strutils
proc main() =
let fileName = paramStr(1)
var increased = 0
var lastVal = -1
var window = [0, 0, 0]
var windowPos = 0
for val in fileName.lines:
let intVal = val.parseInt
var curVal = 0
window[windowPos] = intVal
if windowPos == (len(window) - 1):
# add the values and store
curVal = window.foldl(a + b)
if lastVal != -1 and lastVal < curVal:
increased += 1
lastVal = curVal
# move values up the window
window[0] = window[1]
window[1] = window[2]
window[2] = intVal
else:
windowPos += 1
echo increased, " increases"
main()