-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.py
62 lines (47 loc) · 1.6 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
# https://adventofcode.com/2020/day/7
from typing import Dict
def read_rules_to_graph(path) -> Dict[str, Dict[str, int]]:
with open(path) as f:
# split to key and values
rules = [line.strip('\n.').split(' bags contain ')
for line in f.readlines()]
# make dict, split values into lists
rules = {r[0]: [b.replace(' bags', '').replace(' bag', '').split(' ', 1)
for b in r[1].split(', ')]
for r in rules}
# make from values dicts with key(color):v(number)
return {c: {b[1]: int(b[0]) for b in v if b[0].isdigit()} for c, v in rules.items()}
def find_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
# if start not in graph:
# return None
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
if newpath:
return newpath
return None
def possible_outer_bags(graph, end):
res = []
for color in graph:
if color != end:
path = find_path(graph, color, end)
if path:
res.append(color)
return set(res)
def count_needed_bags(graph, start):
cnt = 0
for node, n in graph[start].items():
if n:
cnt += n + n * count_needed_bags(graph, node)
else:
return 1
return cnt
if __name__ == "__main__":
b_graph = read_rules_to_graph("res\\7.txt")
# Part one
print(len(possible_outer_bags(b_graph, 'shiny gold')))
# Part two
print(count_needed_bags(b_graph, 'shiny gold'))