Compare commits
5 Commits
db1d21df04
...
c977c6436f
Author | SHA1 | Date |
---|---|---|
Daylin Morgan | c977c6436f | |
Daylin Morgan | a82a0fd125 | |
Daylin Morgan | 60720d1001 | |
Daylin Morgan | 45cc5d84e9 | |
Daylin Morgan | 162b2655be |
|
@ -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
|
||||
|
|
|
@ -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"
|
|
@ -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
|
|
@ -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
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue