-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_6_part2.py
executable file
·51 lines (47 loc) · 1.12 KB
/
day_6_part2.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
class Planet:
def __init__(self, value):
self.value = value
self.parent = set()
self.children = set()
def parser(data):
info = {}
for i in data:
i = i[:-1].split(")")
if i[0] not in info:
info[i[0]] = Planet(i[0])
if i[1] not in info:
info[i[1]] = Planet(i[1])
if i[1] == "YOU":
YOU = info[i[0]]
if i[1] == "SAN":
SAN = info[i[0]]
info[i[0]].children.add(info[i[1]])
info[i[1]].parent.add(info[i[0]])
return (set(info.values()), YOU, SAN)
def search(data, YOU, SAN, last_step=set(), blacklist=set(), counter=1):
blacklist |= last_step
while True:
next_step = set()
for i in last_step:
if SAN in i.children:
break
next_step |= (i.parent | i.children) - blacklist
else:
last_step = next_step
blacklist |= next_step
counter += 1
continue
break
return counter
def dead_end(data):
blacklist = set()
for i in data:
if not i.parent:
blacklist.add(i)
for x in i.children:
if not x.children:
blacklist.add(x)
return blacklist
with open("input_6.txt") as file:
data, YOU, SAN = parser(file)
print(search(data, YOU, SAN, last_step={YOU}, blacklist=dead_end(data)))