-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
75 lines (66 loc) · 2.62 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
import functools
import itertools
SAMPLE_INPUT = [
"..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#",
"",
"#..#.",
"#....",
"##..#",
"..#..",
"..###",
]
def images(lines):
alg = lines[0].strip()
image = [line.strip() for line in lines[2:]]
fill = "."
while True:
yield image if fill == "." else None
width = len(image[0])
image = [
"".join(
alg[
functools.reduce(
lambda x, y: 2 * x + (ord(y) & 1),
(
line1[x - 1] if line1 and 0 <= x - 1 < width else fill,
line1[x] if line1 and 0 <= x < width else fill,
line1[x + 1] if line1 and 0 <= x + 1 < width else fill,
line2[x - 1] if line2 and 0 <= x - 1 < width else fill,
line2[x] if line2 and 0 <= x < width else fill,
line2[x + 1] if line2 and 0 <= x + 1 < width else fill,
line3[x - 1] if line3 and 0 <= x - 1 < width else fill,
line3[x] if line3 and 0 <= x < width else fill,
line3[x + 1] if line3 and 0 <= x + 1 < width else fill,
),
0,
)
]
for x in range(-1, width + 1)
)
for y in range(-1, len(image) + 1)
for line1 in (image[y - 1] if 0 <= y - 1 < len(image) else None,)
for line2 in (image[y] if 0 <= y < len(image) else None,)
for line3 in (image[y + 1] if 0 <= y + 1 < len(image) else None,)
]
fill = alg[-1] if ord(fill) & 1 else alg[0]
def part1(lines):
"""
>>> part1(SAMPLE_INPUT)
35
"""
return sum(
c == "#"
for line in next(itertools.islice(images(lines), 2, None))
for c in line
)
def part2(lines):
"""
>>> part2(SAMPLE_INPUT)
3351
"""
return sum(
c == "#"
for line in next(itertools.islice(images(lines), 50, None))
for c in line
)
parts = (part1, part2)