-
Notifications
You must be signed in to change notification settings - Fork 1
/
leftturn.py
60 lines (41 loc) · 1.24 KB
/
leftturn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from collections import deque
def solve(maze):
path = deque([maze.start])
current = maze.start.Neighbours[2]
if current == None:
return path
heading = 2 # South
turn = 1 # Turning left, -1 for right
startpos = maze.start.Position
endpos = maze.end.Position
# N E S W - just a helpful reminder
# 0 1 2 3
count = 1
completed = False
while True:
path.append(current)
count += 1
position = current.Position
if position == startpos or position == endpos:
if position == endpos:
completed = True
break
n = current.Neighbours
if n[(heading - turn) % 4] != None:
heading = (heading - turn) % 4
current = n[heading]
continue
if n[heading] != None:
current = n[heading]
continue
if n[(heading + turn) % 4] != None:
heading = (heading + turn) % 4
current = n[heading]
continue
if n[(heading + 2) % 4] != None:
heading = (heading + 2) % 4
current = n[heading]
continue
completed = False
break
return [path, [count, len(path), completed]]