-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
86 lines (76 loc) · 2.62 KB
/
day12.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
def part1():
with open('resources/input12.txt') as f:
lines = f.readlines()
connections = {}
for line in lines:
line = line.strip()
split = line.split('-')
if split[0] not in connections:
connections[split[0]] = list()
if split[1] not in connections:
connections[split[1]] = list()
connections.get(split[0]).append(split[1])
connections.get(split[1]).append(split[0])
chain = list()
chain.append("start")
print(find_path(connections["start"], connections, chain))
def find_path(node, connections, chain):
found = 0
for value in node:
new_chain = list()
new_chain.extend(chain)
if value == "end":
found += 1
elif value == "start":
continue
elif value.islower() and value in new_chain:
continue
else:
new_chain.append(value)
found += find_path(connections[value], connections, new_chain)
return found
def part2():
with open('resources/input12.txt') as f:
lines = f.readlines()
connections = {}
for line in lines:
line = line.strip()
split = line.split('-')
if split[0] not in connections:
connections[split[0]] = list()
if split[1] not in connections:
connections[split[1]] = list()
connections.get(split[0]).append(split[1])
connections.get(split[1]).append(split[0])
chain = list()
chain.append("start")
print(find_path2(connections["start"], connections, chain))
def find_path2(node, connections, chain):
found = 0
for value in node:
new_chain = list()
new_chain.extend(chain)
if value == "end":
found += 1
elif value == "start":
continue
elif value.islower():
duplicate_already_used = False
seen = set()
dupes = [x for x in new_chain if x in seen or seen.add(x)]
for entry in dupes:
if entry.islower():
duplicate_already_used = True
break
if duplicate_already_used and value in new_chain:
continue
else:
new_chain.append(value)
found += find_path2(connections[value], connections, new_chain)
else:
new_chain.append(value)
found += find_path2(connections[value], connections, new_chain)
return found
if __name__ == '__main__':
part1()
part2()