Skip to content

Instantly share code, notes, and snippets.

@haakonvt
Created December 13, 2023 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haakonvt/f973634be872b5853adad85aa5b6a635 to your computer and use it in GitHub Desktop.
Save haakonvt/f973634be872b5853adad85aa5b6a635 to your computer and use it in GitHub Desktop.
AOC 2023, day 13, p2
import numpy as np
grids = [np.array(list(map(list, grid.splitlines()))) for grid in inp.split("\n\n")]
def is_reflection(arr, col):
a1, a2 = arr[:, :col], np.fliplr(arr[:, col:])
l1, l2 = a1.shape[1], a2.shape[1]
if l1 > l2:
return np.all(a2 == a1[:,-l2:])
else:
return np.all(a1 == a2[:,-l1:])
def get_reflection(arr, dim):
if dim == 1:
arr = arr.transpose()
for col in 1 + np.all(arr[:, :-1] == arr[:, 1:], axis=0).nonzero()[0]:
if is_reflection(arr, col):
return col
part_one = 0
for i, g in enumerate(grids):
for dim, w in zip([0, 1], [1, 100]):
if (idx := get_reflection(g, dim)) is not None:
part_one += w * idx
break
print(part_one)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment