2023/day13.py

48 lines
1.2 KiB
Python
Raw Normal View History

2023-12-29 21:03:50 +00:00
import sys
import numpy as np
# @profile
def main():
with open(sys.argv[1]) as f:
inp = [
[[c == "#" for c in line] for line in field.split("\n")]
for field in f.read().strip().split("\n\n")
]
(part1, part2) = (0, 0)
for field in inp:
field = np.array(field)
(h, w) = field.shape
(x1, y1, x2, y2) = (0, 0, 0, 0)
for x in range(1, w):
w2 = min(x, w - x)
left = field[:, (x - w2) : x]
right = field[:, x : (x + w2)][:, ::-1]
mismatches = (left != right).sum()
if not x2 and mismatches == 1:
x2 = x
elif not x1 and mismatches == 0:
x1 = x
if not x1 or not x2:
for y in range(1, h):
h2 = min(y, h - y)
left = field[(y - h2) : y]
right = field[y : (y + h2)][::-1]
mismatches = (left != right).sum()
if not y2 and mismatches == 1:
y2 = y
elif not y1 and mismatches == 0:
y1 = y
part1 += x1 + y1 * 100
part2 += x2 + y2 * 100
print(part1, part2)
main()