Compare commits

...

2 Commits

Author SHA1 Message Date
Daylin Morgan dcb3951fe1
apply new setup to other solutions 2023-12-04 17:41:45 -06:00
Daylin Morgan 8255ac49d2
standalone prettyprinter for solutions 2023-12-04 17:27:41 -06:00
6 changed files with 84 additions and 49 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"

36
solutions/aoc.nim Normal file
View 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
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 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,7 +1,6 @@
import std/[math, sets, sequtils, strutils]
const example* = slurp("example.txt").strip()
const input* = slurp("input.txt").strip()
import ../aoc
type
Card = object
@ -44,12 +43,10 @@ proc partTwo*(input: string): int =
return winners.sum()
when isMainModule:
import std/unittest
suite "day 4":
test "part one":
check partOne(example) == 13
check partOne(input) == 22674
test "part two":
check partTwo(example) == 30
check partTwo(input) == 5747443
solve:
example:
partOne: 13
partTwo: 30
input:
partOne: 22674
partTwo: 5747443