Skip to content

Commit cdd2fef

Browse files
committed
Refactor solution to mission 26
1 parent 40f5d73 commit cdd2fef

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

24-ambiguity.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def crop_blue_only(image):
101101
start = find_black_square(maze, size, 0)
102102
finish = find_black_square(maze, size, size[1] - 1)
103103
data = tumble_down(maze, start, finish)
104-
image = extract_image(data)
105-
new_image = crop_blue_only(image)
106-
print(image_to_text(new_image))
104+
105+
if __name__ == "__main__":
106+
image = extract_image(data)
107+
new_image = crop_blue_only(image)
108+
print(image_to_text(new_image))

26-decent.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111
# http://www.pythonchallenge.com/pc/hex/decent.html
1212

1313
from auth import read_riddle
14-
from wand.image import Image
15-
from zipfile import crc32 # NOQA used by _update_crc recompiled in patch_update_crc
14+
from image import image_to_text
15+
from importlib import import_module
16+
from io import BytesIO
17+
from PIL import Image
18+
from wand.exceptions import BaseError as WandBaseError
19+
from wand.image import Image as WandImage
1620

1721
import inspect
1822
import zipfile
1923

2024

25+
def get_ambiguity_data():
26+
ambiguity = import_module("24-ambiguity")
27+
return ambiguity.data
28+
29+
2130
def extract_source(func):
2231
"""Extracts source of func"""
2332
source = inspect.getsource(func).splitlines()
@@ -26,45 +35,42 @@ def extract_source(func):
2635

2736

2837
def patch_update_crc():
29-
"""Monkey-patch `ZipExtFile._update_crc` to bypass any bad CRC"""
38+
"""Monkey-patches `ZipExtFile._update_crc` to bypass any bad CRC"""
3039
update_crc_src = extract_source(zipfile.ZipExtFile._update_crc).replace(
3140
"raise", "return #"
3241
)
33-
exec(compile(update_crc_src, "<string>", "exec"))
34-
zipfile.ZipExtFile._update_crc = locals()["_update_crc"] # defined at runtime above
42+
exec_globals = {"crc32": zipfile.crc32}
43+
exec(compile(update_crc_src, "<string>", "exec"), exec_globals)
44+
zipfile.ZipExtFile._update_crc = exec_globals["_update_crc"]
3545

3646

37-
def reveal_mybroken_gif():
38-
"""Extract a gif from a corrupt zip file and displays it as text"""
47+
def extract_image(zip_data):
48+
"""Extracts an image from a corrupt zip file inside `zip_data`"""
3949
patch_update_crc()
40-
try:
41-
with zipfile.ZipFile("mybroken.zip", "r") as zip_file, zip_file.open(
42-
zip_file.namelist()[0]
43-
) as gif_file:
44-
# Pillow couldn't read the corrupt gif, even with
45-
# PIL.ImageFile.LOAD_TRUNCATED_IMAGES set to True
46-
mybroken, text = Image(file=gif_file), ""
47-
for y in range(0, mybroken.size[1], 2):
48-
line = ""
49-
for x in range(0, mybroken.size[0], 2):
50-
if not mybroken[x, y].blue:
51-
line += " "
52-
else:
53-
line += "*"
54-
if "*" in line:
55-
text += line + "\n"
56-
return text
57-
except FileNotFoundError:
58-
return "Please run mission 24 and rerun this mission"
59-
60-
61-
print(reveal_mybroken_gif())
50+
with zipfile.ZipFile(BytesIO(zip_data)) as outer:
51+
for name in outer.namelist():
52+
try:
53+
with zipfile.ZipFile(outer.open(name)) as inner, inner.open(
54+
inner.namelist()[0]
55+
) as img_file:
56+
# Pillow couldn't read the corrupt gif, even with
57+
# PIL.ImageFile.LOAD_TRUNCATED_IMAGES set to True
58+
return WandImage(file=img_file)
59+
except (zipfile.BadZipFile, WandBaseError):
60+
pass
61+
62+
63+
ambiguity_data = get_ambiguity_data()
64+
image = extract_image(ambiguity_data)
65+
image_data = image.export_pixels()
66+
pil_image = Image.frombytes("RGBA", image.size, bytes(image_data))
67+
print(image_to_text(pil_image, skip=3))
6268

6369
for line in read_riddle(
6470
"http://www.pythonchallenge.com/pc/hex/decent.html"
6571
).splitlines():
66-
if "<" in line:
67-
continue
6872
if not line:
6973
break
70-
print(line)
74+
if "<" in line:
75+
continue
76+
print(line.rsplit(maxsplit=1)[-1])

0 commit comments

Comments
 (0)