-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzleParser.py
77 lines (64 loc) · 2.23 KB
/
puzzleParser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import re
import sys
class Parser():
def __init__(self, path):
self._path = path
self._flattenedPuzzle = []
self._shape = -1
def loadData(self):
try:
with open(self._path, "r") as f:
self._lines = f.readlines()
except FileNotFoundError:
sys.exit("Error: File not found.")
except OSError:
sys.exit("Error: OS Error.")
except Exception as e:
sys.exit(f"Error: Unexpected error {repr(e)}")
def printRawPuzzle(self):
for line in self._lines:
print(line, end="")
def removeComments(self):
commentPatten = re.compile("(#.*)|[\s]*[\n]")
newLines = []
for line in self._lines:
newLine = re.sub(commentPatten, '', line)
if newLine:
newLines.append(newLine.strip())
return newLines
def getShape(self, newLines):
self.shape = int(newLines[0].split()[0])
if self._shape <= 2:
sys.exit("Error: Shape cannot be lower than or equal to 2.")
@property
def shape(self):
return self._shape
@property
def flattenedPuzzle(self):
return self._flattenedPuzzle
@flattenedPuzzle.setter
def flattenedPuzzle(self, flattendPuzzle):
self._flattenedPuzzle = flattendPuzzle
@shape.setter
def shape(self, shape):
self._shape = shape
def flattenPuzzle(self, puzzleLines):
if len(puzzleLines) != self.shape:
sys.exit("Error: Wrong row shape.")
for line in puzzleLines:
try:
row = [int(x) for x in line.split()]
except ValueError:
sys.exit("Error: Non numeric characters in puzzle.")
if len(row) != self.shape:
sys.exit("Error: Wrong column shape.")
self.flattenedPuzzle += row
for i in range(pow(self.shape, 2)):
if i not in self.flattenedPuzzle:
sys.exit("Error: Wrong numbers in puzzle.")
def cleanPuzzle(self):
newLines = self.removeComments()
if len(newLines[0].split()) != 1:
sys.exit("Error: Wrong format.")
self.getShape(newLines)
self.flattenPuzzle(newLines[1:])