42 lines
1.2 KiB
Python
Executable File
42 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from useful import *
|
|
import easyocr
|
|
|
|
def parseline(s):
|
|
return [(x, int(y)) for (x, y) in re.findall(r'([xy])=(\d*)', s)]
|
|
|
|
def fold(ar, dim, n):
|
|
if dim == 'x':
|
|
ar = ar.T
|
|
height = min(n, ar.shape[0] - 1 - n)
|
|
ic(dim, n, height, ar.shape)
|
|
ar[n-height:n] |= ar[n+height:n:-1] # flip
|
|
ar = ar[:n] # remove flipped + cutting line
|
|
return ar.T if dim == 'x' else ar
|
|
|
|
with open(0) as f:
|
|
|
|
H,F = headerfooter(f)
|
|
inst = [commaline(l) for l in H.split('\n')]
|
|
folds = parseline(F)
|
|
ic(inst, folds)
|
|
paper = np.zeros((1+max(n for _, n in inst), 1+max(n for n, _ in inst)), dtype=bool)
|
|
for x,y in inst:
|
|
paper[y,x] = True
|
|
|
|
# part 1
|
|
paper = fold(paper, *folds[0])
|
|
print(np.sum(paper))
|
|
|
|
# part 2
|
|
for f in folds[1:]:
|
|
paper = fold(paper, *f)
|
|
print('\n'.join([
|
|
''.join(['#' if c else ' ' for c in l])
|
|
for l in paper]))
|
|
eor = easyocr.Reader(('en',))
|
|
pray = eor.readtext(np.pad(paper, (42, 42)).astype('uint8') * 255)
|
|
# [([[39, 39], [85, 39], [85, 51], [39, 51]], 'LRFJB JEH', 0.770243724973307)]
|
|
text = re.sub(r'[^A-Z]', '', ''.join([s for (_, s, _) in pray]))
|
|
print(text)
|