mirror of
https://github.com/daylinmorgan/advent-of-code-2023.git
synced 2024-12-22 03:10:44 -06:00
standalone prettyprinter for solutions
This commit is contained in:
parent
e79043f336
commit
8255ac49d2
2 changed files with 45 additions and 12 deletions
36
solutions/aoc.nim
Normal file
36
solutions/aoc.nim
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import std/[strutils, os, macros, terminal]
|
||||||
|
|
||||||
|
template loadInputs*(): untyped =
|
||||||
|
const callPath = getProjectPath()
|
||||||
|
const example* {.inject.} = slurp(callPath / "example.txt").strip()
|
||||||
|
const input* {.inject.} = slurp(callPath / "input.txt").strip()
|
||||||
|
|
||||||
|
template solveInput*(input: string, p1: untyped, p2: untyped): untyped =
|
||||||
|
assert partOne(input) == p1
|
||||||
|
assert partTwo(input) == p2
|
||||||
|
|
||||||
|
macro solve*(arg: untyped): untyped =
|
||||||
|
arg.expectKind nnkStmtList
|
||||||
|
result = newStmtList()
|
||||||
|
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
|
||||||
|
inputs[0].expectKind nnkIdent
|
||||||
|
inputs[1].expectKind nnkStmtList
|
||||||
|
let
|
||||||
|
part = inputs[0]
|
||||||
|
puzzleInput = stmt[0]
|
||||||
|
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`)
|
||||||
|
|
||||||
|
loadInputs()
|
|
@ -1,7 +1,6 @@
|
||||||
import std/[math, sets, sequtils, strutils]
|
import std/[math, sets, sequtils, strutils, macros]
|
||||||
|
|
||||||
const example* = slurp("example.txt").strip()
|
import ../aoc
|
||||||
const input* = slurp("input.txt").strip()
|
|
||||||
|
|
||||||
type
|
type
|
||||||
Card = object
|
Card = object
|
||||||
|
@ -44,12 +43,10 @@ proc partTwo*(input: string): int =
|
||||||
return winners.sum()
|
return winners.sum()
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
solve:
|
||||||
import std/unittest
|
example:
|
||||||
suite "day 4":
|
partOne: 13
|
||||||
test "part one":
|
partTwo: 30
|
||||||
check partOne(example) == 13
|
input:
|
||||||
check partOne(input) == 22674
|
partOne: 22674
|
||||||
test "part two":
|
partTwo: 5747443
|
||||||
check partTwo(example) == 30
|
|
||||||
check partTwo(input) == 5747443
|
|
||||||
|
|
Loading…
Reference in a new issue