this repo has no description
1<!-- livebook:{"persist_outputs":true} -->
2
3# Day 18
4
5```elixir
6Mix.install([:kino_aoc])
7```
8
9## Section
10
11<!-- livebook:{"attrs":{"assign_to":"puzzle_input","day":"18","session_secret":"ADVENT_OF_CODE_SESSION","year":"2023"},"chunks":null,"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} -->
12
13```elixir
14{:ok, puzzle_input} =
15 KinoAOC.download_puzzle("2023", "18", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
16```
17
18<!-- livebook:{"output":true} -->
19
20```
21{:ok,
22 "L 5 (#0760b2)\nU 4 (#058b53)\nL 3 (#d05a12)\nD 4 (#058b51)\nL 6 (#12ee82)\nU 6 (#63c403)\nL 4 (#065a12)\nU 8 (#a44373)\nL 7 (#7581c2)\nU 7 (#334ec3)\nL 3 (#35e792)\nU 5 (#079963)\nL 3 (#1737b0)\nU 4 (#29a7a3)\nL 2 (#705a02)\nU 5 (#7151e3)\nL 4 (#705a00)\nU 4 (#43f5a3)\nL 5 (#1737b2)\nU 8 (#61a8c3)\nL 5 (#6005d2)\nD 3 (#20f4c1)\nL 3 (#4aa812)\nD 3 (#8163b1)\nL 4 (#80ac22)\nD 4 (#5e64c1)\nL 7 (#cb5430)\nD 5 (#7ac2d1)\nR 7 (#920132)\nD 6 (#a44371)\nL 6 (#944582)\nU 3 (#bc3423)\nL 5 (#71c1b2)\nU 4 (#88e6f3)\nL 4 (#0e35f2)\nU 5 (#11fa31)\nL 8 (#6ab312)\nU 5 (#5c3df1)\nR 8 (#40be22)\nU 3 (#5c3df3)\nR 9 (#42e412)\nU 6 (#11fa33)\nR 5 (#178f02)\nU 10 (#88e6f1)\nL 2 (#cff5f2)\nU 6 (#41fb73)\nL 5 (#247782)\nU 4 (#8d6d73)\nL 5 (#139492)\nU 8 (#6360d3)\nR 4 (#1688c2)\nU 2 (#28eb43)\nR 5 (#7d6230)\nU 6 (#769093)\nR 3 (#7d6232)\nU 5 (#487383)\nL 5 (#4fe4c2)\nU 4 (#784971)\nL 7 (#649a42)\nU 3 (#81c8d1)\nL 6 (#3e8422)\nU 5 (#2e26f3)\nL 5 (#880bd2)\nU 4 (#cbeb53)\nL 6 (#3c3a82)\nU 3 (#990203)\nL 8 (#18ce92)\nU 6 (#3bd723)\nL 10 (#8b17c2)\nU 5 (#931f03)\nR 7 (#56b4b2)\nU 3 (#1c3d03)\nR 2 (#693490)\nU 7 (#a5d813)\nL 4 (#693492)\nU 7 (#454523)\nR 8 (#749e20)\nU 3 (#b6ecd3)\nL 8 (#1e69b0)\nU 6 (#0cd813)\nR 4 (#a286d0)\nU 8 (#8249a3)\nR 6 (#c431e0)\nU 10 (#64b5d3)\nR 3 (#26fee0)\nU 7 (#3a6403)\nR 7 (#44c0a2)\nU 8 (#574291)\nR 2 (#9bf502)\nU 5 (#574293)\nR 5 (#0a7b22)\nU 8 (#12e553)\nR 2 (#67c0e0)\nU 3 (#ab6f63)\nR 7 (#922b60)\nD 6 (#680283)\nR 5 (#637c90)\nD 5 (#b10343)\nR 3 (#954ed0)\nD 8 (#254333)\nL 8 (#3f16a0)\nD 3 (#55f2c3)\nR 4 (#2465e0)\nD 9 (#6b7461)\nR 6 (#8b2b00)\nD 7 (#6b7463)\nR 5 (#7ae9f0)\nD 3 (#584e33)\nR 3 (#4fd2d2)\nD 4 (#30e451)\nR 3 (#682c22)\nD 3 (#30e453)\nR 4 (#727be2)\nD 8 (#85b173)\nL 4 (#3a8c50)\nD 3 (#004a23)\nR 2 (#6eef60)\nD 3 (#92b8d3)\nR 5 (#bdb140)\nU 8 (#8500b3)\nR 5 (#2e42f2)\nD 8 (#531953)\nR 3 (#9358a2)\nD 5 (#14d4b3)\nR 6 (#b19aa2)\nD 8 (#14d4b1)\nR 8 (#3e0d32)\nU 8 (#274023)\nR 3 (#b2d1a2)\nU 3 (#274021)\nR 8 (#206d02)\nU 6 (#2d02d3)\nR 4 (#0bd782)\nU 3 (#483403)\nR 9 (#7d6fc0)\nU 5 (#717513)\nR 9 (#5e63a0)\nU 6 (#614071)\nR 6 (#6df500)\nU 9 (#6f14e1)\nR 5 (#9229d0)\nU 5 (#6f14e3)\nL 5 (#0993b0)\nU 4 (#614073)\nL 3 (#aad3a0)\nU 7 (#2aed13)\nL 7 (#117f30)\nU 8 (#b185d3)\nL 3 (#a39640)\nU 10 (#a408b1)\nL 5 (#1e31b0)\nU 4 (#5af491)\nL 3 (#43ff50)\nU 4 (#125121)\nR 5 (#2321b0)\nU 4 (#4a6de1)\nR 6 (#80c430)\nU 5 (#4a6de3)\nL 5 (#8fe3b0)\nU 4 (#483d91)\nR 8 (#012050)\nU 3 (#3730e1)\nR 5 (#d36d12)\nU 8 (#7d5761)\nR 7 (#d36d10)\nU 7 (#21b1c1)\nR 9 (#8ea5e2)\nU 7 (#602a01)\nR 8 (#aae3f2)\nU 5 (#8d2981)\nR 5 (#263f72)\nU 5 (#2a3471)\nR 7 (#263f70)\nU 2 (#88ea21)\nR 3 (#3f5f62)\nU 6 (#5e1cc1)\nR 5 (#adffb0)\nU 6 (#104941)\nR 5 (#073230)\nU 4 (#25be03)\nL 9 (#1a2fd0)\nU 5 (#3d1233)\nR 9 (#879540)\nU 4 (#5c9671)\nL 2 (#6ad9e0)\nU 8 (#5c9673)\nL 7 (#3bb750)\nU 3 (#3d1231)\nR 11 (#080ca0)\nU 4 (#25be01)\nR 4 (#50a100)\nU 5 (#163541)\nL 5 (#27ff80)\nU 3 (#ab8481)\nR 4 (#549890)\nU 7 (#010511)\nL 4 (#749c72)\nU 4 (#46cfe1)\nL 5 (#a03822)\nD 5 (#594361)\nL 2 (#12a9f0)\nD 9 (#509f91)\nL 3 (#c051f0)\nU 3 (#509f93)\nL 7 (#41d8b0)\nD 3 (#80af51)\nL 3 (#549892)\nD 9 (#29c701)\nL 5 (#58fe00)\nD 5 (#4ef833)\nR 5 (#4ad860)\nD 7 (#aa4b13)\nL 7 (#6d6810)\nD 8 (#774e21)\nL 6 (#631140)\nU 8 (#4c9b31)\nL 4 (#4a8a50)\nD 3 (#61b9d1)\nL 6 (#665fd0)\nD 5 (#4edd51)\nR 6 (#46f5d0)\nD 3 (#109bd1)\nL 6 (#896f50)\nD 5 (#3ab8e1)\nL 3 (#6f6ce0)\nD 4 (#5acd11)\nL 5 (#1d15b2)\nU 4 (#032e23)\nL 10 (#8d6c92)\nU 3 (#032e21)\nR 10 (#4e59f2)\nU 5 (#70a541)\nL 5 (#1102a0)\nU 8 (#8b66f1)\nL 5 (#871710)\nU 10 (#be4e51)\nR 3 (#871712)\nU 11 (#1182c1)\nR 3 (#1102a2)\nD 9 (#493441)\nR 8 (#7a6b80)\nD 4 (#c3daa1)\nR 3 (#8a38d0)\nU 3 (#07ac71)\nR 4 (#131b90)\nU 5 (#34c6e1)\nR 4 (#914cf2)\nU 5 (#0e9af1)\nR 3 (#6c62d2)\nU 3 (#3af801)\nL 6 (#0018c0)\nU 7 (#2b7361)\nL 4 (#55fb20)\nU 5 (#8a33c1)\nR 4 (#a79be0)\nU 4 (#59acf1)\nR 7 (#bb8240)\nD 5 (#77ca71)\nR 7 (#894bd0)\nU 5 (#a12ff1)\nR 3 (#437630)\nD 4 (#1dbba1)\nR 10 (#4c87b0)\nU 6 (#9ea131)\nR 2 (#4c87b2)\nU 4 (#19f7e1)\nL 11 (#85c770)\nU 4 (#220631)\nL 9 (#224540)\nU 5 (#d4ac83)\nL 4 (#421610)\nU 3 (#23ae63)\nL 9 (#8259c0)\nD 4 (#1b4b51)\nR 6 (#b84e80)\nD 11 (#9b8e41)\nL 6 (#29c310)\nD 4 (#9cba93)\nL 6 (#2e33e0)\nU 10 (#1d8b03)\nL 5 (#2578e2)\nU 7 (#0c7911)\nL 2 (#6112e2)\nU 2 (#0c7913)\nL 7 (#621cb2)\nD 8 (#4164f3)\nL 4 (#080dd2)\nU 11 (#574773)\nR 7 (#6ab252)\nU 3 (#4224b3)\nR 3 (#898930)\nU 2 (#c8" <> ...}
23```
24
25```elixir
26# puzzle_input =
27"""
28R 6 (#70c710)
29D 5 (#0dc571)
30L 2 (#5713f0)
31D 2 (#d2c081)
32R 2 (#59c680)
33D 2 (#411b91)
34L 5 (#8ceee2)
35U 2 (#caa173)
36L 1 (#1b58a2)
37U 2 (#caa171)
38R 2 (#7807d2)
39U 3 (#a77fa3)
40L 2 (#015232)
41U 2 (#7a21e3)
42"""
43```
44
45<!-- livebook:{"output":true} -->
46
47```
48"R 6 (#70c710)\nD 5 (#0dc571)\nL 2 (#5713f0)\nD 2 (#d2c081)\nR 2 (#59c680)\nD 2 (#411b91)\nL 5 (#8ceee2)\nU 2 (#caa173)\nL 1 (#1b58a2)\nU 2 (#caa171)\nR 2 (#7807d2)\nU 3 (#a77fa3)\nL 2 (#015232)\nU 2 (#7a21e3)\n"
49```
50
51```elixir
52commands =
53 puzzle_input
54 |> String.split("\n", trim: true)
55 |> Enum.map(fn <<dir>> <> " " <> rest ->
56 {len, " (#" <> <<hex::binary-6>> <> ")"} = Integer.parse(rest)
57 {dir, len, hex}
58 end)
59```
60
61<!-- livebook:{"output":true} -->
62
63```
64[
65 {76, 5, "0760b2"},
66 {85, 4, "058b53"},
67 {76, 3, "d05a12"},
68 {68, 4, "058b51"},
69 {76, 6, "12ee82"},
70 {85, 6, "63c403"},
71 {76, 4, "065a12"},
72 {85, 8, "a44373"},
73 {76, 7, "7581c2"},
74 {85, 7, "334ec3"},
75 {76, 3, "35e792"},
76 {85, 5, "079963"},
77 {76, 3, "1737b0"},
78 {85, 4, "29a7a3"},
79 {76, 2, "705a02"},
80 {85, 5, "7151e3"},
81 {76, 4, "705a00"},
82 {85, 4, "43f5a3"},
83 {76, 5, "1737b2"},
84 {85, 8, "61a8c3"},
85 {76, 5, "6005d2"},
86 {68, 3, "20f4c1"},
87 {76, 3, "4aa812"},
88 {68, 3, "8163b1"},
89 {76, 4, "80ac22"},
90 {68, 4, "5e64c1"},
91 {76, 7, "cb5430"},
92 {68, 5, "7ac2d1"},
93 {82, 7, "920132"},
94 {68, 6, "a44371"},
95 {76, 6, "944582"},
96 {85, 3, "bc3423"},
97 {76, 5, "71c1b2"},
98 {85, 4, "88e6f3"},
99 {76, 4, "0e35f2"},
100 {85, 5, "11fa31"},
101 {76, 8, "6ab312"},
102 {85, 5, "5c3df1"},
103 {82, 8, "40be22"},
104 {85, 3, "5c3df3"},
105 {82, 9, "42e412"},
106 {85, 6, "11fa33"},
107 {82, 5, "178f02"},
108 {85, 10, "88e6f1"},
109 {76, 2, "cff5f2"},
110 {85, 6, "41fb73"},
111 {76, 5, "247782"},
112 {85, 4, ...},
113 {76, ...},
114 {...},
115 ...
116]
117```
118
119## Part 1
120
121```elixir
122dirs = %{
123 ?U => {0, -1},
124 ?D => {0, 1},
125 ?L => {-1, 0},
126 ?R => {1, 0}
127}
128
129{points, {_, circ}} =
130 Enum.map_reduce(commands, {{0, 0}, 0}, fn
131 {dir, d, _}, {{x, y}, l} ->
132 {dx, dy} = dirs[dir]
133 {{x, y}, {{x + dx * d, y + dy * d}, l + d}}
134 end)
135```
136
137<!-- livebook:{"output":true} -->
138
139```
140{[
141 {0, 0},
142 {-5, 0},
143 {-5, -4},
144 {-8, -4},
145 {-8, 0},
146 {-14, 0},
147 {-14, -6},
148 {-18, -6},
149 {-18, -14},
150 {-25, -14},
151 {-25, -21},
152 {-28, -21},
153 {-28, -26},
154 {-31, -26},
155 {-31, -30},
156 {-33, -30},
157 {-33, -35},
158 {-37, -35},
159 {-37, -39},
160 {-42, -39},
161 {-42, -47},
162 {-47, -47},
163 {-47, -44},
164 {-50, -44},
165 {-50, -41},
166 {-54, -41},
167 {-54, -37},
168 {-61, -37},
169 {-61, -32},
170 {-54, -32},
171 {-54, -26},
172 {-60, -26},
173 {-60, -29},
174 {-65, -29},
175 {-65, -33},
176 {-69, -33},
177 {-69, -38},
178 {-77, -38},
179 {-77, -43},
180 {-69, -43},
181 {-69, -46},
182 {-60, -46},
183 {-60, -52},
184 {-55, -52},
185 {-55, -62},
186 {-57, -62},
187 {-57, -68},
188 {-62, ...},
189 {...},
190 ...
191 ], {{0, 0}, 3518}}
192```
193
194```elixir
195area =
196 points
197 |> Enum.chunk_every(2, 1, [{0, 0}])
198 |> Enum.map(fn [{x1, y1}, {x2, y2}] ->
199 x1 * y2 - x2 * y1
200 end)
201 |> Enum.sum()
202 |> abs()
203 |> div(2)
204
205area + div(circ, 2) + 1
206```
207
208<!-- livebook:{"output":true} -->
209
210```
21149897
212```
213
214## Part 2
215
216```elixir
217dirs = %{
218 ?3 => {0, -1},
219 ?1 => {0, 1},
220 ?2 => {-1, 0},
221 ?0 => {1, 0}
222}
223
224{points, {_, circ}} =
225 Enum.map_reduce(commands, {{0, 0}, 0}, fn
226 {_, _, <<d::binary-5, dir>>}, {{x, y}, l} ->
227 {dx, dy} = dirs[dir]
228 d = String.to_integer(d, 16)
229 {{x, y}, {{x + dx * d, y + dy * d}, l + d}}
230 end)
231```
232
233<!-- livebook:{"output":true} -->
234
235```
236{[
237 {0, 0},
238 {-30219, 0},
239 {-30219, -22709},
240 {-883628, -22709},
241 {-883628, 0},
242 {-961172, 0},
243 {-961172, -408640},
244 {-987189, -408640},
245 {-987189, -1081463},
246 {-1468497, -1081463},
247 {-1468497, -1291619},
248 {-1689290, -1291619},
249 {-1689290, -1322745},
250 {-1594191, -1322745},
251 {-1594191, -1493363},
252 {-2054383, -1493363},
253 {-2054383, -1957521},
254 {-1594191, -1957521},
255 {-1594191, -2235883},
256 {-1689290, -2235883},
257 {-1689290, -2635895},
258 {-2082599, -2635895},
259 {-2082599, -2500907},
260 {-2388392, -2500907},
261 {-2388392, -1970928},
262 {-2915434, -1970928},
263 {-2915434, -1584292},
264 {-2082599, -1584292},
265 {-2082599, -1081463},
266 {-2680634, -1081463},
267 {-2680634, -408640},
268 {-3287954, -408640},
269 {-3287954, -1179522},
270 {-3753901, -1179522},
271 {-3753901, -1740273},
272 {-3812108, -1740273},
273 {-3812108, -1666638},
274 {-4249149, -1666638},
275 {-4249149, -1288815},
276 {-4514335, -1288815},
277 {-4514335, -1666638},
278 {-4788320, -1666638},
279 {-4788320, -1740273},
280 {-4884816, -1740273},
281 {-4884816, -1179522},
282 {-5736623, -1179522},
283 {-5736623, -1449785},
284 {-5885991, ...},
285 {...},
286 ...
287 ], {{0, 0}, 237488828}}
288```
289
290```elixir
291area =
292 points
293 |> Enum.chunk_every(2, 1, [{0, 0}])
294 |> Enum.map(fn [{x1, y1}, {x2, y2}] ->
295 x1 * y2 - x2 * y1
296 end)
297 |> Enum.sum()
298 |> abs()
299 |> div(2)
300
301area + div(circ, 2) + 1
302```
303
304<!-- livebook:{"output":true} -->
305
306```
307194033958221830
308```
309
310<!-- livebook:{"offset":9828,"stamp":{"token":"XCP.PxtVRuGOoJbRBg9li_V5edaJPAIM1giG8b7fnblLM45DtgF15Jzd6NlUMPxbzksg6acUzlzFjYTBEN2LSqNSG1-GyFTIi-SNpma9KQLCo3vK0DZWQEcIQo44tSdMuxRx5A","version":2}} -->