-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
44 lines (35 loc) · 1.47 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
import re
def fold(axis, coordinate, points):
new_points = set()
for x, y in points:
if axis == 'y' and y > coordinate:
new_points.add((x, coordinate * 2 - y))
elif axis == 'x' and x > coordinate:
new_points.add((coordinate * 2 - x, y))
else:
new_points.add((x, y))
return new_points
def print_grid(points):
max_x = max(x for x, _ in points) + 1
max_y = max(y for _, y in points) + 1
for y in range(max_y):
row = ('#' if (x, y) in points else ' ' for x in range(max_x))
print(' '.join(row))
with open('input/day13.txt') as f:
points, folds = f.read().strip().split('\n\n')
points = re.findall(r'(\d+),(\d+)', points)
points = {(int(x), int(y)) for x, y in points}
folds = re.findall(r'fold along ([xy])=(\d+)', folds)
folds = [(axis, int(coordinate)) for axis, coordinate in folds]
print(len(fold(*folds[0], points)))
for axis, coordinate in folds:
points = fold(axis, coordinate, points)
print_grid(points)
'''
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
'''