2023 day02
This commit is contained in:
parent
4b49e6b86c
commit
24ed30fbe6
2 changed files with 83 additions and 0 deletions
65
2023/day02/d02_1.nim
Normal file
65
2023/day02/d02_1.nim
Normal file
|
@ -0,0 +1,65 @@
|
|||
import std/[math, sequtils, strutils, sugar]
|
||||
|
||||
type
|
||||
Rules = object
|
||||
red: int
|
||||
green: int
|
||||
blue: int
|
||||
|
||||
CubeSet* = object
|
||||
red*: int
|
||||
green*: int
|
||||
blue*: int
|
||||
|
||||
Game* = object
|
||||
id*: int
|
||||
sets*: seq[CubeSet]
|
||||
|
||||
const rules = Rules(red: 12, green: 13, blue: 14)
|
||||
|
||||
func isPossible(rules: Rules, cubes: CubeSet): bool =
|
||||
if cubes.red > rules.red:
|
||||
return false
|
||||
if cubes.green > rules.green:
|
||||
return false
|
||||
if cubes.blue > rules.blue:
|
||||
return false
|
||||
return true
|
||||
|
||||
func parseGame*(game: string): Game =
|
||||
let
|
||||
sp = game.split({':', ';'})
|
||||
sets = sp[1..^1]
|
||||
|
||||
result.id = sp[0].split(' ')[1].parseInt()
|
||||
|
||||
for set in sets:
|
||||
var cubes = CubeSet()
|
||||
for reveal in set.split(','):
|
||||
let
|
||||
sp = reveal.strip().split(' ')
|
||||
let
|
||||
amount = sp[0].strip().parseInt()
|
||||
colour = sp[1]
|
||||
case colour
|
||||
of "red":
|
||||
cubes.red = amount
|
||||
of "green":
|
||||
cubes.green = amount
|
||||
of "blue":
|
||||
cubes.blue = amount
|
||||
result.sets.add(cubes)
|
||||
|
||||
when isMainModule:
|
||||
let
|
||||
input = stdin.readAll().strip().splitLines()
|
||||
games = input.map(parseGame)
|
||||
possibleIds =
|
||||
collect:
|
||||
for game in games:
|
||||
block gameBlock:
|
||||
for set in game.sets:
|
||||
if not rules.isPossible(set):
|
||||
break gameBlock
|
||||
game.id
|
||||
echo possibleIds.sum()
|
18
2023/day02/d02_2.nim
Normal file
18
2023/day02/d02_2.nim
Normal file
|
@ -0,0 +1,18 @@
|
|||
import std/[math, sequtils, strutils, sugar]
|
||||
import d02_1
|
||||
|
||||
func fewestCubes(sets: seq[CubeSet]): CubeSet =
|
||||
for cubes in sets:
|
||||
if cubes.red > result.red:
|
||||
result.red = cubes.red
|
||||
if cubes.green > result.green:
|
||||
result.green = cubes.green
|
||||
if cubes.blue > result.blue:
|
||||
result.blue = cubes.blue
|
||||
|
||||
when isMainModule:
|
||||
let
|
||||
input = stdin.readAll().strip().splitLines()
|
||||
games = input.map(parseGame).mapIt(it.sets)
|
||||
fewest = games.map(fewestCubes)
|
||||
echo fewest.mapIt(it.red * it.green * it.blue).sum()
|
Loading…
Add table
Reference in a new issue