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
Use deque instead of list
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from collections import deque
|
||||
|
||||
|
||||
def process_node(node, available_edges, visited):
|
||||
paths = []
|
||||
paths = deque()
|
||||
if node == "end":
|
||||
paths.append([node])
|
||||
paths.append(deque(node))
|
||||
return paths
|
||||
if node.islower():
|
||||
visited.add(node)
|
||||
@@ -13,16 +14,18 @@ def process_node(node, available_edges, visited):
|
||||
for n in neighbors:
|
||||
if n not in visited:
|
||||
nxt = process_node(n, available_edges, visited)
|
||||
paths += [[node] + p for p in nxt]
|
||||
for p in nxt:
|
||||
p.appendleft(node)
|
||||
paths.append(p)
|
||||
if node in visited:
|
||||
visited.remove(node)
|
||||
return paths
|
||||
|
||||
|
||||
def process_node_twice(node, available_edges, visited, visited_twice):
|
||||
paths = []
|
||||
paths = deque()
|
||||
if node == "end":
|
||||
paths.append([node])
|
||||
paths.append(deque(node))
|
||||
return paths
|
||||
if node.islower():
|
||||
if node in visited and visited_twice:
|
||||
@@ -35,7 +38,9 @@ def process_node_twice(node, available_edges, visited, visited_twice):
|
||||
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)
|
||||
paths += [[node] + p for p in nxt]
|
||||
for p in nxt:
|
||||
p.appendleft(node)
|
||||
paths.append(p)
|
||||
if visited_twice == node:
|
||||
visited_twice = None
|
||||
elif node in visited:
|
||||
|
||||
Reference in New Issue
Block a user