this repo has no description
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

ft: add day 08.2022 and day 09.2022

+517 -1
+1 -1
2022/day07.livemd
··· 284 284 285 285 ```elixir 286 286 max = 70_000_000 287 - current = 46_592_386 287 + {current, _} = fs 288 288 needed = 30_000_000 289 289 290 290 left =
+313
2022/day08.livemd
··· 1 + <!-- livebook:{"persist_outputs":true} --> 2 + 3 + # Day 08 4 + 5 + ```elixir 6 + Mix.install([ 7 + {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"} 8 + ]) 9 + ``` 10 + 11 + <!-- livebook:{"output":true} --> 12 + 13 + ``` 14 + :ok 15 + ``` 16 + 17 + ## Section 18 + 19 + <!-- livebook:{"attrs":{"day":"8","session_secret":"ADVENT_OF_CODE_SESSION","variable":"puzzle_input","year":"2022"},"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} --> 20 + 21 + ```elixir 22 + {:ok, puzzle_input} = 23 + KinoAOC.download_puzzle("2022", "8", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION")) 24 + ``` 25 + 26 + <!-- livebook:{"output":true} --> 27 + 28 + ``` 29 + {:ok, 30 + "404310113342042430523206553054613026452223317713352040314520532034136044035422242052404520323121423\n120022131035104020413264056552251024207112052153646716635163015540126546334604634352130343204203212\n114444043404350333465355412140536532335724647116565610572546474626506052303052063065052530351012030\n212240414554223531015555065165021520410363021033163304514155663733352116600665403462501544352500033\n324144130111224105125305614015103771206061460425505011635353313773273343325122626415203015344311232\n113245430241150240464126012614135430370003075735010164413125456766205302613425655024334411031155403\n441245002130523346005210411662277072426351216071200446765701447237330324567025204602342520044502121\n214045553122025654652221354616163561012500762010384874262074144065234401637722263412266464251452314\n420554052436551160102504245073471242400253801258004372606111667766236046654353552356241642235535400\n230444034403653045355316273427144521155460537330375254634265217831710133466511570022515061342332314\n215330305636623403455371527067601715250678103470620507380025232122252031461570076036342312211025412\n152531154664324333510065716166433534816054420634864077383448250450766671674224724566465113516230555\n214332343546262452365454705617472281546633211772075171047360414071436550632720021764434561305210301\n111443005611044613477737043746056557178022072372401206038184673647422320773366100676006211551510212\n133313521334520014277727658110473505777283286103991788280475205774027243280366061162025220015443455\n155404252050544375401645717463634684248661783289138494563725362778686756122861113531553244022156404\n101264441110131756443257557872335606716942314559648922296471179061870856246756574324520340610552502\n552345216103355364312158072306454410946149736569151493634487672710443362287688034427626033035266435\n032116136355447236216431376728258285654613995348237354962282338329733408310145842020562245054642441\n224363655451243141363087456580326751652471257429153117112342588642859256252141665722503521125022565\n203656402177710126565314466702484652152875481365818345352121487714962821315157703464343750046056642\n556042122224622324270101114885994351144861589211656115175234527658398149331284812500044434453264665\n333402317273401421323318415855844499641764213948757537893227517242385296970317342824206711503004500\n341045445002443367084450562962259886574728664877676766689484955395194385797583863611332250124025511\n530441155216276525375870173389743919985448259629562325456237285529598688811050240824703721074602060\n320021441136216827813607342916845194696625696785568454728524752386544154132864251748117643644612016\n356236015127477502168551236473444979246675949449587984562945859468987964451443376640177554302403555\n521032161450418348764563917129159623593365988455562286236885389659621188427923285451742156213351116\n161327255665606701821864175493119539272489637754729983857664245665796727199928145136462257620362465\n324256011240774073378929732467468357478524626995373968674237767743387885552216853467388044247756643\n342275312602047038565615877434724873572784389557789777857693948752846554139723438258054200025014665\n051242516637708146638198373275222758924486548779744574439965559638384373152521363134036477637737533\n643152555635778437871298236855485688298578349488385645847564753765967563925377954286572876073532255\n052252355510464443878597562686999366353446874577654743895687373779864325297372128203202721414431575\n003626454683627045725543215594452394956436955758673339474559874959389629573279978978206522053161354\n627516237702078544682942119397343558584845947957956986767739349699889797692213624665730454802067156\n200457151510133551651137637224323537736454384779467644967339586954746668462789875763614758041306350\n335154370506881211537946276895249658859666595888979499974679997597784647296794417997582782381025574\n417013603706282285595672445598476844553485594594694865577988599545878778554434654896767653370600163\n151076753567166396584462282676939868463579768879699478585698835334464668886671938943507604626741055\n316424603540766436474549593426596456855354984858479546978868766769885893482223982736645038024543" <> ...} 31 + ``` 32 + 33 + ```elixir 34 + surroundings = %{top: 0, lft: 0, rig: 0, bot: 0} 35 + 36 + # puzzle_input =""" 37 + # 30373 38 + # 25512 39 + # 65332 40 + # 33549 41 + # 35390 42 + # """ 43 + 44 + raw_map = 45 + puzzle_input 46 + |> String.split("\n", trim: true) 47 + |> Enum.with_index() 48 + |> Enum.flat_map(fn {line, row} -> 49 + line 50 + |> String.to_charlist() 51 + |> Enum.with_index() 52 + |> Enum.map(fn {v, col} -> {{row, col}, v - ?0} end) 53 + end) 54 + |> Map.new() 55 + 56 + {height, width} = Enum.max(Map.keys(raw_map)) 57 + ``` 58 + 59 + <!-- livebook:{"output":true} --> 60 + 61 + ``` 62 + {98, 98} 63 + ``` 64 + 65 + ## Task 1 66 + 67 + ```elixir 68 + defmodule Task1 do 69 + def get_max(map, where, p) do 70 + case map[pos(where, p)] do 71 + nil -> -1 72 + {curr, %{^where => max}} -> max(curr, max) 73 + curr when is_integer(curr) -> curr 74 + end 75 + end 76 + 77 + defp pos(:top, {x, y}), do: {x - 1, y} 78 + defp pos(:bot, {x, y}), do: {x + 1, y} 79 + defp pos(:lft, {x, y}), do: {x, y - 1} 80 + defp pos(:rig, {x, y}), do: {x, y + 1} 81 + end 82 + ``` 83 + 84 + <!-- livebook:{"output":true} --> 85 + 86 + ``` 87 + {:module, Task1, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:pos, 2}} 88 + ``` 89 + 90 + ```elixir 91 + lt_map = 92 + for x <- 0..height, 93 + y <- 0..width, 94 + reduce: raw_map do 95 + map -> 96 + v = 97 + case map[{x, y}] do 98 + {v, _} -> v 99 + v -> v 100 + end 101 + 102 + top = Task1.get_max(map, :top, {x, y}) 103 + lft = Task1.get_max(map, :lft, {x, y}) 104 + 105 + %{ 106 + map 107 + | {x, y} => 108 + {v, 109 + %{ 110 + top: top, 111 + bot: -1, 112 + lft: lft, 113 + rig: -1 114 + }} 115 + } 116 + end 117 + 118 + map = 119 + for x <- height..0//-1, 120 + y <- width..0//-1, 121 + reduce: lt_map do 122 + map -> 123 + {v, maxes} = map[{x, y}] 124 + 125 + bot = Task1.get_max(map, :bot, {x, y}) 126 + rig = Task1.get_max(map, :rig, {x, y}) 127 + 128 + %{ 129 + map 130 + | {x, y} => {v, %{maxes | bot: bot, rig: rig}} 131 + } 132 + end 133 + ``` 134 + 135 + <!-- livebook:{"output":true} --> 136 + 137 + ``` 138 + %{ 139 + {76, 13} => {7, %{bot: 7, lft: 7, rig: 9, top: 8}}, 140 + {37, 47} => {8, %{bot: 9, lft: 9, rig: 9, top: 9}}, 141 + {65, 63} => {4, %{bot: 9, lft: 9, rig: 9, top: 9}}, 142 + {38, 2} => {7, %{bot: 7, lft: 4, rig: 9, top: 7}}, 143 + {1, 26} => {6, %{bot: 9, lft: 6, rig: 7, top: 3}}, 144 + {83, 76} => {8, %{bot: 7, lft: 9, rig: 7, top: 9}}, 145 + {32, 15} => {4, %{bot: 8, lft: 8, rig: 9, top: 8}}, 146 + {89, 14} => {0, %{bot: 4, lft: 6, rig: 8, top: 8}}, 147 + {35, 30} => {3, %{bot: 9, lft: 9, rig: 9, top: 9}}, 148 + {37, 53} => {9, %{bot: 9, lft: 9, rig: 9, top: 9}}, 149 + {4, 5} => {4, %{bot: 7, lft: 4, rig: 7, top: 4}}, 150 + {8, 50} => {4, %{bot: 9, lft: 8, rig: 7, top: 6}}, 151 + {78, 98} => {6, %{bot: 6, lft: 9, rig: -1, top: 7}}, 152 + {95, 56} => {5, %{bot: 5, lft: 7, rig: 7, top: 9}}, 153 + {74, 12} => {1, %{bot: 6, lft: 7, rig: 9, top: 8}}, 154 + {11, 39} => {0, %{bot: 9, lft: 8, rig: 8, top: 7}}, 155 + {65, 43} => {6, %{bot: 9, lft: 9, rig: 9, top: 9}}, 156 + {22, 38} => {1, %{bot: 9, lft: 9, rig: 9, top: 8}}, 157 + {14, 86} => {5, %{bot: 8, lft: 9, rig: 5, top: 6}}, 158 + {20, 41} => {5, %{bot: 9, lft: 8, rig: 9, top: 9}}, 159 + {29, 25} => {3, %{bot: 9, lft: 9, rig: 9, top: 7}}, 160 + {86, 10} => {3, %{bot: 5, lft: 6, rig: 8, top: 8}}, 161 + {83, 36} => {5, %{bot: 8, lft: 7, rig: 9, top: 9}}, 162 + {29, 26} => {2, %{bot: 9, lft: 9, rig: 9, top: 8}}, 163 + {47, 27} => {3, %{bot: 9, lft: 9, rig: 9, top: 9}}, 164 + {4, 81} => {4, %{bot: 9, lft: 7, rig: 5, top: 4}}, 165 + {31, 42} => {5, %{bot: 9, lft: 9, rig: 9, top: 9}}, 166 + {9, 34} => {2, %{bot: 9, lft: 7, rig: 8, top: 7}}, 167 + {90, 0} => {4, %{bot: 4, lft: -1, rig: 8, top: 7}}, 168 + {67, 98} => {1, %{bot: 6, lft: 9, rig: -1, top: 7}}, 169 + {13, 85} => {0, %{bot: 9, lft: 8, rig: 6, top: 6}}, 170 + {63, 81} => {9, %{bot: 7, lft: 9, rig: 8, top: 9}}, 171 + {82, 60} => {6, %{bot: 8, lft: 9, rig: 9, top: 9}}, 172 + {47, 38} => {4, %{bot: 9, lft: 9, rig: 9, top: 9}}, 173 + {15, 92} => {2, %{bot: 7, lft: 9, rig: 6, top: 6}}, 174 + {58, 58} => {8, %{bot: 9, lft: 9, rig: 9, top: 9}}, 175 + {20, 3} => {6, %{bot: 7, lft: 3, rig: 9, top: 5}}, 176 + {61, 95} => {3, %{bot: 7, lft: 9, rig: 7, top: 7}}, 177 + {23, 67} => {9, %{bot: 9, lft: 9, rig: 9, top: 9}}, 178 + {78, 75} => {1, %{bot: 6, lft: 9, rig: 7, top: 9}}, 179 + {79, 17} => {6, %{bot: 6, lft: 7, rig: 9, top: 9}}, 180 + {75, 0} => {5, %{bot: 6, lft: -1, rig: 9, top: 7}}, 181 + {16, 73} => {4, %{bot: 9, lft: 9, rig: 7, top: 8}}, 182 + {76, 2} => {3, %{bot: 6, lft: 4, rig: 9, top: 7}}, 183 + {58, 84} => {9, %{bot: 8, lft: 9, rig: 6, ...}}, 184 + {58, 33} => {6, %{bot: 9, lft: 9, ...}}, 185 + {47, 44} => {5, %{bot: 9, ...}}, 186 + {54, 31} => {9, %{...}}, 187 + {13, ...} => {3, ...}, 188 + {...} => {...}, 189 + ... 190 + } 191 + ``` 192 + 193 + ```elixir 194 + map 195 + |> Enum.count(fn {_, {v, maxes}} -> 196 + maxes 197 + |> Map.values() 198 + |> Enum.any?(&(v > &1)) 199 + end) 200 + ``` 201 + 202 + <!-- livebook:{"output":true} --> 203 + 204 + ``` 205 + 1538 206 + ``` 207 + 208 + ## Task 2 209 + 210 + ```elixir 211 + defmodule Task2 do 212 + def range(range, value) do 213 + Map.new(range, &{&1, value}) 214 + end 215 + end 216 + ``` 217 + 218 + <!-- livebook:{"output":true} --> 219 + 220 + ``` 221 + {:module, Task2, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:range, 2}} 222 + ``` 223 + 224 + ```elixir 225 + empty = Task2.range(0..9, 0) 226 + 227 + l_map = 228 + Enum.flat_map(0..width, fn x -> 229 + {list, _} = 230 + Enum.map_reduce(0..height, empty, fn y, acc -> 231 + # IO.inspect({{x, y}, acc}) 232 + v = raw_map[{x, y}] 233 + dist = y - acc[v] 234 + new_acc = Map.merge(acc, Task2.range(0..v, y)) 235 + 236 + {{{x, y}, dist}, new_acc} 237 + end) 238 + 239 + list 240 + end) 241 + |> Map.new() 242 + 243 + t_map = 244 + Enum.flat_map(0..width, fn y -> 245 + {list, _} = 246 + Enum.map_reduce(0..height, empty, fn x, acc -> 247 + v = raw_map[{x, y}] 248 + dist = x - acc[v] 249 + new_acc = Map.merge(acc, Task2.range(0..v, x)) 250 + 251 + {{{x, y}, dist}, new_acc} 252 + end) 253 + 254 + list 255 + end) 256 + |> Map.new() 257 + 258 + empty = Task2.range(0..9, width) 259 + 260 + r_map = 261 + Enum.flat_map(height..0//-1, fn x -> 262 + {list, _} = 263 + Enum.map_reduce(width..0//-1, empty, fn y, acc -> 264 + v = raw_map[{x, y}] 265 + dist = acc[v] - y 266 + new_acc = Map.merge(acc, Task2.range(0..v, y)) 267 + 268 + {{{x, y}, dist}, new_acc} 269 + end) 270 + 271 + list 272 + end) 273 + |> Map.new() 274 + 275 + b_map = 276 + Enum.flat_map(width..0//-1, fn y -> 277 + {list, _} = 278 + Enum.map_reduce(height..0//-1, empty, fn x, acc -> 279 + v = raw_map[{x, y}] 280 + dist = acc[v] - x 281 + new_acc = Map.merge(acc, Task2.range(0..v, x)) 282 + 283 + {{{x, y}, dist}, new_acc} 284 + end) 285 + 286 + list 287 + end) 288 + |> Map.new() 289 + 290 + merger = fn _, a, b -> a * b end 291 + 292 + l_map 293 + |> Map.merge(t_map, merger) 294 + |> Map.merge(r_map, merger) 295 + |> Map.merge(b_map, merger) 296 + |> Enum.max_by(&elem(&1, 1)) 297 + ``` 298 + 299 + <!-- livebook:{"output":true} --> 300 + 301 + ``` 302 + {{9, 63}, 496125} 303 + ``` 304 + 305 + ```elixir 306 + {l_map[{3, 2}], t_map[{3, 2}], b_map[{3, 2}], r_map[{3, 2}]} 307 + ``` 308 + 309 + <!-- livebook:{"output":true} --> 310 + 311 + ``` 312 + {2, 1, 1, 1} 313 + ```
+203
2022/day09.livemd
··· 1 + <!-- livebook:{"persist_outputs":true} --> 2 + 3 + # Day 09 4 + 5 + ```elixir 6 + Mix.install([ 7 + {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"} 8 + ]) 9 + ``` 10 + 11 + <!-- livebook:{"output":true} --> 12 + 13 + ``` 14 + :ok 15 + ``` 16 + 17 + ## Setup 18 + 19 + <!-- livebook:{"attrs":{"day":"9","session_secret":"ADVENT_OF_CODE_SESSION","variable":"puzzle_input","year":"2022"},"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} --> 20 + 21 + ```elixir 22 + {:ok, puzzle_input} = 23 + KinoAOC.download_puzzle("2022", "9", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION")) 24 + ``` 25 + 26 + <!-- livebook:{"output":true} --> 27 + 28 + ``` 29 + {:ok, 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+ 33 + ```elixir 34 + moves = 35 + puzzle_input 36 + |> String.split("\n", trim: true) 37 + |> Enum.map(fn line -> 38 + [dir, steps] = String.split(line) 39 + 40 + {dir, String.to_integer(steps)} 41 + end) 42 + ``` 43 + 44 + <!-- livebook:{"output":true} --> 45 + 46 + ``` 47 + [ 48 + {"L", 1}, 49 + {"R", 1}, 50 + {"L", 1}, 51 + {"U", 1}, 52 + {"R", 2}, 53 + {"U", 1}, 54 + {"D", 2}, 55 + {"R", 2}, 56 + {"U", 1}, 57 + {"D", 2}, 58 + {"U", 2}, 59 + {"L", 2}, 60 + {"D", 1}, 61 + {"U", 1}, 62 + {"D", 2}, 63 + {"L", 2}, 64 + {"D", 1}, 65 + {"L", 2}, 66 + {"D", 2}, 67 + {"L", 1}, 68 + {"U", 2}, 69 + {"R", 2}, 70 + {"L", 1}, 71 + {"D", 2}, 72 + {"L", 2}, 73 + {"R", 2}, 74 + {"D", 2}, 75 + {"L", 1}, 76 + {"U", 1}, 77 + {"R", 1}, 78 + {"U", 1}, 79 + {"L", 1}, 80 + {"D", 1}, 81 + {"R", 1}, 82 + {"U", 2}, 83 + {"D", 2}, 84 + {"R", 1}, 85 + {"U", 1}, 86 + {"R", 1}, 87 + {"L", 1}, 88 + {"U", 1}, 89 + {"R", 1}, 90 + {"D", 1}, 91 + {"L", 1}, 92 + {"U", 1}, 93 + {"R", 1}, 94 + {"D", 1}, 95 + {"U", 1}, 96 + {"D", ...}, 97 + {...}, 98 + ... 99 + ] 100 + ``` 101 + 102 + ```elixir 103 + defmodule Rope do 104 + @start %{x: 0, y: 0} 105 + 106 + defstruct segments: [%{x: 0, y: 0}, %{x: 0, y: 0}] 107 + 108 + def new(length), 109 + do: %__MODULE__{segments: List.duplicate(@start, length + 1)} 110 + 111 + def last(%__MODULE__{segments: list}), do: List.last(list) 112 + 113 + def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, dir) do 114 + head = 115 + case dir do 116 + "U" -> %{x: x + 1, y: y} 117 + "D" -> %{x: x - 1, y: y} 118 + "L" -> %{x: x, y: y + 1} 119 + "R" -> %{x: x, y: y - 1} 120 + end 121 + 122 + segments = move_tails([head | tails]) 123 + 124 + %__MODULE__{segments: segments} 125 + end 126 + 127 + defp move_tails([]), do: [] 128 + defp move_tails([head]), do: [head] 129 + 130 + # When nothing else moves, then stop 131 + defp move_tails([head, tail | _] = rope) 132 + when abs(head.x - tail.x) < 2 and abs(head.y - tail.y) < 2, 133 + do: rope 134 + 135 + defp move_tails([head, tail | rest]) 136 + when abs(head.y - tail.y) < 2, 137 + do: [head | move_tails([%{x: head.x - sgn(head.x - tail.x), y: head.y} | rest])] 138 + 139 + defp move_tails([head, tail | rest]) 140 + when abs(head.x - tail.x) < 2, 141 + do: [head | move_tails([%{x: head.x, y: head.y - sgn(head.y - tail.y)} | rest])] 142 + 143 + defp move_tails([head, tail | rest]), 144 + do: [ 145 + head 146 + | move_tails([%{x: head.x - sgn(head.x - tail.x), y: head.y - sgn(head.y - tail.y)} | rest]) 147 + ] 148 + 149 + def sgn(0), do: 0 150 + def sgn(n) when n < 0, do: -1 151 + def sgn(_), do: 1 152 + end 153 + ``` 154 + 155 + <!-- livebook:{"output":true} --> 156 + 157 + ``` 158 + {:module, Rope, <<70, 79, 82, 49, 0, 0, 21, ...>>, {:sgn, 1}} 159 + ``` 160 + 161 + ```elixir 162 + rope = Rope.new(1) 163 + 164 + {_rope, tail_positions} = 165 + moves 166 + |> Enum.flat_map(fn {dir, steps} -> List.duplicate(dir, steps) end) 167 + |> Enum.reduce({rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} -> 168 + new_rope = Rope.move(rope, dir) 169 + 170 + {new_rope, MapSet.put(acc, Rope.last(new_rope))} 171 + end) 172 + 173 + MapSet.size(tail_positions) 174 + ``` 175 + 176 + <!-- livebook:{"output":true} --> 177 + 178 + ``` 179 + 5960 180 + ``` 181 + 182 + ## Task 2 183 + 184 + ```elixir 185 + rope = Rope.new(9) 186 + 187 + {_rope, tail_positions} = 188 + moves 189 + |> Enum.flat_map(fn {dir, steps} -> List.duplicate(dir, steps) end) 190 + |> Enum.reduce({rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} -> 191 + new_rope = Rope.move(rope, dir) 192 + 193 + {new_rope, MapSet.put(acc, Rope.last(new_rope))} 194 + end) 195 + 196 + MapSet.size(tail_positions) 197 + ``` 198 + 199 + <!-- livebook:{"output":true} --> 200 + 201 + ``` 202 + 2327 203 + ```