clean up and use ocr for part 2
This commit is contained in:
parent
3298a33ea1
commit
1f28f0e30c
30
thirteen.py
30
thirteen.py
|
@ -1,23 +1,18 @@
|
|||
#!/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):
|
||||
ic(dim, n)
|
||||
if dim == 'x':
|
||||
ar = ar.T
|
||||
for row in range(1, min(n + 1, ar.shape[0] - n)): # 1-indexed makes sense
|
||||
try:
|
||||
ar[n-row] = np.logical_or(ar[n-row], ar[n+row])
|
||||
except Exception as e:
|
||||
ic(row, n)
|
||||
ic(e)
|
||||
ar = ar[:n+1]
|
||||
if dim == 'x':
|
||||
ar = ar.T
|
||||
return ar
|
||||
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:
|
||||
|
||||
|
@ -28,12 +23,19 @@ with open(0) as f:
|
|||
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)
|
||||
cv.imshow('thirteen', paper.astype('uint8') * 255)
|
||||
cv.waitKey(0)
|
||||
print(np.sum(paper)) # should be 95
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue