-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_15.ex
57 lines (47 loc) · 1.9 KB
/
day_15.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
defmodule Aoc.Y2021.Day15 do
@moduledoc """
Solved https://adventofcode.com/2021/day/15
"""
import Aoc.Helper.IO
def solve_part1(data) do
find_path([{0, 0, 0}], data, %{}, length(data), length(Enum.at(data, 0)), 1) - Enum.at(Enum.at(data, 0), 0)
end
def solve_part2(data) do
find_path([{0, 0, 0}], data, %{}, length(data), length(Enum.at(data, 0)), 5) - Enum.at(Enum.at(data, 0), 0)
end
def find_path([{_risk_level, row, column} | pqueue], data, cache, row_size, column_size, n)
when row < 0 or row >= row_size * n or column < 0 or column >= column_size * n do
find_path(pqueue, data, cache, row_size, column_size, n)
end
def find_path([{risk_level, row, column} | pqueue], data, cache, row_size, column_size, n) do
total_risk =
Enum.at(Enum.at(data, rem(row, row_size)), rem(column, column_size)) + div(row, row_size) +
div(column, column_size)
total_risk =
if total_risk > 9,
do: risk_level + rem(total_risk, 9),
else: risk_level + total_risk
if Map.get(cache, {row, column}) > total_risk do
cache = Map.update(cache, {row, column}, total_risk, fn _ -> total_risk end)
if row == row_size * n - 1 and column == column_size * n - 1 do
Map.get(cache, {row, column})
else
pqueue =
pqueue ++
(Enum.map([[-1, 0], [0, 1], [1, 0], [0, -1]], fn [x, y] ->
{Map.get(cache, {row, column}), row + x, column + y}
end)
|> Enum.reject(fn {_, o, p} -> Map.get(cache, {o, p}) end))
find_path(Enum.sort(pqueue), data, cache, row_size, column_size, n)
end
else
find_path(pqueue, data, cache, row_size, column_size, n)
end
end
def get_input() do
get_string_input("2021", "15")
|> String.split("\n", trim: true)
|> Enum.map(&(String.to_charlist(&1) |> Enum.map(fn v -> v - ?0 end)))
end
def solved_status(), do: :solved
end