-
Notifications
You must be signed in to change notification settings - Fork 1
/
rushhour
executable file
·290 lines (224 loc) · 8.63 KB
/
rushhour
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""Rush Hour Solver, by Jared Corduan"""
from functools import reduce
from itertools import chain
from string import ascii_lowercase
import argparse
HORIZONTAL = 0
VERTICAL = 1
def upward(point):
"""Move a point upward (decrease the y-value)."""
return (point[0], point[1]-1)
def downward(point):
"""Move a point downward (increase the y-value)."""
return (point[0], point[1]+1)
def right(point):
"""Move a point upward (decrease the x-value)."""
return (point[0]+1, point[1])
def left(point):
"""Move a point downward (increase the x-value)."""
return (point[0]-1, point[1])
def make_pts(forward, length, xval, yval):
"""Create a tuple of points using foldl."""
return tuple(reduce(
lambda points, _: points+[forward(points[-1])],
range(length-1),
[(xval, yval)]))
def make_horiz_pts(length, xval, yval):
"""Create a tuple continuous horizontal points."""
return make_pts(right, length, xval, yval)
def make_hcar(name, xval, yval):
"""Create the data for a horizontal car."""
return name, HORIZONTAL, make_horiz_pts(2, xval, yval)
def make_hbus(name, xval, yval):
"""Create the data for a horizontal bus."""
return name, HORIZONTAL, make_horiz_pts(3, xval, yval)
def make_vert_pts(length, xval, yval):
"""Create a tuple continuous vertical points."""
return make_pts(downward, length, xval, yval)
def make_vcar(name, xval, yval):
"""Create the data for a vertical car."""
return name, VERTICAL, make_vert_pts(2, xval, yval)
def make_vbus(name, xval, yval):
"""Create the data for a vertical bus."""
return name, VERTICAL, make_vert_pts(3, xval, yval)
def is_vacant(point, occupied):
"""Returns true if the given point is unoccupied and valid."""
return point not in occupied and 1 <= point[0] <= 6 and 1 <= point[1] <= 6
def get_moves(auto, point, descr, next_pt, occupied):
"""
Generator to yield all new positions a given auto in a given
direction can move to.
:param auto: the automobile data
:param point: the point of the auto in the direction of the movement
:param descr: a format string for a description of the move
:param next_pt: a function to return the next point
:param occupied: a list of all occupied points on the game board
:yields: new positions and descriptions for all valid moves
"""
name, axis, points = auto
spaces = 1
point = next_pt(point)
while is_vacant(point, occupied):
points = tuple(map(next_pt, points))
yield descr.format(spaces), (name, axis, points)
spaces += 1
point = next_pt(point)
def move_auto(old_pos, new_pos, board):
"""Return a new game board with one automobile in a new position."""
new_board = set(board)
new_board.remove(old_pos)
new_board.add(new_pos)
return new_board
def check_moves(auto, occupied, board):
"""
Generator to yield all game boards for valid moves of a given auto
in a given direction.
:param auto: the automobile data
:param occupied: a list of all occupied points on the game board
:param board: the game board
:yields: game boards and descriptions for all valid moves
"""
_, axis, points = auto
if axis == HORIZONTAL:
forward, f_descr = right, 'right {}'
backward, b_descr = left, 'left {}'
else:
forward, f_descr = downward, 'down {}'
backward, b_descr = upward, 'up {}'
forward_moves = get_moves(auto, points[-1], f_descr, forward, occupied)
backward_moves = get_moves(auto, points[0], b_descr, backward, occupied)
for move_descr, new_pos in chain(forward_moves, backward_moves):
yield move_descr, move_auto(auto, new_pos, board)
def explore(board):
"""
Generator to yield all game boards within one move of one auto.
:param auto: the automobile data
:param occupied: a list of all occupied points on the game board
:param board: the game board
:yields: game boards and descriptions for all valid moves
"""
occupied = [point for _, _, auto in board for point in auto]
for auto in board:
for descr, new_board in check_moves(auto, occupied, board):
move_descr = '{} -> {}'.format(auto[0], descr)
yield move_descr, new_board
def success(board, player_car):
"""Returns true if the game board is solved."""
for auto in board:
name, _, points = auto
if name == player_car and (6, 3) in points:
return True
return False
def make_solution(idx, history):
"""Returns a list of the solution instructions."""
solution = []
while idx != 0:
prev = history[idx]
solution.append(prev['descr'])
idx = prev['parent']
return solution[::-1]
def solve(start, player_car):
"""Solves a Rush Hour puzzle."""
explored = [start]
explore_start = 0
current_idx = 0
history = {}
while True:
explore_end = len(explored)
start = explore_start
if explore_start == explore_end:
return []
explore_start = explore_end
for idx, leaf in enumerate(explored[start:explore_end]):
for descr, board in explore(leaf):
if board not in explored:
explored.append(board)
current_idx += 1
history[current_idx] = {
'parent': start+idx,
'descr': descr
}
if success(board, player_car):
return make_solution(current_idx, history)
return None
def validate_board(start, player_car):
"""Validates that a Rush Hour puzzle is valid."""
points = [point for _, _, auto in start for point in auto]
if len(points) != len(set(points)):
return 'Invalid game board, autos are overlapping'
for xval, yval in points:
if xval < 0 or xval > 6 or yval < 0 or yval > 6:
return 'Invalid game board, autos are off the game board'
names = [auto[0] for auto in start]
if len(names) != len(set(names)):
return 'auto names must be unique'
def _has_player_car(auto):
return auto[0] == player_car
player_car = [auto for auto in start if _has_player_car(auto)]
if not player_car:
return 'missing the player (red) car'
if not player_car[0][1] == HORIZONTAL:
return 'player car must be horizontal'
if not player_car[0][2][0][1] == 3:
return 'player car must be on row 3'
def display_board(board, player_car):
"""Pretty prints the game board."""
tiles = ['.'] * 6
for i in range(6):
tiles[i] = ['.'] * 6
for char_tile, (name, _, points) in zip(ascii_lowercase, board):
for point in points:
tile = 'R' if name == player_car else char_tile
tiles[point[1]-1][point[0]-1] = tile
print('\n'.join([''.join(row) for row in tiles]))
def create_parser():
"""Create the command line argument parser."""
auto_factories = {
'hcar': make_hcar,
'hbus': make_hbus,
'vcar': make_vcar,
'vbus': make_vbus,
}
def parse_auto_arg(arg):
"""Parse and validate auto information from the command line."""
try:
name, model, xval, yval = arg.split(',')
xval, yval = int(xval), int(yval)
auto_maker = auto_factories[model.lower()]
return auto_maker(name, xval, yval)
except Exception:
msg = 'format is name,model,xval,yval but got {} (see readme)'
raise argparse.ArgumentTypeError(msg.format(arg))
parser = argparse.ArgumentParser()
auto_msg = 'auto description of the form name,model,x,y (see readme.md)'
parser.add_argument(
'-p', '--player-car', default='red',
help='name of the player car, default is red')
parser.add_argument(
'-d', '--display', action='store_true',
help='set this to display the puzzle instead of solving it')
parser.add_argument('autos', nargs='*', type=parse_auto_arg, help=auto_msg)
return parser
def main():
"""
Creates a game board from the command line arguments
and either solves the puzzle or displays it.
"""
parser = create_parser()
args = parser.parse_args()
start = set([auto for auto in args.autos])
error = validate_board(start, args.player_car)
if error:
parser.error(error)
if args.display:
display_board(start, args.player_car)
else:
solution = solve(start, args.player_car)
if solution:
for step in solution:
print(step)
else:
print('No solution exists.')
if __name__ == '__main__':
main()