-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
56 lines (39 loc) · 1.38 KB
/
day04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Advent of Code 2023 - Day 04."""
import re
import sys
from collections import deque
from typing import TextIO
Card = tuple[int, list[int], list[int]]
def parse(line: str) -> Card:
id, winners, mine = line.replace(":", "|").split("|")
return (
int(id.removeprefix("Card ")),
[int(n) for n in re.findall(r"\d+", winners)],
[int(n) for n in re.findall(r"\d+", mine)],
)
def part_one(file: TextIO) -> int:
def points(card: Card) -> int:
winners = list(filter(lambda n: n in card[1], card[2]))
return 0 if len(winners) == 0 else 1 << (len(winners) - 1)
return sum(map(points, map(parse, file)))
def part_two(file: TextIO) -> int:
cards = list(map(parse, file))
processed = []
queue = deque(cards)
while len(queue) > 0:
card = queue.popleft()
matching_cards = list(filter(lambda n: n in card[1], card[2]))
for i in range(len(matching_cards)):
idx = card[0] + i
if idx < len(cards):
queue.append(cards[idx])
processed.append(card[0])
return len(processed)
def main():
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()