make it file based rather than implicit input loader

This commit is contained in:
Daylin Morgan 2023-12-05 11:32:47 -06:00
parent dcb3951fe1
commit 42e3c1d68a
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
5 changed files with 29 additions and 40 deletions

View file

@ -1,20 +1,21 @@
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 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
# 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
stmt[0].expectKind nnkStrLit
stmt[1].expectKind nnkStmtList
for inputs in stmt[1]:
inputs.expectKind nnkCall
@ -22,15 +23,16 @@ macro solve*(arg: untyped): untyped =
inputs[1].expectKind nnkStmtList
let
part = inputs[0]
puzzleInput = stmt[0]
inputFile = stmt[0]
output = inputs[1][0]
msg = newLit(part.repr & "|" & puzzleInput.repr)
msg = newLit(part.repr & "|" & inputFile.strVal)
result.add quote do:
let solution = `part`(`puzzleInput`)
block:
const input = slurp(getProjectPath() / `inputFile`).strip()
let solution = `part`(input)
if solution == `output`:
stdout.styledWriteLine(fgGreen, `msg`, fgDefault)
stdout.styledWriteLine fgGreen, `msg`
else:
stdout.styledWriteLine(fgRed, `msg`, fgDefault)
stdout.writeLine(" expected: ", $`output`, "; got: ", solution)
stdout.styledWriteLine fgRed, `msg`
stdout.writeLine " expected: ", $`output`, "; got: ", solution
loadInputs()

View file

@ -43,23 +43,11 @@ proc partTwo*(input: string): int =
inc i
result += parseInt(digits[0] & digits[^1])
const example2 = slurp("example2.txt").strip()
solve:
example:
"example.txt":
partOne: 142
example2:
"example2.txt":
partTwo: 281
input:
"input.txt":
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

View file

@ -47,9 +47,9 @@ proc partTwo*(input: string): int =
result += mins[red] * mins[green] * mins[blue]
solve:
example:
"example.txt":
partOne: 8
partTwo: 2286
input:
"input.txt":
partOne: 2239
partTwo: 83435

View file

@ -76,9 +76,9 @@ proc partTwo*(input: string): int =
result += parts[0].val * parts[1].val
solve:
example:
"example.txt":
partOne: 4361
partTwo: 467835
input:
"input.txt":
partOne: 536202
partTwo: 78272573

View file

@ -42,11 +42,10 @@ proc partTwo*(input: string): int =
winners[i+j].inc copies
return winners.sum()
solve:
example:
"example.txt":
partOne: 13
partTwo: 30
input:
"input.txt":
partOne: 22674
partTwo: 5747443