adventofcode-2023/day_02/solution_02.nim

52 lines
1.4 KiB
Nim
Raw Normal View History

import std/[strscans, strformat, strutils]
type
Reveal = tuple[red: int, green: int, blue: int]
Game = object
number: int
reveals: seq[Reveal]
var games: seq[Game]
for line in readFile("input_02.txt").splitLines:
if line.strip.len == 0:
continue
var gameNum: int
var gamespec = line.strip.split(":", 1)
if not scanf(gamespec[0], "Game $i", gameNum):
echo "invalid line. Game number prefix mising."
var game = Game(number: gameNum)
for revealspec in gamespec[1].split(";"):
var reveal: Reveal
for dice in revealspec.strip.split(","):
var spec = dice.strip.split(" ", 1)
case spec[1].strip:
of "red":
reveal.red = parseInt(spec[0])
of "green":
reveal.green = parseInt(spec[0])
of "blue":
reveal.blue = parseInt(spec[0])
game.reveals.add(reveal)
games.add(game)
var max_dice = (red: 12, green: 13, blue: 14)
var result = 0
for game in games:
var possible = true
for reveal in game.reveals:
if reveal.red > max_dice.red or reveal.green > max_dice.green or reveal.blue > max_dice.blue:
possible = false
echo &"Game {game.number} NOT possible"
break
if possible:
result += game.number
echo &"Result: {result}"