Skip to content

Instantly share code, notes, and snippets.

@haakonvt
Last active December 11, 2023 19:50
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/b6aa6a8bec470fe29118f7f395ad5c9d to your computer and use it in GitHub Desktop.
Save haakonvt/b6aa6a8bec470fe29118f7f395ad5c9d to your computer and use it in GitHub Desktop.
AOC 2023, day 11
import numpy as np
from scipy.spatial.distance import cdist
def dist_calc(expansion):
grid = np.array(list(map(list, inp.splitlines())), dtype="U1")
cols = np.nonzero(np.all(grid == ".", axis=0))[0]
rows = np.nonzero(np.all(grid == ".", axis=1))[0]
locs = np.argwhere(grid == "#")
locs[:, 0] += expansion * np.sum([locs[:, 0] > r for r in rows], axis=0)
locs[:, 1] += expansion * np.sum([locs[:, 1] > c for c in cols], axis=0)
return int(cdist(locs, locs, metric="cityblock").sum() / 2)
part_one = dist_calc(1)
part_two = dist_calc(999_999)
# ------------------------------------------------------------------------ #
# Initial solution to part one: (will send your RAM flying if run on part 2)
# ------------------------------------------------------------------------ #
lines = []
for line in inp.splitlines():
lines.append(line)
if "#" not in line:
lines.append("."*len(line))
columns = []
for col in map("".join, zip(*lines)):
columns.append(col)
if "#" not in col:
columns.append("." * len(col))
# There and back again
uni = np.array(list(zip(*columns)))
locs = np.argwhere(uni == "#")
int(cdist(locs, locs, metric="cityblock").sum() / 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment