advent2021/five.py

34 lines
974 B
Python
Executable File

#!/usr/bin/env python3
from useful import *
import cv2 as cv
def tpl(l):
x, y, u, v = (int(n) for n in re.match(r'(\d+),(\d+) -> (\d+),(\d+)', l).groups())
return (x, y), (u, v)
def hori_or_vert(beg, fin):
return any(beg[i] == fin[i] for i in (0, 1))
with open(0) as f:
lines = [l.strip() for l in f.readlines()]
t = [tpl(l) for l in lines]
max_side = np.max(t)
first = np.zeros((len(t), max_side, max_side), dtype='uint8')
second = np.zeros((len(t), max_side, max_side), dtype='uint8')
for i, l in enumerate(t):
if hori_or_vert(*l):
cv.line(first[i], *l, 1)
cv.line(second[i], *l, 1)
for prism in first, second:
smash = prism.sum(axis=0, dtype='uint8')
print(np.sum(smash > 1))
try:
if sys.argv[1] == '-v':
pretty = smash * (255 // np.max(smash))
cv.imshow('', pretty)
cv.waitKey(0)
except:
pass