mirror of
https://github.com/daylinmorgan/advent-of-code-2023.git
synced 2024-12-21 11:00:44 -06:00
make it file based rather than implicit input loader
This commit is contained in:
parent
dcb3951fe1
commit
42e3c1d68a
5 changed files with 29 additions and 40 deletions
|
@ -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`)
|
||||
if solution == `output`:
|
||||
stdout.styledWriteLine(fgGreen, `msg`, fgDefault)
|
||||
else:
|
||||
stdout.styledWriteLine(fgRed, `msg`, fgDefault)
|
||||
stdout.writeLine(" expected: ", $`output`, "; got: ", solution)
|
||||
block:
|
||||
const input = slurp(getProjectPath() / `inputFile`).strip()
|
||||
let solution = `part`(input)
|
||||
if solution == `output`:
|
||||
stdout.styledWriteLine fgGreen, `msg`
|
||||
else:
|
||||
stdout.styledWriteLine fgRed, `msg`
|
||||
stdout.writeLine " expected: ", $`output`, "; got: ", solution
|
||||
|
||||
loadInputs()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue