Compare commits
No commits in common. "aab15cdced617be8bb4d7d0c67fb750d29e3d3a4" and "0f7658a8e0c0a5f68a9de6a0fce8f0bf516e91e7" have entirely different histories.
aab15cdced
...
0f7658a8e0
|
@ -4,35 +4,19 @@ var result = 0
|
||||||
var fn = if paramCount() > 0: paramStr(1) else: "input_04.txt"
|
var fn = if paramCount() > 0: paramStr(1) else: "input_04.txt"
|
||||||
var lines = readFile(fn).strip.splitLines
|
var lines = readFile(fn).strip.splitLines
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
var cardno: int
|
var card: int
|
||||||
let cardspec = line.split(":", 1)
|
let cardspec = line.split(":", 1)
|
||||||
|
if scanf(cardspec[0].strip, "Card$s$i", card):
|
||||||
|
let play = cardspec[1].split("|")[0].strip.splitWhitespace().mapIt(it.strip).mapIt(it.parseInt).mapIt(it.int8).toSet
|
||||||
|
let jackpot = cardspec[1].split("|")[1].strip.splitWhitespace().mapIt(it.strip).mapIt(it.parseInt).mapIt(it.int8).toSet
|
||||||
|
#echo &"Card {card}: {left}, {right} => {left * right}"
|
||||||
|
let wins = (play * jackpot).len
|
||||||
|
if wins == 1:
|
||||||
|
result += 1
|
||||||
|
elif wins > 1:
|
||||||
|
result += 2 ^ (wins - 1)
|
||||||
|
|
||||||
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}"
|
|
||||||
echo &"Result: {result}"
|
echo &"Result: {result}"
|
||||||
|
|
||||||
case fn:
|
case fn:
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
import std/[cmdline, 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
|
|
||||||
|
|
||||||
# ASSUMPTION: lottery numbers are < 128
|
|
||||||
type
|
|
||||||
Card = ref object
|
|
||||||
idx: int
|
|
||||||
play, pot: set[int8]
|
|
||||||
var cards: seq[Card]
|
|
||||||
|
|
||||||
proc value(card: Card): int =
|
|
||||||
(card.play * card.pot).len
|
|
||||||
|
|
||||||
#proc `$`(card: Card): string =
|
|
||||||
# return &"Card idx={card.idx}: value={card.value}"
|
|
||||||
|
|
||||||
|
|
||||||
for i, line in lines:
|
|
||||||
var cardno: int
|
|
||||||
let cardspec = line.split(":", 1)
|
|
||||||
|
|
||||||
if scanf(cardspec[0].strip, "Card$s$i", cardno):
|
|
||||||
let card = new Card
|
|
||||||
card.idx = i
|
|
||||||
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 {card.idx}: {card.play}, {card.pot} => {card.value}"
|
|
||||||
cards.add(card)
|
|
||||||
|
|
||||||
var stack = cards
|
|
||||||
while stack.len > 0:
|
|
||||||
result += len(stack)
|
|
||||||
var extra_cards: seq[Card]
|
|
||||||
|
|
||||||
for card in stack:
|
|
||||||
if card.value > 0:
|
|
||||||
extra_cards.add(cards[card.idx + 1..min(card.idx + card.value, cards.len - 1)])
|
|
||||||
|
|
||||||
stack = extra_cards
|
|
||||||
|
|
||||||
#echo &"Cards processed: {cards.len}"
|
|
||||||
echo &"Result: {result}"
|
|
||||||
|
|
||||||
case fn:
|
|
||||||
of "input_04.txt":
|
|
||||||
assert result == 11787590
|
|
||||||
of "sample_input_04.txt":
|
|
||||||
assert result == 30
|
|
Loading…
Reference in New Issue