summaryrefslogtreecommitdiffstats
path: root/13.py
blob: 2b4a622d30eb6a1a35f891a2a2e3bbf3ca6465ca (plain)
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
from utils import open_day
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int
    @staticmethod
    def from_str(s: str) -> 'Point':
        return Point(*map(int, s.split(',', maxsplit=1)))

@dataclass
class Fold:
    axis: str
    pos: int
    @staticmethod
    def from_str(s: str) -> 'Fold':
        axis, pos = s.split(' ')[2].split('=', maxsplit=1)
        return Fold(axis, int(pos))

with open_day(13) as f:
    parts = f.read().rstrip().split('\n\n')
    points = [Point.from_str(l) for l in parts[0].split('\n')]
    folds = [Fold.from_str(l) for l in parts[1].split('\n')]

def fold(points: list[Point], fold: Fold):
    for i in range(len(points)):
        point_pos = getattr(points[i], fold.axis)
        if point_pos > fold.pos:
            setattr(points[i], fold.axis, 2 * fold.pos - point_pos)

for i, f in enumerate(folds):
    fold(points, f)
    if i == 0: print(len(set((p.x, p.y) for p in points)))
maxx = min(f.pos for f in folds if f.axis == 'x')
maxy = min(f.pos for f in folds if f.axis == 'y')
points = set((p.x, p.y) for p in points)
for y in range(maxy):
    for x in range(maxx):
        print('#' if (x, y) in points else ' ', end='')
    print()