Skip to content

Commit bc8ba68

Browse files
committed
Day 20, Part 1
1 parent eb6b690 commit bc8ba68

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

day_20/__init__.py

Whitespace-only changes.

day_20/__main__.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from typing import Generator, TextIO, List
2+
import sys
3+
from dataclasses import dataclass
4+
from collections import defaultdict
5+
6+
@dataclass(frozen=True, eq=True)
7+
class Point:
8+
x: int
9+
y: int
10+
11+
def neighbors(self) -> Generator["Point", None, None]:
12+
for y_delta in (-1,0,1):
13+
for x_delta in (-1,0,1):
14+
yield Point(x=self.x+x_delta, y=self.y+y_delta)
15+
16+
class Image:
17+
def __init__(self, algo):
18+
self.algo = algo
19+
self.points = set()
20+
self.min_x = 0
21+
self.min_y = 0
22+
self.max_x = 0
23+
self.max_y = 0
24+
25+
def set(self, point: Point, value: bool):
26+
if value:
27+
self.points.add(point)
28+
self.min_x = min(self.min_x, point.x)
29+
self.min_y = min(self.min_y, point.y)
30+
self.max_x = max(self.max_x, point.x)
31+
self.max_y = max(self.max_y, point.y)
32+
else:
33+
self.points.discard(point)
34+
35+
def __len__(self):
36+
return len(self.points)
37+
38+
def __str__(self):
39+
out = ''
40+
for y in range(self.min_y, self.max_y+1):
41+
for x in range(self.min_x, self.max_x+1):
42+
if Point(x,y) in self.points:
43+
out += '#'
44+
else:
45+
out += '.'
46+
out += '\n'
47+
out += f'{len(self)}\n'
48+
out += f'({self.max_y-self.min_y}, {self.max_x-self.min_x})'
49+
return out
50+
51+
def _apply_algo(self, code: List[bool]):
52+
index = sum(1<<(8-index) for index, value in enumerate(code) if value)
53+
return self.algo[index] == '#'
54+
55+
56+
def step(self) -> "Image":
57+
extend = 10
58+
new_image = Image(self.algo)
59+
for y in range(self.min_y-extend, self.max_y+extend+1):
60+
for x in range(self.min_x-extend, self.max_x+extend+1):
61+
point = Point(x=x,y=y)
62+
code = [neighbor in self.points for neighbor in point.neighbors()]
63+
value = self._apply_algo(code)
64+
new_image.set(point, value)
65+
66+
return new_image
67+
68+
def read_input(textio: TextIO):
69+
lines = textio.readlines()
70+
algo = lines[0].strip()
71+
72+
image = Image(algo)
73+
image_input = lines[2:]
74+
for y, line in enumerate(image_input):
75+
for x, char in enumerate(line.strip()):
76+
image.set(Point(x,y), char == '#')
77+
return image
78+
79+
image = read_input(sys.stdin)
80+
81+
82+
print(image)
83+
print()
84+
image = image.step()
85+
print(image)
86+
print()
87+
image = image.step()
88+
print(image)
89+
print()
90+
# image = image.step()
91+
# print(image)
92+
# print()
93+
# image = image.step()
94+
# print(image)
95+
# print()
96+
# image = image.step()
97+
# print(image)
98+
# print()
99+
# image = image.step()
100+
# print(image)
101+
# print()
102+
# image = image.step()
103+
# print(image)
104+
# print()
105+
# image = image.step()
106+
# print(image)
107+
# print()
108+
# image = image.step()
109+
# print(image)
110+
# print()
111+
# image = image.step()
112+
# print(image)

day_20/input

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
##.#...##.#..###.#.#.##..#...###..##.###.###...#...#.#.##.#.##.#...####.####..###..#.##.#.##..##.##.

day_20/test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
2+
3+
#..#.
4+
#....
5+
##..#
6+
..#..
7+
..###

day_20/test2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
###.#.##.##....##...##.#..##...#..##.#.#.#.##.##...#.##.#...#.#.#.#.#...##.#...#####..#...#.#.#...##.#.####.##.##.###......###.#.###...#..##.#..#..##.##.#..###.###.#.#...#.##.######..####..##..#.#.#####.##.###..###.#.#.#....##.#.####.....#..#..#.##.#.##...##.#...###......###.#....#..#......#.##.#..##.#.###.###.####.##..###.#...#.##.####.#.##..#.#..#.#.......#.####..#..##..###.####..##.#.##.###....#.###..####..##......###.##.#.#...#..####.##.#...#..###..##..####.###...#.#..##.#.###.#.....##..#...###..####...
2+
3+
.

0 commit comments

Comments
 (0)