-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-wordsearch.py
executable file
·35 lines (32 loc) · 1.01 KB
/
decode-wordsearch.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
#!/usr/bin/python3
import re
import sys
def print_board(b):
return '\n'.join(''.join(row) for row in b)
def decode(rows, cols):
b = None
for line in sys.stdin:
line = line.rstrip()
if re.match('.*Solution:', line):
if b is not None: yield print_board(b)
b = [['_']*cols for row in range(rows)]
continue
m = re.match(' \\d+: (\\w+)', line)
if m is None: continue
word = m.groups()[0]
cs = [(int(x),int(y),z) for x,y,z in \
re.findall('\((\\d+),(\\d+)\):(\\w+)', line)]
for coord in cs:
b[coord[0]][coord[1]] = coord[2]
if b is not None: yield print_board(b)
if __name__ == '__main__':
try:
assert(len(sys.argv) == 3), "Wrong number of flags"
rows = int(sys.argv[1])
cols = int(sys.argv[2])
for ss in decode(rows, cols):
print(ss)
print()
except AssertionError:
print('Usage: {} rows cols'.format(sys.argv[0]))
raise