From ba924fc41c64ae8416a76e9ef34f8a73e265dacc Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Mon, 12 Dec 2022 17:21:51 +1030 Subject: [PATCH] remove pointless check, tabify --- 2022/day12.py | 85 ++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/2022/day12.py b/2022/day12.py index 0170ec4..7346abf 100644 --- a/2022/day12.py +++ b/2022/day12.py @@ -9,61 +9,42 @@ accszExk acctuvwj abdefghi'''.strip().split('\n') -def make_heightmap(lines: list[str]) -> tuple[ArrayLike, ArrayLike, ArrayLike]: - heightmap = np.zeros((len(lines[0]), len(lines)), dtype=dtype) - for y, line in enumerate(lines): - for x, c in enumerate(line): - if c == 'S': - heightmap[x,y] = 0 - start = np.array([x,y], dtype=dtype) - elif c == 'E': - heightmap[x,y] = 25 - end = np.array([x,y], dtype=dtype) - else: - heightmap[x,y] = ord(c) - ord('a') - return heightmap, start, end -# def make_cell_costs(position, heightmap): -# init_val = 9_999_999 -# costs = np.full_like(heightmap, init_val) -# pos_t = tuple(position) -# costs[pos_t] = 0 -# curr_positions = {pos_t} -# while (costs == init_val).any() and len(curr_positions) > 0: -# next_positions = set() -# for pos_t in curr_positions: -# n_h_limit = heightmap[pos_t] + 1 -# n_cost = costs[pos_t] + 1 -# for d in directions_array: -# next_t = (pos_t[0] + d[0], pos_t[1] + d[1]) -# if (0 <= next_t[0] < heightmap.shape[0]) and (0 <= next_t[1] < heightmap.shape[1]): -# hn = heightmap[next_t] -# if n_h_limit >= hn and costs[next_t] > n_cost: -# costs[next_t] = n_cost -# next_positions.add(next_t) -# curr_positions = next_positions -# return costs +def make_heightmap(lines: list[str]) -> tuple[ArrayLike, ArrayLike, ArrayLike]: + heightmap = np.zeros((len(lines[0]), len(lines)), dtype=dtype) + for y, line in enumerate(lines): + for x, c in enumerate(line): + if c == 'S': + heightmap[x,y] = 0 + start = np.array([x,y], dtype=dtype) + elif c == 'E': + heightmap[x,y] = 25 + end = np.array([x,y], dtype=dtype) + else: + heightmap[x,y] = ord(c) - ord('a') + return heightmap, start, end + def make_reversed_cell_costs(position, heightmap): - init_val = 9_999_999 - costs = np.full_like(heightmap, init_val) - pos_t = tuple(position) - costs[pos_t] = 0 - curr_positions = {pos_t} - while (costs == init_val).any() and len(curr_positions) > 0: - next_positions = set() - for pos_t in curr_positions: - n_h_limit = heightmap[pos_t] - 1 - n_cost = costs[pos_t] + 1 - for d in directions_array: - next_t = (pos_t[0] + d[0], pos_t[1] + d[1]) - if (0 <= next_t[0] < heightmap.shape[0]) and (0 <= next_t[1] < heightmap.shape[1]): - hn = heightmap[next_t] - if n_h_limit <= hn and costs[next_t] > n_cost: - costs[next_t] = n_cost - next_positions.add(next_t) - curr_positions = next_positions - return costs + init_val = 9_999_999 + costs = np.full_like(heightmap, init_val) + pos_t = tuple(position) + costs[pos_t] = 0 + curr_positions = {pos_t} + while len(curr_positions) > 0: + next_positions = set() + for pos_t in curr_positions: + n_h_limit = heightmap[pos_t] - 1 + n_cost = costs[pos_t] + 1 + for d in directions_array: + next_t = (pos_t[0] + d[0], pos_t[1] + d[1]) + if (0 <= next_t[0] < heightmap.shape[0]) and (0 <= next_t[1] < heightmap.shape[1]): + hn = heightmap[next_t] + if n_h_limit <= hn and costs[next_t] > n_cost: + costs[next_t] = n_cost + next_positions.add(next_t) + curr_positions = next_positions + return costs # heightmap, start, end = make_heightmap(sample_lines) heightmap, start, end = make_heightmap(lines)