update
[ar1516.git] / src / a2.py
1 #!/usr/bin/env python3
2 import sys
3 import re
4
5 comps = {}
6
7 for line in sys.stdin:
8 match = re.match('\(= c(?P<c>\d+)(?P<t>.) (?P<v>\d+)\)', line)
9 if match:
10 comps[match.group('c')] = comps.get(match.group('c'), {})
11 comps[match.group('c')][match.group('t')] = int(match.group('v'))
12
13 maxx = max(c['x']+c['w'] for c in comps.values())
14 maxy = max(c['y']+c['h'] for c in comps.values())
15 print('$\\begin{{array}}{{|l|{}|}}'.format('@{}c@{}'*maxx))
16 print('\t\\toprule')
17 print('\t & {}\\\\'.format(
18 ' & '.join(map(str, range(1, maxx+1)))))
19 print('\t\\midrule')
20 for y in range(1, maxy+1):
21 print('\t{} & '.format(y), end='')
22 for x in range(1, maxx+1):
23 cs = [c for c in comps if
24 comps[c]['y'] + comps[c]['h'] >= y and comps[c]['y'] <= y and
25 comps[c]['x'] + comps[c]['w'] >= x and comps[c]['x'] <= x]
26 if cs:
27 print('\\hspace{{.5mm}}{}\\hspace{{.5mm}}'.format(cs[0]), end='')
28 if x != maxx:
29 print(' & ', end='')
30 print('\\\\')
31
32 print('\t\\bottomrule')
33 print('\\end{array}$')