Skip to content

Commit ed49258

Browse files
committed
day12: reorder function defs
1 parent 93576a0 commit ed49258

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

day12/1.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ def edges(graph, coord):
88
return (n for n in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1))
99
if n in graph and graph[n] - graph[coord] <= 1)
1010

11+
def shortest_path(graph, start, end):
12+
to_explore = deque([(start, 0)])
13+
explored = set()
14+
while to_explore:
15+
here, steps = to_explore.popleft()
16+
if here == end:
17+
return steps
18+
for neighbor in edges(graph, here):
19+
if neighbor not in explored:
20+
explored.add(neighbor)
21+
to_explore.append((neighbor, steps + 1))
22+
23+
raise ValueError('No paths exist!')
24+
1125
def main():
1226
graph = {}
1327
start = None
@@ -26,19 +40,5 @@ def main():
2640
steps = shortest_path(graph, start, end)
2741
print(steps)
2842

29-
def shortest_path(graph, start, end):
30-
to_explore = deque([(start, 0)])
31-
explored = set()
32-
while to_explore:
33-
here, steps = to_explore.popleft()
34-
if here == end:
35-
return steps
36-
for neighbor in edges(graph, here):
37-
if neighbor not in explored:
38-
explored.add(neighbor)
39-
to_explore.append((neighbor, steps + 1))
40-
41-
raise ValueError('No paths exist!')
42-
4343
if __name__ == '__main__':
4444
main()

day12/2.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ def edges(graph, coord):
99
return (n for n in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1))
1010
if n in graph and graph[coord] - graph[n] <= 1)
1111

12+
def shortest_path_to_height(graph, start, height):
13+
to_explore = deque([(start, 0)])
14+
explored = set()
15+
while to_explore:
16+
here, steps = to_explore.popleft()
17+
if graph[here] == height:
18+
return steps
19+
for neighbor in edges(graph, here):
20+
if neighbor not in explored:
21+
explored.add(neighbor)
22+
to_explore.append((neighbor, steps + 1))
23+
24+
raise ValueError('No paths exist!')
25+
1226
def main():
1327
graph = {}
1428
end = None
@@ -25,19 +39,5 @@ def main():
2539
steps = shortest_path_to_height(graph, end, ord('a'))
2640
print(steps)
2741

28-
def shortest_path_to_height(graph, start, height):
29-
to_explore = deque([(start, 0)])
30-
explored = set()
31-
while to_explore:
32-
here, steps = to_explore.popleft()
33-
if graph[here] == height:
34-
return steps
35-
for neighbor in edges(graph, here):
36-
if neighbor not in explored:
37-
explored.add(neighbor)
38-
to_explore.append((neighbor, steps + 1))
39-
40-
raise ValueError('No paths exist!')
41-
4242
if __name__ == '__main__':
4343
main()

0 commit comments

Comments
 (0)