-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel_24.py
More file actions
82 lines (68 loc) · 2.31 KB
/
level_24.py
File metadata and controls
82 lines (68 loc) · 2.31 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
#!/bin/env python
# coding=utf-8
# http://butter:fly@www.pythonchallenge.com/pc/hex/ambiguity.html
# Find a path in the maze.
import zipfile
from io import BytesIO
import networkx as nx
import requests
from PIL import Image
PREFIX = "http://butter:fly@www.pythonchallenge.com/pc/hex/"
url = PREFIX + 'maze.png'
# White is the wall. Black is the path. Red is the hidden data.
def build_graph(im):
width, height = im.size
start = None
end = None
G = nx.Graph()
for i in range(width):
for j in range(height):
if im.getpixel((i, j)) not in {(255, 255, 255, 255), (127, 127, 127, 255)}:
G.add_node((i, j))
if j == 0:
start = (i, j) # (639,0)
elif j == height - 1:
end = (i, j) # (1,640)
for i in range(width):
for j in range(height):
if not G.has_node((i, j)):
continue
dirs = [[1, 0], [0, 1], [-1, 0], [0, -1]]
for dir in dirs:
neighbor = (i + dir[0], j + dir[1])
if G.has_node(neighbor):
G.add_edge((i, j), neighbor)
return G, start, end
def solve(something):
im = Image.open(BytesIO(something))
print("Building Maze Graph...")
Maze, start, end = build_graph(im)
print("Searching Shortest path...")
path = nx.astar_path(Maze, start, end)
#
print("Showing Shortest path...")
im2 = im.copy()
for coord in path:
im2.putpixel(coord, (0, 255, 0, 255))
im2.show()
#
print("Extracting hidden data...")
result = None
hidden = bytes([im.getpixel(coords)[0] for coords in path[1::2]])
with zipfile.ZipFile(BytesIO(hidden), 'r') as zip:
for name in zip.namelist():
if name.endswith('.jpg'):
result = BytesIO(zip.read(name))
elif name.endswith('.zip'):
# Generate 'mybroken.zip' in current folder
zip.extract(name)
return result
def plot(byte_io):
Image.open(byte_io).show()
if __name__ == "__main__":
r = requests.get(url)
something = r.content
answer = solve(something)
plot(answer)
# lake
# http://butter:fly@www.pythonchallenge.com/pc/hex/lake.html