adventofcode-2023/day_04/solution_04b.nim

28 lines
615 B
Nim

import std/[cmdline, strformat]
import common
var fn = if paramCount() > 0: paramStr(1) else: "input_04.txt"
var cards = parseInput(readFile(fn))
var stack = cards
var result = 0
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