-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
70 lines (55 loc) · 1.6 KB
/
12.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
INPUT_FILE = f"input/{__file__.split('.')[0].rstrip('b')}"
def getInput():
with open(INPUT_FILE, 'r') as f:
return f.read().splitlines()
def partOne(partTwo = False):
inp = getInput()
graph = {}
for line in inp:
line = line.split('-')
if line[0] in graph:
graph[line[0]].append(line[1])
else:
graph[line[0]] = [line[1]]
if line[1] in graph:
graph[line[1]].append(line[0])
else:
graph[line[1]] = [line[0]]
paths = 0
for node in graph['start']:
paths+= len(traverse(node, graph, ['start'], partTwo))
return paths
def traverse(start, graph, path, partTwo):
path = path + [start]
if start == 'end':
return [path]
paths = []
for node in graph[start]:
if node == 'start':
continue
if node.isupper() or (node.islower() and ((node not in path) or (partTwo and lowerCounts(path) == 0))):
for p in traverse(node, graph, path, partTwo):
paths.append(p)
return paths
def lowerCounts(path):
counts = {}
lowers = 0
for i in path:
if i.isupper():
continue
if i in counts:
counts[i] += 1
else:
counts[i] = 1
for k in counts:
if counts[k] > 1:
lowers += 1
return lowers
def pp(arr):
for i in arr:
print(i)
if __name__ == "__main__":
one = partOne(False)
two = partOne(True)
print(f"Part one: {one}")
print(f"Part two: {two}")