-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_input.py
executable file
·366 lines (347 loc) · 12.3 KB
/
utils_input.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python2
from utils_core import *
import sys
import urllib2
import importlib
''' A module taking input and solve Endview puzzles.
Ngoc Tran - 2016 || underlandian.com
'''
# JANKO SECTION
#___________
def janko_get_text(no = 0):
''' Get the problem from janko site.
Will cache into a folder if not existed.
'''
directory = 'janko_cache'
if directory[-1] != '/':
directory += '/'
if no == 0:
print 'Janko.at problem solver'
not_entered = True
while not_entered:
text = raw_input("Problem number? ")
try:
no = int(text)
if no > 530 or no < 1:
print 'Not a valid question number!'
else:
not_entered = False
except:
print 'Not a valid number!'
# test if file existed, i.e. cached
no = str(no).zfill(3)
directory += (no+'.txt')
try:
file = open(directory,'r')
log('file '+directory+' opened successfully.', LOG)
except:
bs4 = importlib.import_module('bs4')
BeautifulSoup = getattr(bs4, 'BeautifulSoup')
log('file '+directory+' does not exist, caching...', LOG)
url = 'http://www.janko.at/Raetsel/Abc-End-View/'+no+'.a.htm'
file = urllib2.urlopen(url)
html_doc = file.read()
soup = BeautifulSoup(html_doc, 'html.parser')
data = soup.data.get_text().split('\n')
# remove some unnecessary newlines
del data[0]
del data[-1]
# cache the received data
file = open(directory,'w')
for line in data[:-1]:
file.write(line.encode('ascii','ignore'))
file.write('\n')
file.write(data[-1])
file.close()
log('file '+directory+' saved successfully.', LOG)
else:
data = file.read().split('\n')
file.close()
return data
def janko_parser(data):
''' Janko parser - return the board and its constraints.
@return board, constraint, choices, diag
'''
choices = 0
diag = False
attributes = ['size','depth','options','clabels','rlabels','problem']
while True:
while len(data) > 0:
if len(data[0]) == 0 or data[0].split()[0] not in attributes:
del data[0]
else:
break
if len(data) == 0:
break
if data[0].split()[0] == 'size':
dim = int(data[0].split()[1])
constraint = generate_empty_constraint(dim)
attributes.remove('size')
if data[0].split()[0] == 'depth':
depth = int(data[0].split()[1])
choices = ''
for i in range(depth):
choices += chr(ord('A') + i)
if depth < dim:
choices += 'X'
attributes.remove('depth')
if data[0].split()[0] == 'options':
attributes.remove('options')
diag = True
# top/bottom
if data[0].split()[0] == 'clabels':
attributes.remove('clabels')
del data[0]
topConstraint = data[0].upper().split()
for i in range(dim):
if topConstraint[i] != '-':
constraint[0][i] = omni_to_letter(topConstraint[i])
del data[0]
del topConstraint
#__________
bottomConstraint = data[0].upper().split()
for i in range(dim):
if bottomConstraint[i] != '-':
constraint[1][i] = omni_to_letter(bottomConstraint[i])
del data[0]
del bottomConstraint
#__________
# left/right
if data[0].split()[0] == 'rlabels':
attributes.remove('rlabels')
del data[0]
leftConstraint = data[0].upper().split()
for i in range(dim):
if leftConstraint[i] != '-':
constraint[2][i] = omni_to_letter(leftConstraint[i])
del data[0]
del leftConstraint
#__________
rightConstraint = data[0].upper().split()
for i in range(dim):
if rightConstraint[i] != '-':
constraint[3][i] = omni_to_letter(rightConstraint[i])
del data[0]
del rightConstraint
# problem lmao
board = generate_empty_board(dim)
populate_empty_board(board, choices)
if data[0].split()[0] == 'problem':
attributes.remove('problem')
del data[0]
for i in range(dim):
clues = data[0].upper().split()
del data[0]
for j in range(dim):
if clues[j] != '-':
board[i][j] = omni_to_letter(clues[j])
return board, constraint, choices, diag
# INPUT SECTION
#___________
def not_cap_chars(word):
''' Check if all are capital and sequential from A. '''
if len(word) == 0:
raise ValueError('no choices entered')
for i in range(len(word)):
if ord(word[i]) != ord('A')+i:
return True
return False
def omni_to_letter(c):
''' Convert number to letter.
Some problems on Janko has numbers instead of letters in the source
code, confusing the solver.
'''
try:
i = int(c)
return chr(ord('A') - 1 + i)
except:
# not an int means its a normal character
return c
def input_gui():
''' GUI to input a problem. '''
print 'ABC ENDVIEW SOLVER'
print
not_entered = True
while not_entered:
text = raw_input("Dimension (n x n)? ")
try:
dim = int(text)
not_entered = False
if dim < 2:
raise ValueError
except ValueError:
print "Value error!", "Dimension has to be an integer greater than 1!"
constraint = generate_empty_constraint(dim)
#______________
not_entered = True
while not_entered:
choices = raw_input("Characters to fill in, with no delimiters: ") \
.upper()
if not_cap_chars(choices):
print "String error!", "Only sequencial letters are allowed."
elif len(choices) > dim:
print "String error!", "Number of choices must be less than " + \
"or equal to " + str(dim) + "."
else:
not_entered = False
#______________
not_entered = True
while not_entered:
constraint_sub = raw_input("Insert column-beginning constraints " + \
"- Continuously, '-' for none: ").upper()
if len(constraint_sub) != dim:
print "Constraints error!", \
"Number of constraints must be exactly "+str(dim)+"."
else:
legit = True
while legit:
for i in range(dim):
if constraint_sub[i] in choices:
constraint[0][i] = constraint_sub[i]
elif constraint_sub[i] != '-':
legit = False
print "String error!", constraint_sub[i] + \
" is not a valid entry!"
break
if i == dim-1:
break
if legit:
not_entered = False
#______________
not_entered = True
while not_entered:
constraint_sub = raw_input("Insert column-ending constraints " + \
"- Continuously, '-' for none: ").upper()
if len(constraint_sub) != dim:
print "Constraints error!", \
"Number of constraints must be exactly "+str(dim)+"."
else:
legit = True
while legit:
for i in range(dim):
if constraint_sub[i] in choices:
constraint[1][i] = constraint_sub[i]
elif constraint_sub[i] != '-':
legit = False
print "String error!", constraint_sub[i] + \
" is not a valid entry!"
break
if i == dim-1:
break
if legit:
not_entered = False
#______________
not_entered = True
while not_entered:
constraint_sub = raw_input("Insert row-beginning constraints - " + \
"Continuously, '-' for none: ").upper()
if len(constraint_sub) != dim:
print "Constraints error!", \
"Number of constraints must be exactly " + str(dim) + "."
else:
legit = True
while legit:
for i in range(dim):
if constraint_sub[i] in choices:
constraint[2][i] = constraint_sub[i]
elif constraint_sub[i] != '-':
legit = False
print "String error!", constraint_sub[i] + \
" is not a valid entry!"
break
if i == dim-1:
break
if legit:
not_entered = False
#______________
not_entered = True
while not_entered:
constraint_sub = raw_input("Insert row-ending constraints - " + \
"Continuously, '-' for none: ").upper()
if len(constraint_sub) != dim:
print "Constraints error!", \
"Number of constraints must be exactly "+str(dim)+"."
else:
legit = True
while legit:
for i in range(dim):
if constraint_sub[i] in choices:
constraint[3][i] = constraint_sub[i]
elif constraint_sub[i] != '-':
legit = False
print "String error!", constraint_sub[i]+\
" is not a valid entry!"
break
if i == dim-1:
break
if legit:
not_entered = False
#______________
not_entered = True
while not_entered:
not_entered = False
result = raw_input("Are diagonals required to have all characters? ") \
.upper()
if result in ['YES','Y','1']:
diag = True
elif result in ['NO','N','0']:
diag = False
else:
print 'Not a valid answer!'
not_entered = True
# start initializing
if dim > len(choices):
choices += 'X'
#______________
not_entered = True
while not_entered:
not_entered = False
result = raw_input("Are there prefilled boxes? ").upper()
if result in ['YES','Y','1']:
partial = True
elif result in ['NO','N','0']:
partial = False
else:
print 'Not a valid answer!'
not_entered = True
#______________
board = generate_empty_board(dim)
populate_empty_board(board, choices)
if partial:
print "Input coordinates and corresponding value, " + \
"blankspace-separated; blank to end."
while True:
result = raw_input().upper()
if result == '':
break
else:
try:
clues = result.split()
if len(clues) != 3:
raise ValueError
i = int(clues[0])-1
j = int(clues[1])-1
if len(clues[2])!= 1 or clues[2] == 'X' or \
clues[2] not in choices:
raise ValueError
if i<0 or j<0 or i>=dim or j>=dim:
raise ValueError
board[i][j] = clues[2]
except:
print 'Not a valid input! Ignored last input.'
print 'All input taken!'
return board, constraint, choices, diag
def solve_main(no = -1):
''' Pretty printing the output of a solver.
-1 triggers the input prompt, else get redirected to janko getter.
'''
# log('\nABC Endview Solver', GUI)
if no == -1:
board, constraint, choices, diag = input_gui()
else:
board, constraint, choices, diag = janko_parser(janko_get_text(no))
solve_print(board, constraint, choices, diag)
# EXECUTION SECTION
#__________
if __name__ == '__main__':
solve_main(int(sys.argv[1]))