-
Notifications
You must be signed in to change notification settings - Fork 17
/
day07.py
executable file
·71 lines (52 loc) · 1.24 KB
/
day07.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2020, 7)
fin = advent.get_input()
# fin = io.StringIO('''\
# shiny gold bags contain 2 dark red bags.
# dark red bags contain 2 dark orange bags.
# dark orange bags contain 2 dark yellow bags.
# dark yellow bags contain 2 dark green bags.
# dark green bags contain 2 dark blue bags.
# dark blue bags contain 2 dark violet bags.
# dark violet bags contain no other bags.
# ''')
# eprint(*fin, sep=''); fin.seek(0, 0)
timer_start()
lines = get_lines(fin)
G = defaultdict(list)
for line in lines:
l, r = line.strip().split('bags contain')
l = l.strip()
r = r.strip()
for rr in r.split(', '):
rr = rr.replace('bags', '').replace('bag', '').replace('.', '').strip()
if rr.startswith('no'):
continue
else:
i = rr.find(' ')
n = int(rr[:i])
rr = rr[i+1:]
G[l].append((rr, n))
djk = dijkstra_lru(G)
ans = 0
seen = set()
for k in G:
if k == 'shiny gold':
continue
if k not in seen:
seen.add(k)
if djk(k, 'shiny gold') != INFINITY:
ans += 1
advent.print_answer(1, ans)
def xplore(src):
res = 0
if src in G:
for n, wg in G[src]:
res += wg
res += wg * xplore(n)
return res
# dump_dict(G)
ans2 = xplore('shiny gold')
# 32589 wrong
advent.print_answer(2, ans2)