-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch24.py
More file actions
181 lines (139 loc) · 4 KB
/
ch24.py
File metadata and controls
181 lines (139 loc) · 4 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import hashlib
import pickle
from zipfile import ZipFile
from PIL import Image as im
import sys
from StringIO import StringIO
from ch7 import chunks
import itertools
EMAIL_MD5 = 'bbb8b499a0eef99b52c7f13f4e78c24b'
GIF_MD5 = '6494c6fbca209100f0c956a666130c86'
ZIP_MD5 = 'bbf6616928e23ecfef4b717f281c53cc'
WALL = [(127, 127, 127, 255), (255,255,255,255)]
PATH = (0, 255, 0, 255) #green
FILENAME = './ch24_files/mybroken.zip'
def copyPath(path):
copy = []
for pix in path:
copy.append(pix)
return copy
def isClear(pix):
return (pix not in WALL)
def checkDirs(maze, startpix):
# first find the number of available paths
avail = [False, False, False, False] # top right bottom left
availPix = [False, False, False, False]
if startpix[0] > 0:
if isClear(maze[startpix[0]-1][startpix[1]]):
avail[0] = True
availPix[0] = (startpix[0]-1,startpix[1])
#check right
if startpix[1] < 640:
if isClear(maze[startpix[0]][startpix[1]+1]):
avail[1] = True
availPix[1] = (startpix[0],startpix[1]+1)
#check bottom
if startpix[0] < 640:
if isClear(maze[startpix[0]+1][startpix[1]]):
avail[2] = True
availPix[2] = (startpix[0]+1,startpix[1])
#check left
if startpix[1] > 0:
if isClear(maze[startpix[0]][startpix[1]-1]):
avail[3] = True
availPix[3] = (startpix[0],startpix[1]-1)
return avail, availPix
# returns true if it can find path to the end from start pix, false otherwise
def isPath(maze, path):
while True:
# print len(path)
startpix = path[-1]
# break condition
if startpix[0] == 640:
return True
#check dirs
avail, availPix = checkDirs(maze, startpix)
if len(path) > 1:
for i in xrange(4):
if avail[i]:
if availPix[i] == path[-2]: #if this pix was just traversed as part of the path
avail[i] = False
availPix[i] = False
if avail.count(True) == 4:
raise Exception('Invalid State')
if avail.count(True) == 0:
# dead end
return False
state = itertools.izip(avail, availPix)
if avail.count(True) == 1:
# only one path available
for available, availablePix in state:
if available:
path.append(availablePix)
elif avail.count(True) >= 2:
break
# 2 or 3 paths available
for available, availablePix in state:
if available:
path.append(availablePix)
newPath = copyPath(path)
if isPath(maze, newPath):
del path[:]
path.extend(newPath)
return True
else:
del path[len(path)-1]
return False
def solveBroken():
with open(FILENAME, 'rb') as f:
dat = f.read()
print len(dat)
dat = list(dat)
for i in xrange(len(dat)):
orig = dat[i]
print orig
for j in xrange(256):
dat[i] = chr(j)
temp = ''.join(dat)
if hashlib.md5(temp).hexdigest() == EMAIL_MD5:
with open(FILENAME, 'wb') as f:
f.write(temp)
print 'broke out at index:', i, 'using j:', j
return
dat[i] = orig
def solveMaze():
# sys.setrecursionlimit(50000)
maze = im.open('maze.png')
# print maze.mode
li = list(maze.getdata())
maze = chunks(li, 641)
# path = [(0,639)]
# print isPath(maze, path)
# print len(path)
# pickle.dump(path, open('maze-sol.dat', 'wb'))
path = pickle.load(open('maze-sol.dat', 'rb'))
# for pix in path:
# maze[pix[0]][pix[1]] = PATH
# new_img = list(itertools.chain(*maze))
# fim = im.new('RGBA',(641,641))
# fim.putdata(new_img)
# fim.show()
pathdata = []
for pix in path:
pathdata.append(maze[pix[0]][pix[1]][0])
# print len(pathdata)
# for dat in pathdata[:10:2]:
# if dat != (0,0,0,255):
# raise Exception('Assumption is wrong!')
#we can skip every odd element in pathdata
pathdata = pathdata[1::2]
dat = ''.join(chr(x) for x in pathdata)
print dat[:100]
print len(dat)
z = ZipFile(StringIO(dat))
print z.namelist()
z.extractall(path='./ch24_files/')
z.close()
if __name__ == '__main__':
# solveMaze()
solveBroken()