Last active
August 29, 2015 14:06
-
-
Save marctrem/a8cd95c3d225882c7141 to your computer and use it in GitHub Desktop.
AS challenge from IEEE2013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import collections | |
def find_new_paths(graph, destination, current_paths, closed_paths): | |
new_paths = [] | |
for path in current_paths: | |
last_element = path[-1] | |
for next_element in graph[last_element]: | |
new_path = list(path) | |
new_path.append(next_element) | |
if next_element == destination: | |
closed_paths.append(new_path) | |
else: | |
new_paths.append(new_path) | |
return new_paths, closed_paths | |
nodes = collections.defaultdict(list) | |
destination = input().strip() | |
while True: | |
inp = input() | |
if inp == 'A A': | |
break | |
# Todo: check if the characters are between F and Z | |
nodes[inp[0]].append(inp[2]) | |
opened_paths = [['F']] | |
closed_paths = [] | |
for i in range(len(nodes)): | |
opened_paths, closed_paths = find_new_paths(nodes, destination, opened_paths, closed_paths) | |
if(closed_paths): | |
shortest = min([len(x) for x in closed_paths]) | |
shortest_paths_in_strings = sorted([' '.join(x) for x in closed_paths if len(x) == shortest]) | |
print('Total Routes: %s' % len(closed_paths) ) | |
print('Shortest Route Length: %s' % shortest) | |
print('Shortest Route after Sorting of Routes of length %s: %s' % (shortest, shortest_paths_in_strings[0])) | |
else: | |
print('No Route Available from F to %s' % destination) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment