-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
108 lines (81 loc) · 2.61 KB
/
day13.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
import argparse
import os
from dataclasses import dataclass
@dataclass(frozen=True)
class Fold:
axis: str
line: int
def __repr__(self):
return f"Fold({self.axis}={self.line})"
@dataclass(frozen=True)
class Point:
x: int
y: int
def __repr__(self):
return f"({self.x}, {self.y})"
def split_set(set_: set[Point], along: Fold) -> (set[Point], set[Point]):
cutoff = Point(-1, along.line) if along.axis == "y" else Point(along.line, -1)
split = {p for p in set_ if p.x > cutoff.x and p.y > cutoff.y}
set_ -= split
return set_, split
def reflect(point: Point, fold_: Fold) -> Point:
x = point.x
y = point.y
if fold_.axis == "x":
x = -x
x += 2 * fold_.line
else:
y = -y
y += 2 * fold_.line
return Point(x, y)
def fold(points: set[Point], fold_: Fold) -> set[Point]:
bottom, top = split_set(points, fold_)
reflections = {reflect(p, fold_) for p in top}
return bottom.union(reflections)
def render(points: set[Point]):
width = max(p.x for p in points) + 1
height = max(p.y for p in points) + 1
map_ = [False] * width * height
for p in points:
map_[p.y * width + p.x] = True
str_map = ["#" if p else " " for p in map_]
for i in range(width, len(str_map), width + 1):
str_map.insert(i, "\n")
print("".join(str_map))
def read_input(filepath: str) -> (set[Point], list[Fold]):
points = set()
folds = list()
with open(filepath, "r") as f:
for line in f.readlines():
if line.startswith("fold along"):
data = line.rstrip().split("=")
axis = data[0][-1]
val = int(data[1])
folds.append(Fold(axis, val))
else:
data = line.rstrip().split(",")
try:
x = int(data[0])
y = int(data[1])
points.add(Point(x, y))
except ValueError:
pass
return points, folds
def init_parser() -> str:
parser = argparse.ArgumentParser(description="Advent of Code day 13 solution.")
parser.add_argument(
"input", metavar="FILE", type=str, nargs=1, help="Path to input data."
)
args = parser.parse_args()
return os.path.realpath(args.input[0])
if __name__ == "__main__":
path = init_parser()
points, folds = read_input(path)
folded = fold(points, folds[0])
print(f"Part 1 visible dots: {len(folded)}")
for f in folds[1:]:
folded = fold(folded, f)
print("\nPart 2: ")
render(folded)
def main(_):
raise NotImplementedError