- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27.py
More file actions
Latest commit
91 lines (74 loc) · 2.38 KB
/
Copy path27.py
File metadata and controls
91 lines (74 loc) · 2.38 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
importdownload_file
fromPILimportImage
importbz2
importpy2keywords
defmain():
file_name="zigzag.gif"
url="http://www.pythonchallenge.com/pc/hex/{}".format(file_name)
file_path="zigzag/{}".format(file_name)
user="butter"
password="fly"
# first_step(url, file_path, user, password)
im, raw, result=second_step(file_path)
raw_ary, result_ary=third_step(raw, result)
raw_str, result, index=fourth_step(raw_ary, result_ary)
hint_path="zigzag/{}".format("hint.png")
five_step(im, index, hint_path)
six_step(raw_str)
deffirst_step(url, file_path, user, password):
download_file.download_with_auth(url, file_path, user, password)
defsecond_step(file_path):
im=Image.open(file_path)
raw=im.tobytes()
palette=im.getpalette()[::3]
index="".join([chr(i) foriinrange(len(palette))])
value="".join([chr(palette[i]) foriinrange(len(palette))])
table=str.maketrans(index, value)
result=raw.decode("latin1").translate(table).encode("latin1")
returnim, raw, result
defthird_step(raw, result):
raw_ary= []
result_ary= []
assertlen(raw) ==len(result)
l=len(raw)
foriinrange(l):
raw_ary.append(raw[i])
result_ary.append(result[i])
returnraw_ary, result_ary
deffourth_step(raw_ary, result_ary):
zero=raw_ary.pop(0)
raw_ary.append(zero)
assertlen(raw_ary) ==len(result_ary)
raw= []
result= []
index= []
foriinrange(len(raw_ary)):
raw_i=raw_ary[i]
result_i=result_ary[i]
ifraw_i!=result_i:
raw.append(raw_i)
result.append(result_i)
index.append(i)
raw_str=b""
foriinrange(len(raw)):
raw_str+=chr(raw[i]).encode("latin1")
returnraw_str, result, index
deffive_step(im, index, hint_path):
im2=Image.new("RGB", im.size)
colors= [(255, 255, 255)] * (im2.size[0] *im2.size[1])
foriinindex:
colors[i] = (0, 0, 255)
im2.putdata(colors)
im2.save(hint_path)
print("Hint in {}".format(hint_path))
defsix_step(raw_str):
raw_dec=bz2.decompress(raw_str)
strs=raw_dec.decode("latin1").split(" ")
results=set()
foriinrange(len(strs)):
s=strs[i]
ifnotsinpy2keywords.kwlist:
results.add(s)
print(results)
if__name__=="__main__":
main()