Skip to content

Commit 71b6062

Browse files
committed
Refactor get_heat function and fix indexing issue in next_states
1 parent 1e92f33 commit 71b6062

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

17/sol.exs

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ defmodule Day17 do
55
|> Enum.map(fn line -> String.graphemes(line) |> Enum.map(&String.to_integer/1) end)
66
end
77

8-
def get_heat({x, y}, heatmap) do
9-
case heatmap |> Enum.at(x) do
10-
nil -> nil
11-
line -> Enum.at(line, y)
12-
end
13-
end
14-
158
def apply_dir_to_pos({x, y}, {dx, dy}), do: {x + dx, y + dy}
169
@dirs [{1, 0}, {0, -1}, {-1, 0}, {0, 1}]
1710

@@ -24,7 +17,7 @@ defmodule Day17 do
2417
def next_states(heatmap, grid_lims, costs, min_dist, max_dist, [
2518
{{pos, dir, dist}, current_cost} | rest
2619
]) do
27-
new_current_cost = get_heat(pos, heatmap) + current_cost
20+
new_current_cost = (heatmap |> elem(elem(pos, 0)) |> elem(elem(pos, 1))) + current_cost
2821

2922
if !(costs |> Map.get({pos, dir, dist}) <= new_current_cost) do
3023
new_costs = Map.put(costs, {pos, dir, dist}, new_current_cost)
@@ -68,7 +61,7 @@ defmodule Day17 do
6861
def next_states(heatmap, min_dist, max_dist),
6962
do:
7063
next_states(
71-
heatmap,
64+
heatmap |> Enum.map(&List.to_tuple/1) |> List.to_tuple(),
7265
{length(heatmap), length(Enum.at(heatmap, 0))},
7366
%{},
7467
min_dist,

0 commit comments

Comments
 (0)