Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Only count the number of sub-paths for part 2
This commit is contained in:
@@ -23,13 +23,12 @@ def process_node(node, available_edges, visited):
|
||||
|
||||
|
||||
def process_node_twice(node, available_edges, visited, visited_twice):
|
||||
paths = deque()
|
||||
num_paths = 0
|
||||
if node == "end":
|
||||
paths.append(deque(node))
|
||||
return paths
|
||||
return 1
|
||||
if node.islower():
|
||||
if node in visited and visited_twice:
|
||||
return paths
|
||||
return num_paths
|
||||
elif node in visited and node != "start":
|
||||
visited_twice = node
|
||||
else:
|
||||
@@ -37,15 +36,12 @@ def process_node_twice(node, available_edges, visited, visited_twice):
|
||||
neighbors = [n for e in available_edges if node in e for n in e if n != node]
|
||||
for n in neighbors:
|
||||
if n not in visited or visited_twice != node and n != "start":
|
||||
nxt = process_node_twice(n, available_edges, visited, visited_twice)
|
||||
for p in nxt:
|
||||
p.appendleft(node)
|
||||
paths += nxt
|
||||
num_paths += process_node_twice(n, available_edges, visited, visited_twice)
|
||||
if visited_twice == node:
|
||||
visited_twice = None
|
||||
elif node in visited:
|
||||
visited.remove(node)
|
||||
return paths
|
||||
return num_paths
|
||||
|
||||
|
||||
def part_1(input):
|
||||
@@ -67,8 +63,7 @@ def part_2(input):
|
||||
n_start, n_end = line.strip().split('-')
|
||||
e = (n_start, n_end)
|
||||
edges.append(e)
|
||||
paths = process_node_twice("start", edges, set(), None)
|
||||
result = len(paths)
|
||||
result = process_node_twice("start", edges, set(), None)
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user