diff --git a/solutions/day02/example.txt b/solutions/day02/example.txt new file mode 100644 index 0000000..b49c10d --- /dev/null +++ b/solutions/day02/example.txt @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 diff --git a/solutions/day02/solution b/solutions/day02/solution new file mode 100755 index 0000000..4de0299 Binary files /dev/null and b/solutions/day02/solution differ diff --git a/solutions/day02/solution.nim b/solutions/day02/solution.nim new file mode 100644 index 0000000..5c2bd8f --- /dev/null +++ b/solutions/day02/solution.nim @@ -0,0 +1,64 @@ +import std/[strutils, algorithm, sequtils] +import aoc + +proc parseInput(input: string): seq[seq[int]] = + for line in input.splitLines(): + result.add line.splitWhitespace().mapIt(parseInt(it)) + +proc checkPair(x, y: int): bool = + if y < x: return false + let dist = abs(y - x) + if dist < 1 or dist > 3: return false + return true + +proc checkWindow(x, y, z: int): bool = + if not [x, y, z].isSorted: return false + let dists = [abs(z-y), abs(y-x)] + if dists.filterIt(it < 1 or it > 3).len != 0: return false + return true + +proc isSafe(report: seq[int]): bool = + var levels = report + # make all lists ascending order + if levels[0] > levels[1]: + levels.reverse() + for i in 0..