success with 4:)
[ar1516.git] / a2 / src / 4 / print4.py
1 #!/bin/env python
2 """hi"""
3
4 import sys
5 import re
6
7 PAT = r'\(= i(?P<i>\d+)x(?P<x>\d+)y(?P<y>\d+) (?P<v>true|false)\)'
8
9 def print_board(data, tofile):
10 """Prints a peg solitaire board"""
11 for yco in range(8):
12 for xco in range(8):
13 pos = [d for d in data if d[0] == xco and d[1] == yco]
14 if not pos:
15 tofile.write(' ')
16 else:
17 tofile.write('.' if pos[0][2] else 'o')
18 tofile.write('\n')
19
20 def parse(lines, output):
21 """Parse the model from yices"""
22 data = {}
23 for line in lines:
24 line = line.strip()
25 match = re.match(PAT, line)
26 if match:
27 ite = int(match.group('i'))
28 if ite not in data:
29 data[ite] = []
30 data[ite].append((int(match.group('x')), int(match.group('y')),
31 match.group('v') == 'true'))
32 for _, dat in sorted(data.items()):
33 print_board(dat, output)
34
35
36 if __name__ == '__main__':
37 if len(sys.argv) == 1:
38 parse(sys.stdin, sys.stdout)
39 elif len(sys.argv) == 2:
40 with open(sys.argv[1], 'r') as fin:
41 parse(fin, sys.stdout)
42 else:
43 with open(sys.argv[1], 'r') as fin, open(sys.argv[2], 'w') as fout:
44 parse(fin, fout)