-
Notifications
You must be signed in to change notification settings - Fork 0
/
cracker_barrel.py
207 lines (174 loc) · 6.07 KB
/
cracker_barrel.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
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
# -*-coding:utf8;-*-
# qpy:3
# qpy:console
import argparse
from collections import namedtuple, Counter
from copy import deepcopy
from numbers import Number
from typing import Iterable
import sys
Move = namedtuple('Move', 'row column direction')
class Direction:
def __init__(self, x: int, y: int, name=''):
self.x = x
self.y = y
self.name = name
def __mul__(self, rhs: Number) -> 'Direction':
return Direction(self.x * rhs, self.y * rhs)
def __str__(self):
return self.name or '<' + __name__ + '(x: ' + str(self.x) + ', y: ' + str(self.y) + ')>'
def __repr__(self):
return str(self)
class Puzzle:
directions = tuple(
Direction(x, y, name)
for (x, y, name) in [
(0, -1, 'up'),
(0, 1, 'down'),
(-1, 0, 'left'),
(1, 0, 'right'),
(1, -1, 'up_right'),
(-1, 1, 'down_left'),
(0, 0, 'zero'),
]
)
up, down, left, right, up_right, down_left, zero = directions
directions = directions[:-1]
starting_puzzles = {}
_all_moves = []
def __init__(self, board: Iterable[Iterable[bool]] = None, moves: Iterable[Move] = None):
self.board = board or [
[True, True, True, True, True],
[True, True, True, True],
[True, False, True],
[True, True],
[True]]
if moves is None:
if not Puzzle._all_moves:
Puzzle._all_moves = self.all_moves()
self.moves = Puzzle._all_moves
else:
self.moves = moves
self.solution = []
def solve(self, memory={}) -> bool:
if self.is_solved():
return True
elif self.board_hash() in memory:
return False
for move in self.moves:
puzzle = Puzzle(deepcopy(self.board), self.moves)
if puzzle.hop(move) and puzzle.solve():
self.solution = [move] + puzzle.solution
return True
memory[self.board_hash()] = True
return False
def is_solved(self) -> bool:
left = 0
for row in self.board:
for column in row:
left += column
if left == 1:
return True
return False
def hop(self, move: Move) -> bool:
if not (self.get(move, move.direction) and self.get(move, move.direction * 2) and
not self.get(move, Puzzle.zero)):
return False
self.set(move, Puzzle.zero, True)
self.set(move, move.direction, False)
self.set(move, move.direction * 2, False)
return True
def all_moves(self):
res = []
for direction in Puzzle.directions:
for row in range(5):
for column in range(5 - row):
move = Move(row, column, direction)
try:
self.get(move, move.direction * 2)
res.append(move)
except IndexError:
pass
return res
def __str__(self):
rows = []
for row in self.board:
rows.append(' '.join(str(int(val)) for val in row))
return '\n'.join(rows)
def __repr__(self):
return self.__str__()
def get(self, move: Move, direction: Direction) -> bool:
if move.row + direction.y < 0 or move.column + direction.x < 0:
raise IndexError
return self.board[move.row + direction.y][move.column + direction.x]
def set(self, move: Move, direction: Direction, val: bool):
if move.row + direction.y < 0 or move.column + direction.x < 0:
raise IndexError
self.board[move.row + direction.y][move.column + direction.x] = val
def board_hash(self) -> int:
res = 0
i = 0
for row in self.board:
for val in row:
res += val << i
i += 1
return res
@staticmethod
def print_all_starting_puzzles():
for puzzle_name, puzzle in Puzzle.starting_puzzles.items():
print(puzzle_name, ':\n', puzzle, sep='')
class _PuzzleStaticMemberInitialization:
Puzzle.starting_puzzles['tip'] = Puzzle([
[False, True, True, True, True],
[True, True, True, True],
[True, True, True],
[True, True],
[True]])
Puzzle.starting_puzzles['middle'] = Puzzle([
[True, True, True, True, True],
[True, False, True, True],
[True, True, True],
[True, True],
[True]])
Puzzle.starting_puzzles['middle-edge'] = Puzzle([
[True, True, False, True, True],
[True, True, True, True],
[True, True, True],
[True, True],
[True]])
Puzzle.starting_puzzles['corner-edge'] = Puzzle([
[True, False, True, True, True],
[True, True, True, True],
[True, True, True],
[True, True],
[True]])
def main(puzzle):
display_puzzle = deepcopy(puzzle)
if puzzle.solve():
print('solution:')
print(display_puzzle)
for move in puzzle.solution:
display_puzzle.hop(move)
print(move, display_puzzle, sep='\n')
else:
print('no solution found ):')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Solve the Cracker Barrel Peg game',
epilog='Valid values of puzzle_name and the corresponding puzzle image follow:\n' +
'\n'.join((
puzzle_name + ':\n' + str(puzzle)
for puzzle_name, puzzle in
Puzzle.starting_puzzles.items())),
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'puzzle_name',
type=str,
choices=list(Puzzle.starting_puzzles.keys()) + ['all'])
puzzle_name = parser.parse_args().puzzle_name
if puzzle_name == 'all':
for puzzle_name, puzzle in Puzzle.starting_puzzles.items():
print('SOLVING ', puzzle_name, ':', sep='')
main(deepcopy(puzzle))
else:
main(deepcopy(Puzzle.starting_puzzles[puzzle_name]))