2023-12-04 09:39:35 +01:00
|
|
|
import std/[cmdline, math, sequtils, setutils, strformat, strscans, strutils]
|
|
|
|
|
|
|
|
var result = 0
|
|
|
|
var fn = if paramCount() > 0: paramStr(1) else: "input_04.txt"
|
|
|
|
var lines = readFile(fn).strip.splitLines
|
|
|
|
|
2023-12-04 11:36:45 +01:00
|
|
|
# ASSUMPTION: lottery numbers are < 128
|
|
|
|
type
|
|
|
|
Card = ref object
|
|
|
|
play, pot: set[int8]
|
|
|
|
var cards: seq[Card]
|
|
|
|
|
|
|
|
proc points(card: Card): int =
|
|
|
|
let matches = (card.play * card.pot).len
|
|
|
|
if matches == 1:
|
|
|
|
result = 1
|
|
|
|
elif matches > 1:
|
|
|
|
result = 2 ^ (matches - 1)
|
|
|
|
else:
|
|
|
|
result = 0
|
|
|
|
|
|
|
|
|
2023-12-04 09:39:35 +01:00
|
|
|
for line in lines:
|
2023-12-04 11:36:45 +01:00
|
|
|
var cardno: int
|
2023-12-04 09:39:35 +01:00
|
|
|
let cardspec = line.split(":", 1)
|
|
|
|
|
2023-12-04 11:36:45 +01:00
|
|
|
if scanf(cardspec[0].strip, "Card$s$i", cardno):
|
|
|
|
let card = new Card
|
|
|
|
card.play = cardspec[1].split("|")[0].strip.splitWhitespace().mapIt(it.strip).mapIt(it.parseInt).mapIt(it.int8).toSet
|
|
|
|
card.pot = cardspec[1].split("|")[1].strip.splitWhitespace().mapIt(it.strip).mapIt(it.parseInt).mapIt(it.int8).toSet
|
|
|
|
echo &"Card {cardno}: {card.play}, {card.pot} => {card.points}"
|
|
|
|
cards.add(card)
|
|
|
|
result += card.points
|
|
|
|
|
|
|
|
echo &"Cards processed: {cards.len}"
|
2023-12-04 09:39:35 +01:00
|
|
|
echo &"Result: {result}"
|
|
|
|
|
|
|
|
case fn:
|
|
|
|
of "input_04.txt":
|
|
|
|
assert result == 18519
|
|
|
|
of "sample_input_04.txt":
|
|
|
|
assert result == 13
|