-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.py
74 lines (53 loc) · 1.98 KB
/
20.py
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
INPUT_FILE = "input_20"
SAMPLE = """..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###"""
def decode(algorithm, seed, i, j, fill):
n = 0
b = 8
for l in range(j - 1, j + 2):
for k in range(i - 1, i + 2):
if 0 <= k < len(seed[0]) and 0 <= l < len(seed):
if seed[l][k] == "#":
n += 2**b
else:
if fill == "#":
n += 2**b
b -= 1
assert b == -1
return algorithm[n]
def decode_image(algorithm, seed, fill):
nrows = len(seed) + 2
ncols = len(seed[0]) + 2
output = [[fill] * ncols for _ in range(nrows)]
for j in range(nrows):
for i in range(ncols):
output[j][i] = decode(algorithm, seed, i - 1, j - 1, fill)
return output
def print_grid(grid):
print("\n".join("".join(row) for row in grid))
def count_pixels(grid):
return "".join("".join(row) for row in grid).count("#")
if __name__ == "__main__":
input_image = []
with open(INPUT_FILE) as f:
# f = iter(SAMPLE.splitlines())
algorithm = next(f).strip()
next(f)
for line in f:
input_image.append(list(line.strip()))
image = input_image
for i in range(50):
if algorithm[0] == "#" and i % 2 == 1:
fill = "#"
else:
fill = "."
image = decode_image(algorithm, image, fill)
# Part 1
if i == 1:
print(count_pixels(image))
# Part 2
print(count_pixels(image))