2023-12-04 12:42:23 +01:00
|
|
|
import std/[cmdline, strformat]
|
2023-12-04 11:37:05 +01:00
|
|
|
|
2023-12-04 12:42:23 +01:00
|
|
|
import common
|
2023-12-04 11:37:05 +01:00
|
|
|
|
2023-12-04 12:42:23 +01:00
|
|
|
var fn = if paramCount() > 0: paramStr(1) else: "input_04.txt"
|
|
|
|
var cards = parseInput(readFile(fn))
|
2023-12-04 11:37:05 +01:00
|
|
|
var stack = cards
|
2023-12-04 12:42:23 +01:00
|
|
|
var result = 0
|
|
|
|
|
2023-12-04 11:37:05 +01:00
|
|
|
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
|
|
|
|
|
2023-12-04 12:42:23 +01:00
|
|
|
echo &"Cards processed: {cards.len}"
|
2023-12-04 11:37:05 +01:00
|
|
|
echo &"Result: {result}"
|
|
|
|
|
|
|
|
case fn:
|
|
|
|
of "input_04.txt":
|
|
|
|
assert result == 11787590
|
|
|
|
of "sample_input_04.txt":
|
|
|
|
assert result == 30
|