apply new setup to other solutions

This commit is contained in:
Daylin Morgan 2023-12-04 17:41:45 -06:00
parent 8255ac49d2
commit dcb3951fe1
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
6 changed files with 47 additions and 45 deletions

View file

@ -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"

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,4 @@
import std/[math, sets, sequtils, strutils, macros]
import std/[math, sets, sequtils, strutils]
import ../aoc