Compare commits

...

5 Commits

5 changed files with 71 additions and 1 deletions

View File

@ -10,6 +10,7 @@
To generate a new day and fetch the input's for a puzzle use the below command.
Note: the day and year are inferred from today's date if not specified.
Set advent of code session cookie to `AOC_COOKIE` environment variable.
```sh
nim r ./tools/aoc.nim # -d:1 -y:2023

5
config.nims Normal file
View File

@ -0,0 +1,5 @@
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"

View File

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

View File

@ -0,0 +1,59 @@
import std/[strutils, strscans]
const example* = slurp("example.txt").strip()
const input* = slurp("input.txt").strip()
type
Color {.pure.} = enum
red, blue, green
Cubes = array[Color, int]
Game = object
turns: seq[Cubes]
proc parseInput(input: string): seq[Game] =
for line in input.splitLines:
result.add Game()
let turns = line.split(":")[1]
for turn in turns.split(";"):
var cubes: Cubes
for cubeStr in turn.split(","):
var cnt: int
var color: string
discard cubeStr.strip().scanf("$i $w", cnt, color)
cubes[parseEnum[Color](color)] = cnt
result[^1].turns.add cubes
proc isValid(game: Game): bool =
const limits: Cubes = [red: 12, blue: 14, green: 13]
for turn in game.turns:
for col, cnt in turn:
if cnt > limits[col]:
return false
return true
proc partOne*(input: string): int =
var games = parseInput(input)
for i, game in games:
if game.isValid():
result += (i+1)
proc partTwo*(input: string): int =
var games = parseInput(input)
for i, game in games:
var mins: Cubes
for turn in game.turns:
for col, cnt in turn:
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

View File

@ -18,7 +18,7 @@ proc getInput(year, day: int): string =
return response.body().strip()
proc skeleton(day: int) =
const solution = """
let solution = fmt"""
import std/[strutils]
const example* = slurp("example.txt").strip()