-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd2yass.py
More file actions
86 lines (69 loc) · 2.63 KB
/
std2yass.py
File metadata and controls
86 lines (69 loc) · 2.63 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# yass: Yet Another Soma Solver
# Copyright (C) 2021 Mark R. Rubin aka "thanks4opensource"
#
# This file is part of yass.
#
# The yass program is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# The yass program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# (LICENSE.txt) along with the yass program. If not, see
# <https:#www.gnu.org/licenses/gpl.html>
#!/usr/bin/env python3
import argparse
import sys
def std2mrr(infile, outfile):
height = None
width = None
depth = 0
for line in infile:
if not line or ';' in line or '/SOMA' in line:
continue
layers = line.strip()[1:].split('/')
if height:
if len(layers) != height:
raise ValueError( "Mismatched heights: %d vs %d"
% (len(layers), height) )
else:
height = len(layers)
mrr = [[] for _ in range(height)]
if width:
if len(layers[0]) != width:
raise ValueError( "Mismatched widths: %d vs %d"
% (len(layers), width) )
else:
width = len(layers[0])
for (z, layer) in enumerate(layers):
piece_empty = ''.join(['.'
if std in '.-0' else 'o'
for std in layer ])
mrr[z].append(piece_empty)
depth += 1
# outfile.write("%d %d %d * -\n" % (width, depth, height))
for layer in mrr[:-1]:
outfile.write('\n'.join(layer) + '\n\n')
outfile.write('\n'.join(mrr[-1]) + '\n')
def parse_commandline():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('infile',
nargs='?',
type=argparse.FileType('r'),
default=sys.stdin)
parser.add_argument('outfile',
nargs='?',
type=argparse.FileType('w'),
default=sys.stdout)
return parser.parse_args()
if __name__ == "__main__":
args = parse_commandline()
std2mrr(args.infile, args.outfile)
args. infile.close()
args.outfile.close()