diff --git a/challenges/2022/07-noSpaceLeftOnDevice/benchmark.json b/challenges/2022/07-noSpaceLeftOnDevice/benchmark.json index dc00547..4f3dc41 100644 --- a/challenges/2022/07-noSpaceLeftOnDevice/benchmark.json +++ b/challenges/2022/07-noSpaceLeftOnDevice/benchmark.json @@ -2,13 +2,21 @@ "day": 7, "dir": "challenges/2022/07-noSpaceLeftOnDevice", "implementations": { + "Nim": { + "part.1.avg": 0.005115992285999994, + "part.1.max": 0.020854849, + "part.1.min": 0.002606261, + "part.2.avg": 0.0051005765810000015, + "part.2.max": 0.018048126, + "part.2.min": 0.002556105 + }, "Python": { - "part.1.avg": 0.007820404291152954, - "part.1.max": 0.027557849884033203, - "part.1.min": 0.00545811653137207, - "part.2.avg": 0.00781301736831665, - "part.2.max": 0.030576467514038086, - "part.2.min": 0.005479097366333008 + "part.1.avg": 0.00848755979537964, + "part.1.max": 0.0318598747253418, + "part.1.min": 0.0057828426361083984, + "part.2.avg": 0.008544703245162964, + "part.2.max": 0.03149008750915527, + "part.2.min": 0.005964994430541992 } }, "numRuns": 1000 diff --git a/challenges/2022/07-noSpaceLeftOnDevice/nim/challenge.nim b/challenges/2022/07-noSpaceLeftOnDevice/nim/challenge.nim new file mode 100644 index 0000000..5b3cabd --- /dev/null +++ b/challenges/2022/07-noSpaceLeftOnDevice/nim/challenge.nim @@ -0,0 +1,101 @@ +import std/strutils +import std/tables + +type + Path = ref object + parts: seq[string] + +proc newPath(): Path = + new result + result.parts = newSeq[string]() + +proc cd(self: Path, arg: string) = + if arg == "..": + self.parts = self.parts[0..= amountToDelete: + opts.add(size) + + return min(opts) \ No newline at end of file diff --git a/challenges/2022/08-treetopTreeHouse/benchmark.json b/challenges/2022/08-treetopTreeHouse/benchmark.json index f93e98d..9e78360 100644 --- a/challenges/2022/08-treetopTreeHouse/benchmark.json +++ b/challenges/2022/08-treetopTreeHouse/benchmark.json @@ -2,14 +2,22 @@ "day": 8, "dir": "challenges/2022/08-treetopTreeHouse", "implementations": { + "Nim": { + "part.1.avg": 0.006382409829000001, + "part.1.max": 0.026628604, + "part.1.min": 0.004007148, + "part.2.avg": 0.007344551142000002, + "part.2.max": 0.034047325, + "part.2.min": 0.004595712 + }, "Python": { - "part.1.avg": 0.033502077102661135, - "part.1.max": 0.06080937385559082, - "part.1.min": 0.023911237716674805, - "part.2.avg": 0.04170952320098877, - "part.2.max": 0.0762169361114502, - "part.2.min": 0.02996683120727539 + "part.1.avg": 0.0385476336479187, + "part.1.max": 0.09098124504089355, + "part.1.min": 0.025599002838134766, + "part.2.avg": 0.04808542013168335, + "part.2.max": 0.1426246166229248, + "part.2.min": 0.031610965728759766 } }, - "numRuns": 500 + "numRuns": 1000 } \ No newline at end of file diff --git a/challenges/2022/08-treetopTreeHouse/nim/challenge.nim b/challenges/2022/08-treetopTreeHouse/nim/challenge.nim new file mode 100644 index 0000000..ed77ea1 --- /dev/null +++ b/challenges/2022/08-treetopTreeHouse/nim/challenge.nim @@ -0,0 +1,84 @@ +import std/tables +import std/strutils +import std/sequtils + +type + Coordinate = (int, int) + Forest = ref Table[Coordinate, int] + +proc parse(instr: string): (Forest, Coordinate) = + var forest = newTable[Coordinate, int]() + + let lines = instr.strip().splitlines() + + var + x = 0 + y = 0 + + while x < lines.len: + y = 0 + while y < lines[x].len: + forest[(x, y)] = parseInt($lines[x][y]) + y += 1 + x += 1 + + return (forest, (x, y)) + +proc areAdjacentsLower(forest: Forest, initialPos: Coordinate, nextPos: proc(pos: Coordinate): Coordinate): bool = + let height = forest[initialPos] + var pos = nextPos(initialPos) + while forest.contains(pos): + if forest[pos] >= height: + return false + pos = nextPos(pos) + return true + +proc getViewingDistance(forest: Forest, initialPos: Coordinate, nextPos: proc(pos: Coordinate): Coordinate): int = + let height = forest[initialPos] + var pos = nextPos(initialPos) + while forest.contains(pos): + result += 1 + if forest[pos] >= height: + break + pos = nextPos(pos) + +proc partOne*(instr: string): int = + let + (forest, maxPos) = parse(instr) + (maxX, maxY) = maxPos + + for treePos in forest.keys(): + let (x, y) = treePos + if x == 0 or y == 0 or x == maxX or y == maxY: + result += 1 + continue + + if areAdjacentsLower(forest, treePos, proc(c: Coordinate): Coordinate = (c[0] - 1, c[1])): + result += 1 + continue + + if areAdjacentsLower(forest, treePos, proc(c: Coordinate): Coordinate = (c[0] + 1, c[1])): + result += 1 + continue + + if areAdjacentsLower(forest, treePos, proc(c: Coordinate): Coordinate = (c[0], c[1] - 1)): + result += 1 + continue + + if areAdjacentsLower(forest, treePos, proc(c: Coordinate): Coordinate = (c[0], c[1] + 1)): + result += 1 + + +proc partTwo*(instr: string): int = + let (forest, _) = parse(instr) + + for treePos in forest.keys(): + let + view_left = getViewingDistance(forest, treePos, proc(c: Coordinate): Coordinate = (c[0] - 1, c[1])) + view_right = getViewingDistance(forest, treePos, proc(c: Coordinate): Coordinate = (c[0] + 1, c[1])) + view_up = getViewingDistance(forest, treePos, proc(c: Coordinate): Coordinate = (c[0], c[1] - 1)) + view_down = getViewingDistance(forest, treePos, proc(c: Coordinate): Coordinate = (c[0], c[1] + 1)) + + let senic = view_left * view_right * view_up * view_down + if senic > result: + result = senic diff --git a/challenges/2022/running-times.png b/challenges/2022/running-times.png index 75fa2b2..1d58202 100644 Binary files a/challenges/2022/running-times.png and b/challenges/2022/running-times.png differ