-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13-python.py
40 lines (28 loc) · 981 Bytes
/
day13-python.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
import numpy as np
def parse_dot(line: str):
if "," in line:
x, y = line.strip().split(",")
return int(y), int(x) # reverse coords to be row-major
def parse_fold(line: str):
if "=" in line:
a, n = line.replace("fold along", "").strip().split("=")
return a, int(n)
with open('input-day-13.txt') as file:
lines = file.readlines()
dots = [d for line in lines if (d := parse_dot(line)) is not None]
folds = [f for line in lines if (f := parse_fold(line)) is not None]
shape = np.array(dots).max(axis=0) + 1
data = np.zeros(shape)
data[tuple(zip(*dots))] = 1
for i, (axis, row) in enumerate(folds):
if axis == "x":
data = data[:, 0:row] + data[:, :row:-1]
if axis == "y":
data = data[0:row, :] + data[:row:-1, :]
if i == 0:
print("Task 1:")
print(np.count_nonzero(data))
print()
print("Task 2:")
for row in data:
print("".join("." if i == 0 else "#" for i in row))