this repo has no description

ft: add day 08.2022 and day 09.2022

Changed files
+517 -1
2022
+1 -1
2022/day07.livemd
···
```elixir
max = 70_000_000
-
current = 46_592_386
+
{current, _} = fs
needed = 30_000_000
left =
+313
2022/day08.livemd
···
+
<!-- livebook:{"persist_outputs":true} -->
+
+
# Day 08
+
+
```elixir
+
Mix.install([
+
{:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}
+
])
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
:ok
+
```
+
+
## Section
+
+
<!-- livebook:{"attrs":{"day":"8","session_secret":"ADVENT_OF_CODE_SESSION","variable":"puzzle_input","year":"2022"},"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} -->
+
+
```elixir
+
{:ok, puzzle_input} =
+
KinoAOC.download_puzzle("2022", "8", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{:ok,
+
"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" <> ...}
+
```
+
+
```elixir
+
surroundings = %{top: 0, lft: 0, rig: 0, bot: 0}
+
+
# puzzle_input ="""
+
# 30373
+
# 25512
+
# 65332
+
# 33549
+
# 35390
+
# """
+
+
raw_map =
+
puzzle_input
+
|> String.split("\n", trim: true)
+
|> Enum.with_index()
+
|> Enum.flat_map(fn {line, row} ->
+
line
+
|> String.to_charlist()
+
|> Enum.with_index()
+
|> Enum.map(fn {v, col} -> {{row, col}, v - ?0} end)
+
end)
+
|> Map.new()
+
+
{height, width} = Enum.max(Map.keys(raw_map))
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{98, 98}
+
```
+
+
## Task 1
+
+
```elixir
+
defmodule Task1 do
+
def get_max(map, where, p) do
+
case map[pos(where, p)] do
+
nil -> -1
+
{curr, %{^where => max}} -> max(curr, max)
+
curr when is_integer(curr) -> curr
+
end
+
end
+
+
defp pos(:top, {x, y}), do: {x - 1, y}
+
defp pos(:bot, {x, y}), do: {x + 1, y}
+
defp pos(:lft, {x, y}), do: {x, y - 1}
+
defp pos(:rig, {x, y}), do: {x, y + 1}
+
end
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{:module, Task1, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:pos, 2}}
+
```
+
+
```elixir
+
lt_map =
+
for x <- 0..height,
+
y <- 0..width,
+
reduce: raw_map do
+
map ->
+
v =
+
case map[{x, y}] do
+
{v, _} -> v
+
v -> v
+
end
+
+
top = Task1.get_max(map, :top, {x, y})
+
lft = Task1.get_max(map, :lft, {x, y})
+
+
%{
+
map
+
| {x, y} =>
+
{v,
+
%{
+
top: top,
+
bot: -1,
+
lft: lft,
+
rig: -1
+
}}
+
}
+
end
+
+
map =
+
for x <- height..0//-1,
+
y <- width..0//-1,
+
reduce: lt_map do
+
map ->
+
{v, maxes} = map[{x, y}]
+
+
bot = Task1.get_max(map, :bot, {x, y})
+
rig = Task1.get_max(map, :rig, {x, y})
+
+
%{
+
map
+
| {x, y} => {v, %{maxes | bot: bot, rig: rig}}
+
}
+
end
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
%{
+
{76, 13} => {7, %{bot: 7, lft: 7, rig: 9, top: 8}},
+
{37, 47} => {8, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{65, 63} => {4, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{38, 2} => {7, %{bot: 7, lft: 4, rig: 9, top: 7}},
+
{1, 26} => {6, %{bot: 9, lft: 6, rig: 7, top: 3}},
+
{83, 76} => {8, %{bot: 7, lft: 9, rig: 7, top: 9}},
+
{32, 15} => {4, %{bot: 8, lft: 8, rig: 9, top: 8}},
+
{89, 14} => {0, %{bot: 4, lft: 6, rig: 8, top: 8}},
+
{35, 30} => {3, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{37, 53} => {9, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{4, 5} => {4, %{bot: 7, lft: 4, rig: 7, top: 4}},
+
{8, 50} => {4, %{bot: 9, lft: 8, rig: 7, top: 6}},
+
{78, 98} => {6, %{bot: 6, lft: 9, rig: -1, top: 7}},
+
{95, 56} => {5, %{bot: 5, lft: 7, rig: 7, top: 9}},
+
{74, 12} => {1, %{bot: 6, lft: 7, rig: 9, top: 8}},
+
{11, 39} => {0, %{bot: 9, lft: 8, rig: 8, top: 7}},
+
{65, 43} => {6, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{22, 38} => {1, %{bot: 9, lft: 9, rig: 9, top: 8}},
+
{14, 86} => {5, %{bot: 8, lft: 9, rig: 5, top: 6}},
+
{20, 41} => {5, %{bot: 9, lft: 8, rig: 9, top: 9}},
+
{29, 25} => {3, %{bot: 9, lft: 9, rig: 9, top: 7}},
+
{86, 10} => {3, %{bot: 5, lft: 6, rig: 8, top: 8}},
+
{83, 36} => {5, %{bot: 8, lft: 7, rig: 9, top: 9}},
+
{29, 26} => {2, %{bot: 9, lft: 9, rig: 9, top: 8}},
+
{47, 27} => {3, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{4, 81} => {4, %{bot: 9, lft: 7, rig: 5, top: 4}},
+
{31, 42} => {5, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{9, 34} => {2, %{bot: 9, lft: 7, rig: 8, top: 7}},
+
{90, 0} => {4, %{bot: 4, lft: -1, rig: 8, top: 7}},
+
{67, 98} => {1, %{bot: 6, lft: 9, rig: -1, top: 7}},
+
{13, 85} => {0, %{bot: 9, lft: 8, rig: 6, top: 6}},
+
{63, 81} => {9, %{bot: 7, lft: 9, rig: 8, top: 9}},
+
{82, 60} => {6, %{bot: 8, lft: 9, rig: 9, top: 9}},
+
{47, 38} => {4, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{15, 92} => {2, %{bot: 7, lft: 9, rig: 6, top: 6}},
+
{58, 58} => {8, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{20, 3} => {6, %{bot: 7, lft: 3, rig: 9, top: 5}},
+
{61, 95} => {3, %{bot: 7, lft: 9, rig: 7, top: 7}},
+
{23, 67} => {9, %{bot: 9, lft: 9, rig: 9, top: 9}},
+
{78, 75} => {1, %{bot: 6, lft: 9, rig: 7, top: 9}},
+
{79, 17} => {6, %{bot: 6, lft: 7, rig: 9, top: 9}},
+
{75, 0} => {5, %{bot: 6, lft: -1, rig: 9, top: 7}},
+
{16, 73} => {4, %{bot: 9, lft: 9, rig: 7, top: 8}},
+
{76, 2} => {3, %{bot: 6, lft: 4, rig: 9, top: 7}},
+
{58, 84} => {9, %{bot: 8, lft: 9, rig: 6, ...}},
+
{58, 33} => {6, %{bot: 9, lft: 9, ...}},
+
{47, 44} => {5, %{bot: 9, ...}},
+
{54, 31} => {9, %{...}},
+
{13, ...} => {3, ...},
+
{...} => {...},
+
...
+
}
+
```
+
+
```elixir
+
map
+
|> Enum.count(fn {_, {v, maxes}} ->
+
maxes
+
|> Map.values()
+
|> Enum.any?(&(v > &1))
+
end)
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
1538
+
```
+
+
## Task 2
+
+
```elixir
+
defmodule Task2 do
+
def range(range, value) do
+
Map.new(range, &{&1, value})
+
end
+
end
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{:module, Task2, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:range, 2}}
+
```
+
+
```elixir
+
empty = Task2.range(0..9, 0)
+
+
l_map =
+
Enum.flat_map(0..width, fn x ->
+
{list, _} =
+
Enum.map_reduce(0..height, empty, fn y, acc ->
+
# IO.inspect({{x, y}, acc})
+
v = raw_map[{x, y}]
+
dist = y - acc[v]
+
new_acc = Map.merge(acc, Task2.range(0..v, y))
+
+
{{{x, y}, dist}, new_acc}
+
end)
+
+
list
+
end)
+
|> Map.new()
+
+
t_map =
+
Enum.flat_map(0..width, fn y ->
+
{list, _} =
+
Enum.map_reduce(0..height, empty, fn x, acc ->
+
v = raw_map[{x, y}]
+
dist = x - acc[v]
+
new_acc = Map.merge(acc, Task2.range(0..v, x))
+
+
{{{x, y}, dist}, new_acc}
+
end)
+
+
list
+
end)
+
|> Map.new()
+
+
empty = Task2.range(0..9, width)
+
+
r_map =
+
Enum.flat_map(height..0//-1, fn x ->
+
{list, _} =
+
Enum.map_reduce(width..0//-1, empty, fn y, acc ->
+
v = raw_map[{x, y}]
+
dist = acc[v] - y
+
new_acc = Map.merge(acc, Task2.range(0..v, y))
+
+
{{{x, y}, dist}, new_acc}
+
end)
+
+
list
+
end)
+
|> Map.new()
+
+
b_map =
+
Enum.flat_map(width..0//-1, fn y ->
+
{list, _} =
+
Enum.map_reduce(height..0//-1, empty, fn x, acc ->
+
v = raw_map[{x, y}]
+
dist = acc[v] - x
+
new_acc = Map.merge(acc, Task2.range(0..v, x))
+
+
{{{x, y}, dist}, new_acc}
+
end)
+
+
list
+
end)
+
|> Map.new()
+
+
merger = fn _, a, b -> a * b end
+
+
l_map
+
|> Map.merge(t_map, merger)
+
|> Map.merge(r_map, merger)
+
|> Map.merge(b_map, merger)
+
|> Enum.max_by(&elem(&1, 1))
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{{9, 63}, 496125}
+
```
+
+
```elixir
+
{l_map[{3, 2}], t_map[{3, 2}], b_map[{3, 2}], r_map[{3, 2}]}
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{2, 1, 1, 1}
+
```
+203
2022/day09.livemd
···
+
<!-- livebook:{"persist_outputs":true} -->
+
+
# Day 09
+
+
```elixir
+
Mix.install([
+
{:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}
+
])
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
:ok
+
```
+
+
## Setup
+
+
<!-- livebook:{"attrs":{"day":"9","session_secret":"ADVENT_OF_CODE_SESSION","variable":"puzzle_input","year":"2022"},"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} -->
+
+
```elixir
+
{:ok, puzzle_input} =
+
KinoAOC.download_puzzle("2022", "9", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{:ok,
+
"L 1\nR 1\nL 1\nU 1\nR 2\nU 1\nD 2\nR 2\nU 1\nD 2\nU 2\nL 2\nD 1\nU 1\nD 2\nL 2\nD 1\nL 2\nD 2\nL 1\nU 2\nR 2\nL 1\nD 2\nL 2\nR 2\nD 2\nL 1\nU 1\nR 1\nU 1\nL 1\nD 1\nR 1\nU 2\nD 2\nR 1\nU 1\nR 1\nL 1\nU 1\nR 1\nD 1\nL 1\nU 1\nR 1\nD 1\nU 1\nD 1\nU 1\nR 2\nL 2\nD 2\nL 2\nR 1\nU 2\nL 2\nR 1\nU 1\nR 2\nD 2\nR 2\nL 2\nU 2\nR 2\nD 2\nR 1\nU 1\nL 2\nR 1\nU 1\nD 2\nU 2\nD 1\nR 2\nL 2\nD 2\nL 2\nR 2\nU 1\nR 2\nL 2\nD 2\nL 1\nD 1\nR 2\nL 1\nU 2\nR 1\nU 2\nD 1\nU 2\nD 1\nL 1\nR 2\nD 2\nR 2\nU 2\nR 1\nU 2\nR 2\nU 1\nR 1\nD 2\nU 2\nR 2\nU 1\nR 2\nD 2\nR 1\nL 1\nU 3\nL 2\nU 1\nL 2\nU 1\nD 1\nU 2\nD 1\nU 2\nR 2\nL 1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nR 7\nU 10\nR 4\nU 6\nR 7\nU 2\nR 5\nU 6\nL 4\nU 8\nL 6\nD 8\nR 2\nU 2\nL 4\nU 4\nR 5\nU 10\nD 8\nU 7\nR 5\nD 3\nL 2\nR 9\nU 10\nL 1\nU 5\nL 1\nD 3\nL 9\nR 9\nU 2\nD 3\nU 8\nR 10\nL 2\nU 9\nD 4\nU 4\nD 10\nL 4\nD 3\nR 3\nL 2\nU 8\nL 4\nD 6\nR 2\nL 3\nD 6\nL 4\nD 5\nL 7\nU 9\nL 3\nD 4\nR 6\nL 10\nU 5\nL 3\nR 3\nD 6\nU 1\nL 3\nR 5\nD 8\nR 11\nU 2\nL 11\nR 7\nU 8\nR 4\nD 4\nU 6\nD 4\nU 9\nD 5\nR 9\nD 9\n" <> ...}
+
```
+
+
```elixir
+
moves =
+
puzzle_input
+
|> String.split("\n", trim: true)
+
|> Enum.map(fn line ->
+
[dir, steps] = String.split(line)
+
+
{dir, String.to_integer(steps)}
+
end)
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
[
+
{"L", 1},
+
{"R", 1},
+
{"L", 1},
+
{"U", 1},
+
{"R", 2},
+
{"U", 1},
+
{"D", 2},
+
{"R", 2},
+
{"U", 1},
+
{"D", 2},
+
{"U", 2},
+
{"L", 2},
+
{"D", 1},
+
{"U", 1},
+
{"D", 2},
+
{"L", 2},
+
{"D", 1},
+
{"L", 2},
+
{"D", 2},
+
{"L", 1},
+
{"U", 2},
+
{"R", 2},
+
{"L", 1},
+
{"D", 2},
+
{"L", 2},
+
{"R", 2},
+
{"D", 2},
+
{"L", 1},
+
{"U", 1},
+
{"R", 1},
+
{"U", 1},
+
{"L", 1},
+
{"D", 1},
+
{"R", 1},
+
{"U", 2},
+
{"D", 2},
+
{"R", 1},
+
{"U", 1},
+
{"R", 1},
+
{"L", 1},
+
{"U", 1},
+
{"R", 1},
+
{"D", 1},
+
{"L", 1},
+
{"U", 1},
+
{"R", 1},
+
{"D", 1},
+
{"U", 1},
+
{"D", ...},
+
{...},
+
...
+
]
+
```
+
+
```elixir
+
defmodule Rope do
+
@start %{x: 0, y: 0}
+
+
defstruct segments: [%{x: 0, y: 0}, %{x: 0, y: 0}]
+
+
def new(length),
+
do: %__MODULE__{segments: List.duplicate(@start, length + 1)}
+
+
def last(%__MODULE__{segments: list}), do: List.last(list)
+
+
def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, dir) do
+
head =
+
case dir do
+
"U" -> %{x: x + 1, y: y}
+
"D" -> %{x: x - 1, y: y}
+
"L" -> %{x: x, y: y + 1}
+
"R" -> %{x: x, y: y - 1}
+
end
+
+
segments = move_tails([head | tails])
+
+
%__MODULE__{segments: segments}
+
end
+
+
defp move_tails([]), do: []
+
defp move_tails([head]), do: [head]
+
+
# When nothing else moves, then stop
+
defp move_tails([head, tail | _] = rope)
+
when abs(head.x - tail.x) < 2 and abs(head.y - tail.y) < 2,
+
do: rope
+
+
defp move_tails([head, tail | rest])
+
when abs(head.y - tail.y) < 2,
+
do: [head | move_tails([%{x: head.x - sgn(head.x - tail.x), y: head.y} | rest])]
+
+
defp move_tails([head, tail | rest])
+
when abs(head.x - tail.x) < 2,
+
do: [head | move_tails([%{x: head.x, y: head.y - sgn(head.y - tail.y)} | rest])]
+
+
defp move_tails([head, tail | rest]),
+
do: [
+
head
+
| move_tails([%{x: head.x - sgn(head.x - tail.x), y: head.y - sgn(head.y - tail.y)} | rest])
+
]
+
+
def sgn(0), do: 0
+
def sgn(n) when n < 0, do: -1
+
def sgn(_), do: 1
+
end
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
{:module, Rope, <<70, 79, 82, 49, 0, 0, 21, ...>>, {:sgn, 1}}
+
```
+
+
```elixir
+
rope = Rope.new(1)
+
+
{_rope, tail_positions} =
+
moves
+
|> Enum.flat_map(fn {dir, steps} -> List.duplicate(dir, steps) end)
+
|> Enum.reduce({rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} ->
+
new_rope = Rope.move(rope, dir)
+
+
{new_rope, MapSet.put(acc, Rope.last(new_rope))}
+
end)
+
+
MapSet.size(tail_positions)
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
5960
+
```
+
+
## Task 2
+
+
```elixir
+
rope = Rope.new(9)
+
+
{_rope, tail_positions} =
+
moves
+
|> Enum.flat_map(fn {dir, steps} -> List.duplicate(dir, steps) end)
+
|> Enum.reduce({rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} ->
+
new_rope = Rope.move(rope, dir)
+
+
{new_rope, MapSet.put(acc, Rope.last(new_rope))}
+
end)
+
+
MapSet.size(tail_positions)
+
```
+
+
<!-- livebook:{"output":true} -->
+
+
```
+
2327
+
```