Skip to content

Commit b10166d

Browse files
committed
Add level_33.py
1 parent 7c7c58a commit b10166d

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ Google the grandpa rock and find the differences between two Mandelbrot images.
108108
See [Mandelbrot Set](https://en.wikipedia.org/wiki/Mandelbrot_set)
109109

110110
### Level 32
111-
Nonogram is a type of clue-based grid-filling puzzle.
112-
See [Nonogram](https://en.wikipedia.org/wiki/Nonogram)
113-
The CP-SAT solver in OR-Tools can solve such problems using boolean variables and constraints.
111+
Nonogram is a type of clue-based grid-filling puzzle.
112+
See [Nonogram](https://en.wikipedia.org/wiki/Nonogram)
113+
The CP-SAT solver in OR-Tools can solve such problems using boolean variables and constraints.
114114
See [考えるコード ― 制約と最適化の冒険](https://ruoyu0088.github.io/thinking_code/index.html)
115115

116+
### Level 33
117+
Remove the brightest pixels and find the square images.
118+
119+
120+
Congratulations! All levels are Cleared!

level_22.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ def solve(something):
2828
if dx == dy == 0:
2929
cx += 25
3030
cy = 100
31-
unknown.show()
31+
return unknown
32+
33+
def plot(im):
34+
im.show()
3235

3336

3437
if __name__ == "__main__":
3538
r = requests.get(url)
3639
something = r.content
3740
answer = solve(something)
41+
plot(answer)
3842
# bonus
3943

4044
# http://butter:fly@www.pythonchallenge.com/pc/hex/bonus.html

level_32.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ def parse(text):
9090
i_hor = next(i for i, line in enumerate(lines) if i > i_dim and line.startswith('# Horizontal'))
9191
i_ver = next(i for i, line in enumerate(lines) if i > i_hor and line.startswith('# Vertical'))
9292
# Parse rows and columns
93-
r, c = map(int, lines[i_dim + 1].split())
93+
rows, cols = map(int, lines[i_dim + 1].split())
9494
# Extract horizontal and vertical constraints
9595
row_clues = [list(map(int, line.split())) for line in lines[i_hor + 1:i_ver]]
9696
col_clues = [list(map(int, line.split())) for line in lines[i_ver + 1:]]
97-
return r, c, row_clues, col_clues
97+
return rows, cols, row_clues, col_clues
9898

9999

100-
def solve(r_num, c_num):
101-
print_nonogram(solve_nonogram(r_num, c_num))
100+
def solve(row_clues, col_clues):
101+
print_nonogram(solve_nonogram(row_clues, col_clues))
102102
# See an up arrow
103103

104104
url = PREFIX + 'up.html'
@@ -107,8 +107,8 @@ def solve(r_num, c_num):
107107
url2 = PREFIX + catch(requests.get(url).text)
108108
# http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/up.txt"
109109

110-
r, c, r_num, c_num = parse(requests.get(url2).text)
111-
print_nonogram(solve_nonogram(r_num, c_num))
110+
rows, cols, row_clues, col_clues = parse(requests.get(url2).text)
111+
print_nonogram(solve_nonogram(row_clues, col_clues))
112112
# See a python
113113

114114
url3 = PREFIX + 'python.html'
@@ -119,8 +119,8 @@ def solve(r_num, c_num):
119119
if __name__ == "__main__":
120120
r = requests.get(url)
121121
something = r.text
122-
r, c, r_num, c_num = parse(something)
123-
answer = solve(r_num, c_num)
122+
rows, cols, row_clues, col_clues = parse(something)
123+
answer = solve(row_clues, col_clues)
124124
print(answer)
125125
# "Free" as in "Free speech", not as in "free...
126126
# Google this sentence and the answer is beer

level_33.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/env python
2+
# coding=utf-8
3+
# http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/beer.html
4+
#
5+
# If you are blinded by the light,
6+
# remove its power, with its might.
7+
# Then from the ashes, fair and square,
8+
# another truth at you will glare.
9+
import re
10+
from io import BytesIO
11+
12+
import numpy as np
13+
import requests
14+
from PIL import Image
15+
16+
PREFIX = "http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/"
17+
url = PREFIX + 'beer2.jpg'
18+
19+
20+
def catch(text, pattern=r'<a href="(.*?)">', cnt=0):
21+
return re.findall(pattern, text, re.DOTALL)[cnt]
22+
23+
24+
def solve(something):
25+
im = Image.open(BytesIO(something))
26+
im.show()
27+
# no, png
28+
29+
url2 = PREFIX + 'beer2.png'
30+
im = Image.open(BytesIO(requests.get(url2).content))
31+
32+
# use numpy to accelerate
33+
data = np.array(im.getdata())
34+
# use histogram to skip non-existing pixels
35+
histogram = im.histogram()
36+
37+
hidden_images = []
38+
for value in range(254, -1, -1):
39+
if histogram[value] == 0:
40+
continue
41+
# Remove the brightest pixels
42+
data = data[data < value]
43+
# Reshape the array to a square image
44+
sq = np.sqrt(len(data))
45+
if sq.is_integer() and sq > 0:
46+
max_val = np.max(data)
47+
ashes = np.array([255 if v == max_val else 0 for v in data], dtype=np.uint8)
48+
image = Image.fromarray(ashes.reshape(int(sq), int(sq)))
49+
hidden_images.append(image)
50+
51+
# combine the images
52+
total_width = hidden_images[0].width*len(hidden_images)
53+
max_height = hidden_images[0].height
54+
composite_image = Image.new('L', (total_width, max_height))
55+
x_offset = 0
56+
for img in hidden_images:
57+
composite_image.paste(img, (x_offset, 0))
58+
x_offset += img.width
59+
60+
return composite_image
61+
62+
63+
def plot(im):
64+
im.show()
65+
66+
67+
if __name__ == "__main__":
68+
r = requests.get(url)
69+
something = r.content
70+
answer = solve(something)
71+
plot(answer)
72+
# gremlins
73+
74+
# http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/gremlins.html

0 commit comments

Comments
 (0)