-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
166 lines (131 loc) · 4.24 KB
/
day20.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import unittest
from input import read_and_solve
def parse_input(lines):
index = lines.index("")
return lines[:index][0], list(map(list, lines[index+1:]))
def add_border(image):
for row in image:
row.insert(0, ".")
row.append(".")
image.insert(0, list("." * len(image[0])))
image.append(list("." * len(image[0])))
def enhance(algo, image):
add_border(image)
add_border(image)
enhanced = [[] for _ in range(len(image) - 2)]
for r in range(1, len(image) - 1):
for c in range(1, len(image[0]) - 1):
bin_digits = []
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
bin_digits.append("0" if image[r+dr][c+dc] == "." else "1")
offset = int("".join(bin_digits), 2)
enhanced[r-1].append(algo[offset])
return enhanced
def count_pixels(image):
pixels = [pixel for row in image for pixel in row]
return pixels.count("#")
def part1(lines):
algo, image = parse_input(lines)
for _ in range(2):
image = enhance(algo, image)
return count_pixels(image)
def part2(lines):
algo, image = parse_input(lines)
for _ in range(50):
image = enhance(algo, image)
return count_pixels(image)
class TestDay20(unittest.TestCase):
sample = [
"".join([
"..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##",
"#..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###",
".######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#.",
".#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#.....",
".#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#..",
"...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.....",
"..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#",
]),
"",
"#..#.",
"#....",
"##..#",
"..#..",
"..###",
]
def test_parse_input(self):
algo, image = parse_input(self.sample)
self.assertEqual(len(algo), 512)
self.assertEqual(image, [
["#", ".", ".", "#", "."],
["#", ".", ".", ".", "."],
["#", "#", ".", ".", "#"],
[".", ".", "#", ".", "."],
[".", ".", "#", "#", "#"],
])
def test_add_border(self):
image = [
list("#..#."),
list("#...."),
list("##..#"),
list("..#.."),
list("..###"),
]
add_border(image)
self.assertEqual(image, [
list("......."),
list(".#..#.."),
list(".#....."),
list(".##..#."),
list("...#..."),
list("...###."),
list("......."),
])
add_border(image)
self.assertEqual(image, [
list("........."),
list("........."),
list("..#..#..."),
list("..#......"),
list("..##..#.."),
list("....#...."),
list("....###.."),
list("........."),
list("........."),
])
def test_enhance(self):
algo = self.sample[0]
image = [
list("#..#."),
list("#...."),
list("##..#"),
list("..#.."),
list("..###"),
]
enhanced_image = enhance(algo, image)
self.assertEqual(enhanced_image, [
list(".##.##."),
list("#..#.#."),
list("##.#..#"),
list("####..#"),
list(".#..##."),
list("..##..#"),
list("...#.#."),
])
def test_count_pixels(self):
image = [
list("#..#."),
list("#...."),
list("##..#"),
list("..#.."),
list("..###"),
]
pixels = count_pixels(image)
self.assertEqual(pixels, 10)
def test_part_1_sample(self):
self.assertEqual(part1(self.sample), 35)
def test_part_2_sample(self):
self.assertEqual(part2(self.sample), 3351)
if __name__ == '__main__':
unittest.main(exit=False)
read_and_solve(__file__, part1, part2)