master
Dustin Swan 2 weeks ago
parent 697feb3c14
commit 08aab2ebc8
Signed by: dustinswan
GPG Key ID: AB49BD6B2B3A6377

@ -11,6 +11,7 @@ defmodule Day18 do
map_list1 = Enum.take(map_list, 1024)
# create the grid as a map
map1 =
Enum.reduce(0..(height - 1), %{}, fn row, acc ->
Enum.reduce(0..(width - 1), acc, fn col, acc2 ->
@ -29,10 +30,9 @@ defmodule Day18 do
{_path, shortest} = walk(map1, q, dest, visited, [], 100_000_000_000, width, height)
dbg(shortest)
# count up from 1024 and create a new map each time, until we create an impassable map
part2 =
Enum.reduce(1024..length(map_list), nil, fn idx, acc ->
dbg(idx)
if !is_nil(acc) do
# exit early, we already found the coord
acc
@ -57,7 +57,6 @@ defmodule Day18 do
q = :queue.from_list([{start, 0, [{0, 0}]}])
res = walk(map, q, dest, visited, [], 100_000_000_000, width, height)
dbg(res)
if is_nil(res) do
List.last(map_list)
@ -112,38 +111,6 @@ defmodule Day18 do
end
end
end
defp render(map, location \\ nil, visited \\ %{}) do
width = Map.keys(map) |> Enum.map(&elem(&1, 1)) |> Enum.max()
height = Map.keys(map) |> Enum.map(&elem(&1, 0)) |> Enum.max()
Enum.each(0..height, fn row ->
Enum.each(0..width, fn col ->
el = if Map.get(map, {col, row}, :wall) == :wall, do: "#", else: "."
visited? = Map.has_key?(visited, {col, row})
formatted =
if location == {col, row} do
IO.ANSI.format([:red_background, "@"])
else
if visited? do
IO.ANSI.format([:blue_background, el])
else
el
end
end
IO.write(formatted)
end)
IO.write("\n")
end)
IO.write("\n")
map
end
end
Day18.run()

Loading…
Cancel
Save