success with 4:)
[ar1516.git] / a2 / src / 4 / print4.py
diff --git a/a2/src/4/print4.py b/a2/src/4/print4.py
new file mode 100644 (file)
index 0000000..c0fcfdd
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/env python
+"""hi"""
+
+import sys
+import re
+
+PAT = r'\(= i(?P<i>\d+)x(?P<x>\d+)y(?P<y>\d+) (?P<v>true|false)\)'
+
+def print_board(data, tofile):
+    """Prints a peg solitaire board"""
+    for yco in range(8):
+        for xco in range(8):
+            pos = [d for d in data if d[0] == xco and d[1] == yco]
+            if not pos:
+                tofile.write(' ')
+            else:
+                tofile.write('.' if pos[0][2] else 'o')
+        tofile.write('\n')
+
+def parse(lines, output):
+    """Parse the model from yices"""
+    data = {}
+    for line in lines:
+        line = line.strip()
+        match = re.match(PAT, line)
+        if match:
+            ite = int(match.group('i'))
+            if ite not in data:
+                data[ite] = []
+            data[ite].append((int(match.group('x')), int(match.group('y')),
+                              match.group('v') == 'true'))
+    for _, dat in sorted(data.items()):
+        print_board(dat, output)
+
+
+if __name__ == '__main__':
+    if len(sys.argv) == 1:
+        parse(sys.stdin, sys.stdout)
+    elif len(sys.argv) == 2:
+        with open(sys.argv[1], 'r') as fin:
+            parse(fin, sys.stdout)
+    else:
+        with open(sys.argv[1], 'r') as fin, open(sys.argv[2], 'w') as fout:
+            parse(fin, fout)