this repo has no description
1<!-- livebook:{"persist_outputs":true} -->
2
3# Day 06
4
5```elixir
6Mix.install([:kino_aoc, :arrays])
7```
8
9## Section
10
11<!-- livebook:{"attrs":"eyJhc3NpZ25fdG8iOiJwdXp6bGVfaW5wdXQiLCJkYXkiOiI2Iiwic2Vzc2lvbl9zZWNyZXQiOiJBRFZFTlRfT0ZfQ09ERV9TRVNTSU9OIiwieWVhciI6IjIwMjQifQ","chunks":null,"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} -->
12
13```elixir
14{:ok, puzzle_input} =
15 KinoAOC.download_puzzle("2024", "6", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
16```
17
18<!-- livebook:{"output":true} -->
19
20```
21{:ok,
22 "...#..................#...................#......................#..............................#.............................#...\n....#.......................#........#.#..............##...........#.....#..........#..........................#..................\n.............#........................................................................#.........#.#..#..#..........#..............\n..........#.......#..........#..................#........##...................#..............#....................................\n....................#..........................................................#..#.....................................#.........\n....#............................#..............................................#.....................#..........##....#..........\n.......#.............#......................................................................#.............#.........#.............\n....###......................#....#...........#.....#................................#..........................#.................\n..........#..#...........#..#........................................................#.....#...........#................#.........\n..#....#......#....#....#...................................................................#.....................................\n....................#............#............................................#......#....#...............................#.......\n..........#.......#.........#.......#.............................................................................................\n.......#...#.......................................#...................#..........................................................\n.......#........#............................#.....#.......#..............................................#..................#...#\n.....................#.................................#................#...#...............#.....#.#..........................#..\n.....#..........................................#......................#.............##..............................#........#...\n.....#......#.........................#..........................#..........................................................#..#..\n.#......#.............#......#.................................................#.....#...................##......#................\n.............................................................................##...................................................\n........................................#.......................................#........#.............................#.#.......#\n...........#.....................................#.............................#......#......#..#....................#..#.........\n.............................................#.......................#..................##....#.....#.............................\n........................#..............................................................#..........................................\n#..............#.........................#........#...#...............#..............#..................#....................##...\n...#...#.............................#.......#................................................#........................#....#.....\n.....#.........................#..#.......................#..........#.....................#..................#.#.....#...........\n..................#........#..#.......#...................................................................................#....#..\n..#...#...............................................#.#..#........................................................#....#......#.\n...............#...............#..........................#............................#..........#.............#.................\n............#.....#........#.........................................................#........#........#.....#........#......#....\n............................#.........#....#........#.....#...#.............#.................................#...................\n........#..................#....#.." <> ...}
23```
24
25```elixir
26map =
27 puzzle_input
28 |> String.split("\n", trim: true)
29 |> Enum.with_index()
30 |> Enum.flat_map(fn {row, y} ->
31 row
32 |> String.to_charlist()
33 |> Enum.with_index()
34 |> Enum.map(fn {v, x} -> {{y, x}, v} end)
35 end)
36 |> Map.new()
37```
38
39<!-- livebook:{"output":true} -->
40
41```
42%{
43 {18, 103} => 46,
44 {76, 13} => 46,
45 {61, 121} => 46,
46 {37, 47} => 46,
47 {65, 63} => 46,
48 {77, 129} => 46,
49 {120, 47} => 46,
50 {38, 2} => 46,
51 {1, 26} => 46,
52 {116, 69} => 46,
53 {124, 56} => 46,
54 {83, 76} => 46,
55 {117, 125} => 46,
56 {32, 15} => 46,
57 {103, 106} => 46,
58 {30, 113} => 46,
59 {123, 104} => 46,
60 {124, 60} => 46,
61 {89, 14} => 46,
62 {35, 30} => 46,
63 {37, 53} => 46,
64 {4, 5} => 46,
65 {8, 50} => 46,
66 {78, 98} => 46,
67 {101, 62} => 46,
68 {95, 56} => 46,
69 {74, 12} => 46,
70 {102, 74} => 46,
71 {11, 39} => 46,
72 {65, 43} => 46,
73 {22, 38} => 46,
74 {14, 86} => 46,
75 {49, 117} => 46,
76 {20, 41} => 46,
77 {29, 25} => 46,
78 {86, 10} => 46,
79 {83, 36} => 46,
80 {29, 26} => 46,
81 {47, 27} => 46,
82 {4, 81} => 46,
83 {31, 42} => 46,
84 {9, 34} => 46,
85 {13, 124} => 46,
86 {90, 0} => 46,
87 {14, 122} => 46,
88 {120, 42} => 46,
89 {121, 77} => 46,
90 {103, 39} => 46,
91 {102, ...} => 46,
92 {...} => 46,
93 ...
94}
95```
96
97```elixir
98{start_pos, _} = start = Enum.find(map, fn {_pos, v} -> v in ~c'^>v<' end)
99
100map = Map.replace(map, start_pos, ?.)
101
102start
103```
104
105<!-- livebook:{"output":true} -->
106
107```
108{{93, 71}, 94}
109```
110
111```elixir
112defmodule Guard do
113 def walk(map, start),
114 do: walk(map, start, MapSet.new())
115
116 defp walk(map, {pos, dir} = curr, visited) do
117 if curr in visited do
118 :loop
119 else
120 visited = MapSet.put(visited, curr)
121
122 case Map.fetch(map, next(pos, dir)) do
123 {:ok, ?#} ->
124 walk(map, {pos, turn(dir)}, visited)
125
126 {:ok, ?.} ->
127 walk(map, {next(pos, dir), dir}, visited)
128
129 :error ->
130 MapSet.new(visited, fn {pos, _} -> pos end)
131 end
132 end
133 end
134
135 defp next({y, x}, ?^), do: {y - 1, x}
136 defp next({y, x}, ?>), do: {y, x + 1}
137 defp next({y, x}, ?v), do: {y + 1, x}
138 defp next({y, x}, ?<), do: {y, x - 1}
139
140 defp turn(?^), do: ?>
141 defp turn(?>), do: ?v
142 defp turn(?v), do: ?<
143 defp turn(?<), do: ?^
144end
145```
146
147<!-- livebook:{"output":true} -->
148
149```
150{:module, Guard, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:turn, 1}}
151```
152
153## Part 1
154
155```elixir
156orig_path = Guard.walk(map, start)
157
158MapSet.size(orig_path)
159```
160
161<!-- livebook:{"output":true} -->
162
163```
1644433
165```
166
167## Part 2
168
169```elixir
170orig_path
171|> MapSet.delete(start_pos)
172|> Task.async_stream(fn point ->
173 map
174 |> Map.put(point, ?#)
175 |> Guard.walk(start)
176 |> Kernel.==(:loop)
177 end, ordered: false)
178|> Enum.count(& &1)
179```
180
181<!-- livebook:{"output":true} -->
182
183```
1844432
185```
186
187<!-- livebook:{"offset":7376,"stamp":{"token":"XCP.Bv-aM7sns5Kb69SQdY4giDFl6fWiJ5L3lnISxUUjGi_DgTgM_r5rl-y8sy2RLl-KbEtOmW99QQsGItLYJhKJmkOZ5pPZ_7Aw3BhkL_ap8RnW8UkOt_OsNdpqoZT7FEDtmFQ","version":2}} -->