Add day1 problem + solution
This commit is contained in:
parent
32599c8441
commit
bde3a8968d
4 changed files with 2139 additions and 0 deletions
38
day1/day1p2.nim
Normal file
38
day1/day1p2.nim
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue