This repository was archived by the owner on Oct 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday07.py
More file actions
executable file
·65 lines (50 loc) · 1.47 KB
/
Copy pathday07.py
File metadata and controls
executable file
·65 lines (50 loc) · 1.47 KB
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
import re
def get_data(filename='input'):
with open(filename) as f:
text = f.read().strip()
lookup = {}
extractor = re.compile(r'[Ss]tep\s+(\w+)')
nodes = set()
for line in text.splitlines():
a, b = extractor.findall(line)
nodes.update((a, b))
lookup.setdefault(b, set()).add(a)
return sorted(nodes), lookup
def part_1(nodes, lookup):
result = ''
visited = set()
while len(visited) != len(nodes):
for node in nodes:
if node in visited:
continue
if not lookup.get(node, set()) - visited:
result += node
visited.add(node)
break
return result
def part_2(nodes, lookup):
workers = 5
seconds = -1
visited = set()
preparing = {}
while len(visited) != len(nodes):
for k in list(preparing.keys()):
preparing[k] -= 1
if preparing[k] == 0:
visited.add(k)
del preparing[k]
for node in nodes:
if node in visited or node in preparing:
continue
if len(preparing) == workers:
break
if not lookup.get(node, set()) - visited:
preparing[node] = 60 + ord(node) - 64
seconds += 1
return seconds
def main():
data = get_data()
print('1:', part_1(*data))
print('2:', part_2(*data))
if __name__ == '__main__':
main()