aboutsummaryrefslogtreecommitdiff
path: root/2021/day-15/day-15.livemd
blob: 2495c32dad43112d6cc729b10bc1ba0723fb518b (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!-- vim: set syntax=markdown: -->
<!-- livebook:{"persist_outputs":true} -->

# Advent of Code 2021, Day 15

## Short summary

**Parts 1 and 2.**  Find the least "risky" path through a grid of nodes,
where each node has a "risk" cost to enter.  Part 1's grid is the given data,
while Part 2's is the data after undergoing a transformation.
Only orthogonal travel is possible.

## Setup

Using the `libgraph` library to build the graph 
and use its implementation of the Dijkstra algorithm:

```elixir
Mix.install([
  {:libgraph, github: "bitwalker/libgraph"}
])
```

```output
* Getting libgraph (https://github.com/bitwalker/libgraph.git)
remote: Enumerating objects: 783, done.        
remote: Counting objects: 100% (64/64), done.        
remote: Compressing objects: 100% (52/52), done.        
remote: Total 783 (delta 27), reused 24 (delta 10), pack-reused 719        
origin/HEAD set to main
==> libgraph
Compiling 14 files (.ex)
Generated libgraph app
```

```output
:ok
```

```elixir
floor =
  with {:ok, data} <- File.read("./2021/day-15/input") do
    data
    |> String.split()
    |> Enum.map(fn xs ->
      String.graphemes(xs)
      |> Enum.map(&String.to_integer/1)
    end)
  end
```

```output
[
  [4, 5, 5, 2, 2, 8, 5, 9, 8, 9, 4, 4, 1, 1, 2, 4, 7, 1, 9, 7, 9, 8, 4, 6, 5, 8, 2, 5, 7, 7, 3, 3,
   1, 8, 2, 5, 2, 2, 6, 9, 4, 2, 2, 6, 2, 5, 7, 3, 1, ...],
  [8, 3, 1, 1, 8, 2, 6, 9, 1, 7, 6, 7, 7, 2, 9, 5, 5, 6, 1, 3, 9, 5, 7, 1, 6, 2, 4, 5, 4, 2, 6, 1,
   8, 4, 2, 1, 2, 4, 7, 1, 3, 1, 1, 2, 1, 4, 8, 5, ...],
  [3, 1, 3, 4, 1, 5, 5, 5, 2, 8, 2, 3, 1, 2, 9, 6, 3, 1, 3, 2, 9, 2, 9, 5, 1, 6, 8, 9, 3, 7, 1, 6,
   7, 1, 6, 1, 1, 3, 5, 5, 1, 9, 9, 4, 2, 9, 3, ...],
  [7, 1, 3, 4, 1, 5, 9, 6, 8, 1, 5, 3, 3, 3, 3, 1, 1, 7, 2, 9, 9, 1, 9, 3, 2, 8, 1, 9, 1, 2, 8, 7,
   1, 8, 1, 3, 1, 1, 2, 1, 4, 2, 8, 7, 1, 5, ...],
  [3, 2, 9, 1, 6, 2, 2, 1, 2, 1, 8, 9, 3, 8, 2, 1, 2, 5, 8, 2, 1, 5, 1, 1, 1, 5, 3, 1, 6, 1, 4, 2,
   3, 3, 9, 9, 9, 6, 6, 1, 6, 4, 9, 2, 7, ...],
  [9, 1, 1, 6, 1, 9, 7, 3, 1, 9, 9, 9, 2, 2, 2, 3, 7, 8, 4, 9, 7, 3, 7, 4, 9, 1, 5, 9, 9, 1, 2, 9,
   1, 8, 9, 4, 8, 5, 1, 4, 8, 5, 5, 2, ...],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  ...
]
```

Give each node a label based on its coordinates:

```elixir
floor_nodes =
  for {row, i} <- Enum.with_index(floor),
      {col, j} <- Enum.with_index(row),
      into: %{} do
    {{i, j}, col}
  end
```

```output
%{
  {76, 13} => 1,
  {37, 47} => 2,
  {65, 63} => 5,
  {38, 2} => 1,
  {1, 26} => 4,
  {83, 76} => 2,
  {32, 15} => 6,
  {89, 14} => 1,
  {35, 30} => 7,
  {37, 53} => 7,
  {4, 5} => 2,
  {8, 50} => 7,
  {78, 98} => 7,
  {95, 56} => 7,
  {74, 12} => 9,
  {11, 39} => 2,
  {65, 43} => 4,
  {22, 38} => 1,
  {14, 86} => 4,
  {20, 41} => 1,
  {29, 25} => 1,
  {86, 10} => 1,
  {83, 36} => 3,
  {29, 26} => 3,
  {47, 27} => 9,
  {4, 81} => 3,
  {31, 42} => 1,
  {9, 34} => 3,
  {90, 0} => 3,
  {67, 98} => 1,
  {13, 85} => 1,
  {63, 81} => 4,
  {82, 60} => 4,
  {47, 38} => 1,
  {15, 92} => 1,
  {58, 58} => 1,
  {20, 3} => 1,
  {61, 95} => 7,
  {23, 67} => 4,
  {78, 75} => 1,
  {79, 17} => 2,
  {75, 0} => 7,
  {16, 73} => 2,
  {76, 2} => 8,
  {58, 84} => 1,
  {58, 33} => 7,
  {47, 44} => 2,
  {54, 31} => 6,
  {13, ...} => 1,
  {...} => 9,
  ...
}
```

We can travel in the four cardinal directions from each node, so we need
a function to identify a node's neighbors:

```elixir
neighbors = fn {i, j}, nodes ->
  [{i + 1, j}, {i - 1, j}, {i, j + 1}, {i, j - 1}]
  |> Enum.filter(&Map.has_key?(nodes, &1))
end

[neighbors.({0, 0}, floor_nodes), neighbors.({50, 50}, floor_nodes)]
```

```output
[[{1, 0}, {0, 1}], [{51, 50}, {49, 50}, {50, 51}, {50, 49}]]
```

## Part 1

Now we fold all the edges into a `Graph`:

```elixir
make_graph = fn nodes ->
  for {coord, _} <- nodes,
      neighbor <- neighbors.(coord, nodes),
      risk = Map.get(nodes, neighbor),
      reduce: Graph.new(vertex_identifier: fn v -> v end) do
    acc -> Graph.add_edge(acc, coord, neighbor, weight: risk)
  end
end

floor_graph = make_graph.(floor_nodes)
```

```output
#Graph<type: directed, num_vertices: 10000, num_edges: 39600>
```

Now we just need to use Dijkstra's algorithm to find the best path from the upper left to the lower right,
and use the map of each node's risk value to sum up the total risk for the path.

```elixir
get_lowest_risk_path = fn nodes ->
  with {{min_c, _}, {max_c, _}} <- Enum.min_max_by(nodes, fn {{i, j}, _} -> i + j end) do
    nodes
    |> then(make_graph)
    |> Graph.dijkstra(min_c, max_c)
    |> tl()
    |> Enum.reduce(0, fn coord, sum -> sum + Map.get(nodes, coord) end)
  end
end

get_lowest_risk_path.(floor_nodes)
```

```output
403
```

## Part 2

The process for Part 2 will be similar; the only difference 
is that the graph is five times bigger after the transform.

If the transformed risk is greater than 9, it rolls over to 1, so a
`Stream.cycle` can be used to easily get the rolled-over values.

```elixir
expanded_floor_nodes =
  with {{max_i, max_j}, _} <- Enum.max_by(floor_nodes, fn {{i, j}, _} -> i + j end),
       nine_cycle = Stream.cycle(1..9) do
    for {{i, j}, risk} <- floor_nodes,
        x <- 0..4,
        y <- 0..4,
        into: %{} do
      {{i + x * (max_i + 1), j + y * (max_j + 1)}, Enum.at(nine_cycle, risk - 1 + x + y)}
    end
  end

Enum.count(expanded_floor_nodes)
```

```output
250000
```

We repeat the same steps as before: building the graph, 
using Dijkstra's algorithm and summing up the risks along the best path.

```elixir
get_lowest_risk_path.(expanded_floor_nodes)
```

```output
2840
```