-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle.py
119 lines (94 loc) · 2.18 KB
/
puzzle.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
import pprint
class Puzzle(object):
"""docstring for Puzzle"""
def __init__(self, data):
super(Puzzle, self).__init__()
self.comment = data[0]
if "unsolvable" in self.comment:
raise Exception(self.comment)
self.size = int(data[1])
self.puzzle = self.getPuzzle(data)
self.list = self.getPuzzleLst(self.puzzle)
self.snail = self.checkSnail()
self.solution = self.getSolution()
def __repr__(self):
return "%s\nPuzzle(%d, %s)" % (self.comment, self.size, self.list)
def getPuzzleLst(self, data):
lst = []
for line in data:
for coln in line:
lst.append(coln)
return lst
def getPuzzle(self, data):
puzzle = []
for x in xrange(2, len(data)):
line = data[x].split(' ')
tab = []
for number in line:
if number != '':
tab.append(int(number))
puzzle.append(tab)
return puzzle
def getSolution(self):
solucList = sorted(self.list, key=int)
solucList.pop(0)
solucList.append(0)
firstTurn = True
x = 0
y = 0
diff = 0
index = self.size - 1
list = self.puzzle
while index > 0:
index = index - diff
if firstTurn == True:
list[x][y] = solucList.pop(0)
firstTurn = False
while y < index:
y += 1
list[x][y] = solucList.pop(0)
while x < index:
x += 1
list[x][y] = solucList.pop(0)
index = index - diff
while y > diff:
y -= 1
list[x][y] = solucList.pop(0)
diff += 1
while x > diff:
x -= 1
list[x][y] = solucList.pop(0)
res = []
for line in list:
for coln in line:
res.append(coln)
return res
def addNumber(self, list, number):
if number not in list:
list.append(number)
return list
def checkSnail(self):
x = 0
y = 0
diff = 0
index = self.size - 1
list = []
while index > 0:
index = index - diff
list = self.addNumber(list, self.puzzle[x][y])
while y < index:
y += 1
list = self.addNumber(list, self.puzzle[x][y])
while x < index:
x += 1
list = self.addNumber(list, self.puzzle[x][y])
index = index - diff
while y > diff:
y -= 1
list = self.addNumber(list, self.puzzle[x][y])
diff += 1
while x > diff:
x -= 1
list = self.addNumber(list, self.puzzle[x][y])
return list