2023.11.02
This commit is contained in:
parent
038277283a
commit
c3653bbc3c
1 changed files with 29 additions and 62 deletions
|
@ -16,7 +16,8 @@ def parse(instr: str) -> Universe:
|
|||
res = {}
|
||||
for y, line in enumerate(instr.splitlines()):
|
||||
for x, char in enumerate(line):
|
||||
res[(x, y)] = char
|
||||
if char != ".":
|
||||
res[(x, y)] = char
|
||||
return res
|
||||
|
||||
|
||||
|
@ -26,7 +27,7 @@ def print_coord_grid(universe: Universe):
|
|||
|
||||
for y in range(n_y):
|
||||
for x in range(n_x):
|
||||
_debug(universe[(x, y)], end="")
|
||||
_debug(universe.get((x, y), "."), end="")
|
||||
_debug()
|
||||
|
||||
|
||||
|
@ -38,7 +39,7 @@ def get_universe_y_size(universe: Universe):
|
|||
return max(map(lambda x: x[1], universe.keys())) + 1
|
||||
|
||||
|
||||
def expand_universe(universe: Universe):
|
||||
def expand_universe(universe: Universe, n: int):
|
||||
n_x = get_universe_x_size(universe)
|
||||
n_y = get_universe_y_size(universe)
|
||||
|
||||
|
@ -48,7 +49,7 @@ def expand_universe(universe: Universe):
|
|||
for y in reversed(range(n_y)):
|
||||
all_are_empty = True
|
||||
for x in range(n_x):
|
||||
if universe[(x, y)] != ".":
|
||||
if universe.get((x, y)) is not None:
|
||||
all_are_empty = False
|
||||
break
|
||||
|
||||
|
@ -58,7 +59,7 @@ def expand_universe(universe: Universe):
|
|||
for x in reversed(range(n_x)):
|
||||
all_are_empty = True
|
||||
for y in range(n_y):
|
||||
if universe[(x, y)] != ".":
|
||||
if universe.get((x, y)) is not None:
|
||||
all_are_empty = False
|
||||
break
|
||||
|
||||
|
@ -66,86 +67,52 @@ def expand_universe(universe: Universe):
|
|||
expand_cols.append(x)
|
||||
|
||||
for src_col_x in reversed(sorted(expand_cols)):
|
||||
for x in reversed(range(src_col_x + 1, get_universe_x_size(universe) + 1)): # we don't want to touch the starting col but we do want to overflow by one
|
||||
for y in range(n_y):
|
||||
universe[(x, y)] = universe[(x - 1, y)]
|
||||
|
||||
n_x = get_universe_x_size(universe)
|
||||
exp = [galaxy for galaxy in universe if galaxy[0] > src_col_x]
|
||||
for galaxy in exp:
|
||||
(gx, gy) = galaxy
|
||||
v = universe[galaxy]
|
||||
del universe[galaxy]
|
||||
universe[(gx+n, gy)] = v
|
||||
|
||||
for src_row_y in reversed(sorted(expand_rows)):
|
||||
for y in reversed(range(src_row_y + 1, get_universe_y_size(universe) + 1)): # we don't want to touch the starting col but we do want to overflow by one
|
||||
for x in range(n_x):
|
||||
universe[(x, y)] = universe[(x, y - 1)]
|
||||
exp = [galaxy for galaxy in universe if galaxy[1] > src_row_y]
|
||||
for galaxy in exp:
|
||||
(gx, gy) = galaxy
|
||||
v = universe[galaxy]
|
||||
del universe[galaxy]
|
||||
universe[(gx, gy+n)] = v
|
||||
|
||||
|
||||
# def heuristic(a: Coordinate, b: Coordinate) -> float:
|
||||
# (c, d) = a
|
||||
# (e, f) = b
|
||||
# return math.sqrt((e - c) ** 2 + (f - d) ** 2)
|
||||
|
||||
|
||||
# def get_shortest_path_len(universe: Universe, start: Coordinate, end: Coordinate) -> int:
|
||||
# d = {start: 0}
|
||||
# h = {start: 0}
|
||||
# p = {}
|
||||
# f = {}
|
||||
|
||||
# while end not in d:
|
||||
# w = min(filter(lambda x: x[0] not in f, h.items()), key=lambda x: x[1])[0]
|
||||
# f[w] = None
|
||||
|
||||
# for cardinal_dir in ((-1, 0), (1, 0), (0, -1), (0, 1)):
|
||||
# u = apply_coord_delta(w, cardinal_dir)
|
||||
# if u not in universe:
|
||||
# continue
|
||||
|
||||
# if u not in d or h[w] + 1 < h[u]:
|
||||
# d[u] = d[w] + 1
|
||||
# h[u] = heuristic(w, u)
|
||||
# p[u] = w
|
||||
|
||||
# if u == end:
|
||||
# break
|
||||
|
||||
# return d[end]
|
||||
|
||||
def get_shortest_path_len(start: Coordinate, end: Coordinate) -> int:
|
||||
(xa, ya) = start
|
||||
(xb, yb) = end
|
||||
return abs(xb - xa) + abs(yb - ya)
|
||||
|
||||
|
||||
def one(instr: str):
|
||||
def run(instr: str, expand_to: int) -> int:
|
||||
universe = parse(instr)
|
||||
expand_universe(universe)
|
||||
|
||||
galaxies = []
|
||||
for y in range(get_universe_y_size(universe)):
|
||||
for x in range(get_universe_x_size(universe)):
|
||||
if universe[(x, y)] == "#":
|
||||
galaxies.append((x, y))
|
||||
expand_universe(universe, expand_to - 1)
|
||||
|
||||
galaxy_pairs = {}
|
||||
for g in galaxies:
|
||||
for h in galaxies:
|
||||
for g in universe:
|
||||
for h in universe:
|
||||
if h == g or (g, h) in galaxy_pairs or (h, g) in galaxy_pairs:
|
||||
continue
|
||||
galaxy_pairs[(g, h)] = None
|
||||
galaxy_pairs = list(galaxy_pairs.keys())
|
||||
|
||||
acc = 0
|
||||
n = len(galaxy_pairs)
|
||||
for i, (a, b) in enumerate(galaxy_pairs):
|
||||
_debug(f"{i}/{n} - ", end="")
|
||||
# h = heuristic(a, b)
|
||||
sp = get_shortest_path_len(a, b)
|
||||
_debug(h, sp)
|
||||
acc += sp
|
||||
for (a, b) in galaxy_pairs:
|
||||
acc += get_shortest_path_len(a, b)
|
||||
return acc
|
||||
|
||||
|
||||
def one(instr: str):
|
||||
return run(instr, 2)
|
||||
|
||||
|
||||
def two(instr: str):
|
||||
return
|
||||
return run(instr, 1_000_000)
|
||||
|
||||
|
||||
def _debug(*args, **kwargs):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue