-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
54 lines (38 loc) · 1.34 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
import re
from PIL import Image
import pytesseract as pytesseract
from matplotlib import pyplot as plt
import numpy as np
from aoc import get_input
def parse(lines):
dot_lines, fold_lines = lines.split('\n\n')
dots = [tuple(map(int, line.split(','))) for line in dot_lines.splitlines()]
X, Y = max(d[0] for d in dots)+1, max(d[1] for d in dots)+1
sheet = np.full((X, Y), False)
for x, y in dots:
sheet[x, y] = True
pattern = r'fold along ([xy])=(\d+)'
folds = []
for line in fold_lines.splitlines():
direction, l = re.fullmatch(pattern, line).groups()
folds.append((direction, int(l)))
return sheet, folds
sheet, folds = parse(get_input(day=13, as_list=False))
for i, (direc, l) in enumerate(folds):
if direc == 'y':
sheet = sheet.T
if 2*l + 1 > sheet.shape[0]:
empty_sheet = np.full((2*l + 1, sheet.shape[1]), False)
empty_sheet[:sheet.shape[0], :sheet.shape[1]] = sheet
sheet = empty_sheet
left, right = sheet[:l, :], sheet[l+1:, :]
sheet = left + np.flip(right, axis=0)
if direc == 'y':
sheet = sheet.T
if i == 0:
print(sheet.sum())
fig = plt.figure(figsize=(1.5, 0.75), dpi=100)
plt.imshow(sheet.T, cmap='binary')
plt.axis('off')
plt.savefig('day13-2.png')
print(pytesseract.image_to_string(Image.open('day13-2.png')))