diff --git a/config.nims b/config.nims index 438fdc9..f91d549 100644 --- a/config.nims +++ b/config.nims @@ -1,5 +1,6 @@ import std/[algorithm,os,sequtils] task solve, "run all solutions": - for dir in walkDir("solutions").toSeq().sortedByIt(it.path): - selfExec "r --hint:all:off " & dir.path & "/solution.nim" + for dir in walkDirRec("solutions", yieldFilter = {pcDir}).toSeq().sortedByIt(it): + echo "--",dir,"--" + selfExec "r --hint:all:off " & dir & "/solution.nim" diff --git a/solutions/aoc.nim b/solutions/aoc.nim index 7f0a977..b1de6d8 100644 --- a/solutions/aoc.nim +++ b/solutions/aoc.nim @@ -15,8 +15,6 @@ macro solve*(arg: untyped): untyped = for stmt in arg: stmt.expectKind nnkCall stmt[0].expectKind nnkIdent - if not (stmt[0].eqIdent("example") or stmt[0].eqIdent("input")): - error "Invalid input identifier: " & stmt[0].strVal stmt[1].expectKind nnkStmtList for inputs in stmt[1]: inputs.expectKind nnkCall @@ -28,9 +26,11 @@ macro solve*(arg: untyped): untyped = output = inputs[1][0] msg = newLit(part.repr & "|" & puzzleInput.repr) result.add quote do: - let color = - if `part`(`puzzleInput`) == `output`: fgGreen - else: fgRed - stdout.styledWriteLine(color, `msg`, fgDefault, ": ", $`output`) + let solution = `part`(`puzzleInput`) + if solution == `output`: + stdout.styledWriteLine(fgGreen, `msg`, fgDefault) + else: + stdout.styledWriteLine(fgRed, `msg`, fgDefault) + stdout.writeLine(" expected: ", $`output`, "; got: ", solution) loadInputs() diff --git a/solutions/day01/solution.nim b/solutions/day01/solution.nim index 0a5d0a1..f824c46 100644 --- a/solutions/day01/solution.nim +++ b/solutions/day01/solution.nim @@ -1,8 +1,6 @@ import std/[strutils] -const example* = slurp("example.txt").strip() -const example2* = slurp("example2.txt").strip() -const input* = slurp("input.txt").strip() +import ../aoc proc parseInput(input: string): seq[string] = input.split("\n") @@ -45,12 +43,23 @@ proc partTwo*(input: string): int = inc i result += parseInt(digits[0] & digits[^1]) -when isMainModule: - import std/unittest - suite "day 1": - test "part one": - check partOne(example) == 142 - check partOne(input) == 54597 - test "part two": - check partTwo(example2) == 281 - check partTwo(input) == 54504 +const example2 = slurp("example2.txt").strip() + +solve: + example: + partOne: 142 + example2: + partTwo: 281 + input: + partOne: 54597 + partTwo: 54504 + +# when isMainModule: +# import std/unittest +# suite "day 1": +# test "part one": +# check partOne(example) == 142 +# check partOne(input) == 54597 +# test "part two": +# check partTwo(example2) == 281 +# check partTwo(input) == 54504 diff --git a/solutions/day02/solution.nim b/solutions/day02/solution.nim index ee24e8a..77f306b 100644 --- a/solutions/day02/solution.nim +++ b/solutions/day02/solution.nim @@ -1,7 +1,6 @@ import std/[strutils, strscans] -const example* = slurp("example.txt").strip() -const input* = slurp("input.txt").strip() +import ../aoc type Color {.pure.} = enum @@ -47,13 +46,10 @@ proc partTwo*(input: string): int = mins[col] = max(mins[col], cnt) result += mins[red] * mins[green] * mins[blue] - -when isMainModule: - import std/unittest - suite "day 2": - test "part one": - check partOne(example) == 8 - check partOne(input) == 2239 - test "part two": - check partTwo(example) == 2286 - check partTwo(input) == 83435 +solve: + example: + partOne: 8 + partTwo: 2286 + input: + partOne: 2239 + partTwo: 83435 diff --git a/solutions/day03/solution.nim b/solutions/day03/solution.nim index e367723..39ba79e 100644 --- a/solutions/day03/solution.nim +++ b/solutions/day03/solution.nim @@ -1,7 +1,6 @@ import std/[strutils, sequtils, sets, tables] -const example* = slurp("example.txt").strip() -const input* = slurp("input.txt").strip() +import ../aoc type Pos = tuple[x, y: int] @@ -76,13 +75,10 @@ proc partTwo*(input: string): int = if parts.len == 2: result += parts[0].val * parts[1].val -when isMainModule: - import std/unittest - - suite "day 3": - test "part one": - check partOne(example) == 4361 - check partOne(input) == 536202 - test "part two": - check partTwo(example) == 467835 - check partTwo(input) == 78272573 +solve: + example: + partOne: 4361 + partTwo: 467835 + input: + partOne: 536202 + partTwo: 78272573 diff --git a/solutions/day04/solution.nim b/solutions/day04/solution.nim index 27b21ab..000ef1e 100644 --- a/solutions/day04/solution.nim +++ b/solutions/day04/solution.nim @@ -1,4 +1,4 @@ -import std/[math, sets, sequtils, strutils, macros] +import std/[math, sets, sequtils, strutils] import ../aoc