add day 1 solution

main
Christian Ulrich 2021-12-01 18:24:20 +01:00
parent 65124706d6
commit d2687a5602
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
4 changed files with 2056 additions and 0 deletions

2000
day01/input Normal file

File diff suppressed because it is too large Load Diff

21
day01/part1.nim Normal file
View File

@ -0,0 +1,21 @@
import strutils
proc countIncreases(): uint =
var
line = ""
prevDepth = high(uint)
while readLine(stdin, line):
if line.len() > 0:
let depth = parseUint(line)
if depth > prevDepth:
result.inc()
prevDepth = depth
proc main(): int =
try:
echo countIncreases()
except ValueError:
result = -1
when isMainModule:
quit(main())

25
day01/part2.nim Normal file
View File

@ -0,0 +1,25 @@
import algorithm
import math
import strutils
const WindowSize = 3
proc countIncreases(): uint =
var
line = ""
window: array[WindowSize + 1, uint]
while readLine(stdin, line):
if line.len() > 0:
window.rotateLeft(1)
window[^1] = parseUint(line)
if window[0] > 0 and sum(window[1 .. ^1]) > sum(window[0 .. ^2]):
result.inc()
proc main(): int =
try:
echo countIncreases()
except ValueError:
result = -1
when isMainModule:
quit(main())

10
day01/testinput Normal file
View File

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263