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

View file

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

View file

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

View file

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