adventofcode-2023/day_04/common.nim

33 lines
913 B
Nim
Raw Permalink Normal View History

import std/[math, sequtils, setutils, strformat, strmisc, strutils]
# ASSUMPTION: lottery numbers are < 128
type
Card* = ref object
idx*: int
play, pot: set[int8]
proc value*(card: Card): int =
(card.play * card.pot).len
proc points*(card: Card): int =
result = case card.value:
of 0: 0
of 1: 1
else: 2 ^ (card.value - 1)
proc `$`*(card: Card): string =
return &"Card {card.idx+1}: {card.play}, {card.pot} => {card.value}"
proc parseNumberSet(s: string): set[int8] =
s.strip.splitWhitespace().mapIt(it.parseInt.int8).toSet
proc parseInput*(s: string): seq[Card] =
let lines = s.strip.splitLines
for i, line in lines:
let card = new Card
let (left, _, right) = line.split(":")[1].partition("|")
card.idx = i
card.play = parseNumberSet(left)
card.pot = parseNumberSet(right)
result.add(card)