-
Notifications
You must be signed in to change notification settings - Fork 2
/
floorparse.py
62 lines (46 loc) · 1.52 KB
/
floorparse.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
#!/usr/bin/env python3
from collections import defaultdict
class FloorParser:
def __init__(self):
pass
def parse(self, floor):
'''
parses a txt floor
'''
grid = []
for row in floor.split('\n'):
if not row: continue
sqs = row.split(';')
rowattrs = [set(sq.strip().split(',')) for sq in sqs]
print(' '.join([list(row)[0] for row in rowattrs]))
grid += [rowattrs]
graph = defaultdict(lambda: {'nbrs': set()})
R, C = len(grid), len(grid[0])
for i in range(R):
for j in range(C):
attrs = grid[i][j]
graph[(i,j)].update({att:int(att in attrs) for att in 'WSBFNP'})
for off in {-1, 1}:
if 0 <= i+off < R:
graph[(i,j)]['nbrs'].add((i+off, j))
if 0 <= j+off < C:
graph[(i,j)]['nbrs'].add((i, j+off))
self.graph = dict(graph.items())
return self.graph
def tostr(self, graph):
'''
'''
r, c = 0, 0
for loc, attrs in graph.items():
r = max(r, loc[0])
c = max(c, loc[1])
r, c = r+1, c+1
s = ''
for r_ in range(r):
for c_ in range(c):
sq = graph[(r_, c_)]
# this =
att = ','.join([a for a in sq if a in 'BNSFWP' and sq[a]])
s += '{:>4}'.format(att)
s += '\n'
return s