adventofcode-2023/day_04/common.nim

34 lines
931 B
Nim

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)
echo card
result.add(card)