-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.py
94 lines (68 loc) · 2.48 KB
/
7.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from collections import deque
from pathlib import Path
import re
def read():
path = Path(__file__).parent / "input7.txt"
rows = []
with open(path) as f:
for line in f.readlines():
line = line.strip()
rows.append(line)
return rows
def p1(rows):
alist = {}
pattern_container = re.compile(r"([a-zA-Z]+ [a-zA-Z]+) bags?")
pattern_contained = re.compile(r" ([a-zA-Z]+ [a-zA-Z]+) bags?")
for row in rows:
match = re.match(pattern_container, row)
container_colour = match.group(1)
for match in re.finditer(pattern_contained, row):
contained_colour = match.group(1)
if contained_colour not in alist:
alist[contained_colour] = []
# notice the key is contained_colour, which is different from p2 where we use container_colour as key
alist[contained_colour].append(container_colour)
unique_colours = set()
queue = deque()
for colour in alist["shiny gold"]:
queue.append(colour)
unique_colours.add(colour)
while queue:
current = queue.popleft()
if current in alist:
for colour in alist[current]:
queue.append(colour)
unique_colours.add(colour)
return len(unique_colours)
def p2(rows):
alist = {}
pattern_container = re.compile(r"([a-zA-Z]+ [a-zA-Z]+) bags?")
pattern_contained = re.compile(r"([0-9]+) ([a-zA-Z]+ [a-zA-Z]+) bags?")
for row in rows:
match = re.match(pattern_container, row)
container_colour = match.group(1)
if container_colour not in alist:
alist[container_colour] = []
for match in re.finditer(pattern_contained, row):
contained_count = match.group(1)
contained_colour = match.group(2)
alist[container_colour].append(
[int(contained_count), contained_colour])
queue = deque()
for key_value in alist["shiny gold"]:
queue.append(key_value)
count = 0
while queue:
current_container_count, current_container_colour = queue.popleft()
count += current_container_count
for next_container_count, next_container_colour in alist[current_container_colour]:
queue.append(
(next_container_count * current_container_count, next_container_colour)
)
return count
def main():
input = read()
print(p1(input))
print(p2(input))
if __name__ == "__main__":
main()