Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First, with some help of Sim0000! solution in Clear category for Disposable Teleports by Thenbacker
from collections import defaultdict
from collections import deque
def make_graph(teleports_string):
teleports_list = teleports_string.split(',')
graph = defaultdict(list)
for i in teleports_list:
graph[i[0]].append(i[1])
graph[i[1]].append(i[0])
return graph
def checkio(teleports_string):
start ='1'
queue = deque([(start, start)])
graph = make_graph(teleports_string)
while queue:
path, current = queue.popleft()
# check complete or not; if all stations have been visited, return result
if current == start and len(set(path)) == 8: return ''.join([str(x) for x in path])
for neighbor in graph[current]:
# check current + neighbor list is already in path
if current + neighbor in path or neighbor + current in path: continue
queue.append((path + neighbor, neighbor))
Oct. 22, 2016
Comments: