-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
66 lines (51 loc) · 1.73 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
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
Fold = namedtuple("Fold", ["axis", "line"])
with open("./input13.txt") as file:
input_dots, input_folds = file.read().split("\n\n")
dots = set()
for line in input_dots.strip().split():
x, y = line.strip().split(",")
dots.add(Point(int(x), int(y)))
folds = []
for line in input_folds.strip().split("\n"):
axis, line = line.removeprefix("fold along ").split("=")
folds.append(Fold(axis, int(line)))
def fold_once(dots, fold):
folded_dots = set()
for dot in dots:
if fold.axis == "y":
if dot.y > fold.line:
folded_y = fold.line - (dot.y - fold.line)
folded_dots.add(Point(dot.x, folded_y))
else:
folded_dots.add(dot)
else:
if dot.x > fold.line:
folded_x = fold.line - (dot.x - fold.line)
folded_dots.add(Point(folded_x, dot.y))
else:
folded_dots.add(dot)
return folded_dots
def fold(dots, folds):
for fold in folds:
dots = fold_once(dots, fold)
return dots
def print_dots(dots):
max_x = max(dot.x for dot in dots)
max_y = max(dot.y for dot in dots)
to_print = ""
for y in range(max_y + 1):
for x in range(max_x + 1):
if Point(x, y) in dots:
to_print += "#"
else:
to_print += "."
to_print += "\n"
print(to_print)
if __name__ == "__main__":
folded_once = fold_once(dots, folds[0])
print(f"Part 1\nAfter one fold, there are {len(folded_once)} dots visible.")
folded = fold(dots, folds)
print(f"Part 2\nThe code is:")
print_dots(folded)