-
Notifications
You must be signed in to change notification settings - Fork 12
/
puzzle.py
247 lines (209 loc) · 6.99 KB
/
puzzle.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
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
import z3
from functools import reduce
from random import seed, randint, choice
import json
seed(69420)
f = open("prog.s", "w")
f.write("""; vim: ft=nasm
%include "vmasm.mac"
_start:
\tjump 0x000c
\texit 1
\texit 0
""")
text = "sice deet:\n"
def print_text(text):
sb = ""
for i in text:
if i == "\n":
sb += f"\tprint 0x000A\n"
else:
sb += f"\tprint '{i}'\n"
f.write(sb)
print_text(text)
with open("settings.json", "r") as jf:
js = json.load(jf)
printable = "".join(chr(a) for a in range(js["min"], js["max"]))
sz = len(printable)
opchoice = js["opchoice"]
opchoice_not_commute = js["opchoice_nc"]
opchoice_ndiv = js["opchoice_ndiv"]
size_choice = js["size_choice"]
def make_board(size):
row = []
for i in range(sz):
row.append(i+1)
board = []
for a in range(sz):
arow = row[:]
for k in range(a):
arow = [arow.pop()] + arow
board.append(arow)
for shuf in range(10000):
r1 = randint(0, sz - 1)
r2 = r1
while r2 == r1:
r2 = randint(0, sz - 1)
board[r1], board[r2] = board[r2], board[r1]
b2 = [a[:] for a in board[:]]
for i in range(sz):
board[i] = [b2[j][i] for j in range(sz)]
return board
board = make_board(sz)
for i in range(len(board)):
for j in range(len(board)):
board[i][j] = printable[board[i][j]-1]
get_size = lambda : choice(size_choice)
cages = []
for i in range(sz):
cages.append([])
for j in range(sz):
cages[-1].append(-1)
uq = 0
direcs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
from functools import reduce
from random import choice
i, j = 0, 0
boxes = []
while -1 in reduce(lambda a,b: a+b, cages):
for i in range(sz):
for j in range(sz):
if cages[i][j] == -1:
cages[i][j] = uq
pos1 = i
pos2 = j
adjs = [(a[0]+pos1, a[1]+pos2) for a in direcs if 0 <= a[0]+pos1 <= sz - 1 and 0 <= a[1]+pos2 <= sz - 1]
vals = []
for adj in adjs:
if cages[adj[0]][adj[1]] == -1:
vals.append(adj)
size = get_size()
if size == 2:
op = choice(opchoice_not_commute)
boxes.append([op, uq, [(pos1, pos2)]])
else:
op = choice(opchoice)
boxes.append([op, uq, [(pos1, pos2)]])
index = 1
while index < size and len(vals) != 0:
nbox = choice(vals)
pos1 = nbox[0]
pos2 = nbox[1]
cages[pos1][pos2] = uq
adjs = [(a[0]+pos1, a[1]+pos2) for a in direcs if 0 <= a[0]+pos1 <= sz - 1 and 0 <= a[1]+pos2 <= sz - 1]
vals = []
for adj in adjs:
if cages[adj[0]][adj[1]] == -1:
vals.append(adj)
boxes[-1][2].append((pos1, pos2))
index += 1
uq += 1
if len(boxes[-1][2]) == 2:
poss = boxes[-1][2]
largest = printable.index(board[poss[0][0]][poss[0][1]])+1
smallest = printable.index(board[poss[1][0]][poss[1][1]])+1
if smallest > largest:
smallest, largest = largest, smallest
if (largest % smallest == 0):
boxes[-1][0] = "/"
else:
boxes[-1][0] = choice(opchoice_ndiv)
with open("sol.txt", "w") as sol:
for r in board:
sol.write("".join(r))
exit1 = "\tjump 0x0004\n"
exit0 = "\tjump 0x0008\n"
diff = js["min"] - 1
for i in range(sz):
for j in range(sz):
f.write(f"\tload {diff}\n")
f.write("\tin\n")
f.write("\tsub\n")
f.write("\tsto\n")
f.write("\timp\n")
f.write(f"\tdms {sz**2}\n")
for i in range(sz):
for j in range(sz):
for r in range(j):
f.write(f"\treturn {i*sz + j}\n")
f.write(f"\treturn {i*sz + r}\n")
f.write(f"\tsub\n")
f.write("\tjiz 0x0004\n")
f.write(f"\treturn {j*sz + i}\n")
f.write(f"\treturn {r*sz + i}\n")
f.write(f"\tsub\n")
f.write("\tjiz 0x0004\n")
val = 0
for i in boxes:
if len(i[2]) == 1:
val = printable.index(board[i[2][0][0]][i[2][0][1]]) + 1
f.write(f"\tload {val}\n")
f.write(f"\treturn {i[2][0][0]*sz + i[2][0][1]}\n")
f.write(f"\tsub\n")
f.write(f"\tjnz 0x0004\n")
elif i[0] == "+":
val = sum([printable.index(board[a[0]][a[1]])+1 for a in i[2]])
f.write(f"\tload {val}\n")
for coord in i[2]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
for c in range(len(i[2])):
if c != 0:
f.write(f"\tadd\n")
f.write(f"\tsub\n")
f.write(f"\tjnz 0x0004\n")
elif i[0] == "*":
val = reduce(lambda a,b:a*b, [printable.index(board[a[0]][a[1]])+1 for a in i[2]])
f.write(f"\tload {val%32767}\n")
for coord in i[2]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
for c in range(len(i[2])):
if c != 0:
f.write(f"\tmmod 32767\n")
f.write(f"\tsub\n")
f.write(f"\tjnz 0x0004\n")
elif i[0] == "-" and len(i[2]) == 2:
val = abs(printable.index(board[i[2][0][0]][i[2][0][1]]) - printable.index(board[i[2][1][0]][i[2][1][1]]))
f.write(f"\tload {val}\n")
for coord in i[2]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
for c in range(len(i[2])):
if c != 0:
f.write(f"\tsub\n")
f.write(f"\tdup\n")
f.write(f"\tsig\n")
f.write(f"\tmul\n")
f.write(f"\tsub\n")
f.write(f"\tjnz 0x0004\n")
elif i[0] == "/" and len(i[2]) == 2:
a = printable.index(board[i[2][0][0]][i[2][0][1]])+1
b = printable.index(board[i[2][1][0]][i[2][1][1]])+1
if a % b == 0:
val = a // b
else:
val = b // a
f.write(f"\tload {val}\n")
for coord in i[2]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
f.write(f"\tmod\n")
for coord in i[2][::-1]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
f.write(f"\tmod\n")
f.write(f"\tmul\n")
f.write(f"\tjnz 0x0004\n")
for coord in i[2]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
f.write(f"\tneq\n")
f.write(f"\tload 1\n")
f.write(f"\tadd\n")
for coord in i[2]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
f.write(f"\tdiv\n")
for coord in i[2][::-1]:
f.write(f"\treturn {coord[0]*sz + coord[1]}\n")
f.write(f"\tdiv\n")
f.write(f"\tadd\n")
f.write(f"\tdiv\n")
f.write(f"\tsub\n")
f.write(f"\tjnz 0x0004\n")
f.write(f"\tjump 0x0008\n")
f.close()