2023 Day 4 Part 2
This commit is contained in:
parent
ca613a0315
commit
4a556998fd
1 changed files with 23 additions and 1 deletions
|
|
@ -28,8 +28,11 @@ proc parseCards(text: string): seq[Card] =
|
|||
for line in lines:
|
||||
result.add parseCard(line)
|
||||
|
||||
func matches(card: Card): int =
|
||||
len(card.winning * card.mine)
|
||||
|
||||
func score(card: Card): int =
|
||||
let matches = len(card.winning * card.mine)
|
||||
let matches = card.matches
|
||||
if matches > 0:
|
||||
2 ^ (matches - 1)
|
||||
else:
|
||||
|
|
@ -42,4 +45,23 @@ proc part1(): int =
|
|||
for card in cards:
|
||||
result.inc card.score
|
||||
|
||||
proc part2(): int =
|
||||
let text = readFile("input/day04.txt")
|
||||
let cards = parseCards(text)
|
||||
|
||||
var multiples: CountTable[int]
|
||||
|
||||
for card in cards:
|
||||
multiples.inc card.id
|
||||
|
||||
let extras = card.matches
|
||||
let scale = multiples[card.id]
|
||||
|
||||
for id in (card.id + 1) .. (card.id + extras):
|
||||
multiples.inc(id, scale)
|
||||
|
||||
for (id, count) in multiples.pairs:
|
||||
result += count
|
||||
|
||||
echo part1()
|
||||
echo part2()
|
||||
|
|
|
|||
Loading…
Reference in a new issue