2020/Day 10/two.py

47 lines
975 B
Python
Raw Normal View History

2022-07-01 15:56:12 +02:00
#!/bin/python
import time
# Source node: [Destination nodes]
edges = {}
# Returns amount of paths
def find_all_paths(start, target):
#print(f"Checking from {start} to {target}")
if start == target:
return 1
total = 0
for node in edges[start]:
if node > target:
continue
total += find_all_paths(node, target)
return total
with open("input", 'r') as input:
adapters = [int(i.strip()) for i in input]
adapters.sort()
adapters.insert(0, 0)
adapters.append(adapters[-1]+3)
i = 0
while i < len(adapters)-1:
source = adapters[i]
edges[source] = []
j = i+1
while j < len(adapters)-1 and adapters[j]-source <= 3:
edges[source].append(adapters[j])
j += 1
i += 1
print(edges)
start_time = time.time()
print(find_all_paths(adapters[0], adapters[-1]))
print(f"Calculation time: {time.time() - start_time}")