-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzlr.py
More file actions
221 lines (170 loc) · 5.27 KB
/
Copy pathPuzzlr.py
File metadata and controls
221 lines (170 loc) · 5.27 KB
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
import importlib
import sys
import pickle
import types
class SwapOperation:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def apply(self, state):
state[self.p1], state[self.p2] = state[self.p2], state[self.p1]
return state
def reverse(self, state):
state[self.p1], state[self.p2] = state[self.p2], state[self.p1]
return state
def serialise(self):
return {
'name': "SwapOperation",
'args': [self.p1, self.p2]
}
class CompoundOperation:
def __init__(self, op1, op2):
self.op1 = op1
self.op2 = op2
def apply(self, state):
return self.op2.apply(self.op1.apply(state))
def reverse(self, state):
return self.op2.reverse(self.op1.reverse(state))
def serialise(self):
return {
'name': 'CompoundOperation',
'args': [self.op1.serialise(), self.op2.serialise()],
}
class AddOperation:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def apply(self, state):
state[self.p1] += state[self.p2]
return state
def reverse(self, state):
state[self.p1] -= state[self.p2]
return state
def serialise(self):
return {
'name': "AddOperation",
'args': [self.p1, self.p2]
}
class SubOperation:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def apply(self, state):
state[self.p1] -= state[self.p2]
return state
def reverse(self, state):
state[self.p1] += state[self.p2]
return state
def serialise(self):
return {
'name': "SubOperation",
'args': [self.p1, self.p2]
}
class AddConstOperation:
def __init__(self, index, by):
self.index = index
self.by = by
def apply(self, state):
state[self.index] += self.by
return state
def reverse(self, state):
state[self.index] -= self.by
return state
def serialise(self):
return {
'name': "AddConstOperation",
'args': [self.index, self.by]
}
class SubConstOperation:
def __init__(self, index, by):
self.index = index
self.by = by
def apply(self, state):
state[self.index] -= self.by
return state
def reverse(self, state):
state[self.index] += self.by
return state
def serialise(self):
return {
'name': "SubConstOperation",
'args': [self.index, self.by]
}
def serialise_puzzle(puzzle):
obj = {
'state': puzzle.state,
'operations': []
}
for key in dir(puzzle):
if not key.startswith("_") and callable(getattr(puzzle, key)):
function = getattr(puzzle, key)
if function._direction == "op":
obj['operations'].append({
'operator': function._operator.serialise(),
'names': [key, function._invname]
})
return obj
def add_single_operation(puzzle, operation, name, direction):
def op(self):
if direction == "op":
self.state = operation.apply(self.state)
elif direction == "invop":
self.state = operation.reverse(self.state)
op.__name__ = name
op._operator = operation
op._direction = direction
setattr(puzzle, name, types.MethodType(op, self.puzzle))
def deserialise_operation(given_operation):
operations = [
SwapOperation,
AddOperation,
SubOperation,
AddConstOperation,
SubConstOperation,
CompoundOperation,
]
args = given_operation['args']
if given_operation['name'] == "CompoundOperation":
args = [deserialise_operation(x) for x in args]
for operation in operations:
if operation.__name__ == given_operation['name']:
return operation(*args)
def deserialise_puzzle(obj):
operations = [
SwapOperation,
AddOperation,
SubOperation,
AddConstOperation,
SubConstOperation,
CompoundOperation,
]
factory = PuzzleFactory()
for included_operation in obj['operations']:
for operation in operations:
if operation.__name__ == included_operation['operator']['name']:
factory.add_operation(deserialise_operation(included_operation['operator']), *included_operation['names'])
puzzle = factory.build()
puzzle.state = obj['state']
return puzzle
class PuzzleFactory:
def __init__(self):
self.puzzle = Puzzle()
def add_operation(self, operation, name, inv_name):
def op(self):
self.state = operation.apply(self.state)
op.__name__ = name
op._operator = operation
op._direction = "op"
op._invname = inv_name
def invop(self):
self.state = operation.reverse(self.state)
invop.__name__ = inv_name
invop._operator = operation
invop._direction = "invop"
setattr(self.puzzle, name, types.MethodType(op, self.puzzle))
setattr(self.puzzle, inv_name, types.MethodType(invop, self.puzzle))
def build(self):
return self.puzzle
class Puzzle:
def __init__(self):
self.state = [0, 0, 0, 0, 0]