-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.py
85 lines (79 loc) · 3.68 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def cos(direction):
"""naive implementation of cos"""
return int(abs(2 - direction) - 1)
def sin(direction):
"""naive implementation of cos"""
return int(1 - abs(direction - 1))
def print_observed_map(sweeper):
min_y = min(sweeper.observed_map)
min_dict_x = min(sweeper.observed_map, key=lambda x: min(sweeper.observed_map[x]))
min_x = min(sweeper.observed_map[min_dict_x])
max_y = max(sweeper.observed_map)
max_dict_x = max(sweeper.observed_map, key=lambda x: max(sweeper.observed_map[x]))
max_x = max(sweeper.observed_map[max_dict_x])
for i in range(min_y, max_y + 1):
text = ""
for j in range(min_x, max_x + 1):
item = sweeper.observed_map[i].get(j, None)
if sweeper.current_position['x'] == j \
and sweeper.current_position['y'] == i:
text += 'o'
elif not item:
text += ' '
elif item == 1:
text += '*'
else:
text += '|'
print(text)
print('')
def bfs(start_position, start_direction, finish_check_fn, adjacent_check_fn, spiral):
# this is just simple BFS implementation
checked = {}
queue = []
queue.append({'x': start_position['x'], 'y': start_position['y'], 'direction': None, 'parent': None})
while queue:
current = queue.pop(0)
if current['direction'] is not None:
start_direction = current['direction']
finished = finish_check_fn(current)
if finished:
path = []
while current['parent']:
path.append(current['direction'])
current = current['parent']
return path
for node in adjacent(current, start_direction, spiral):
key = str(node['x']) + '_' + str(node['y'])
if not checked.get(key, None) \
and adjacent_check_fn(node):
checked[key] = 1
queue.append(node)
def adjacent(current, start_direction, spiral):
if spiral:
if start_direction == 0:
return [
{'x': current['x'], 'y': current['y'] - 1, 'direction': 1, 'parent': current},
{'x': current['x'] + 1, 'y': current['y'], 'direction': 0, 'parent': current},
{'x': current['x'], 'y': current['y'] + 1, 'direction': 3, 'parent': current},
{'x': current['x'] - 1, 'y': current['y'], 'direction': 2, 'parent': current}
]
if start_direction == 1:
return [
{'x': current['x'] - 1, 'y': current['y'], 'direction': 2, 'parent': current},
{'x': current['x'], 'y': current['y'] - 1, 'direction': 1, 'parent': current},
{'x': current['x'] + 1, 'y': current['y'], 'direction': 0, 'parent': current},
{'x': current['x'], 'y': current['y'] + 1, 'direction': 3, 'parent': current}
]
if start_direction == 2:
return [
{'x': current['x'], 'y': current['y'] + 1, 'direction': 3, 'parent': current},
{'x': current['x'] - 1, 'y': current['y'], 'direction': 2, 'parent': current},
{'x': current['x'], 'y': current['y'] - 1, 'direction': 1, 'parent': current},
{'x': current['x'] + 1, 'y': current['y'], 'direction': 0, 'parent': current}
]
return [
{'x': current['x'] + 1, 'y': current['y'], 'direction': 0, 'parent': current},
{'x': current['x'], 'y': current['y'] + 1, 'direction': 3, 'parent': current},
{'x': current['x'] - 1, 'y': current['y'], 'direction': 2, 'parent': current},
{'x': current['x'], 'y': current['y'] - 1, 'direction': 1, 'parent': current}
]