Day 10
Mix.install([:kino_aoc])
Section
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "10", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
{:ok,
"543067650323210321032110356789890110710189878760134567\n612148941212306782345091235410765227893258759651021298\n701238932301456798106787549323454336794567549842330807\n898547215450589867287654678892961245987654456732498910\n987656306769678956398013234781870301256003301201567821\n234543215878760145478920105690210982340112210341056932\n109650124981232230569834124567345673451234985452347845\n018744323890341021656765033438454589298545676701037796\n199803210787650110765017842129823672107655677812378987\n785012345654789541874326956010510563201234389983405676\n174321498723498632903455467173421454102348210234514430\n065430239014323721212762398982567876218959650149623521\n145540128765017890101871081071018967327968743898734678\n236692134984178768718981012567894458456876212798604329\n987783005673269659654108923458763349663212108789015012\n896654012562154546743267830309854218767103419276126787\n765780123473043432890154321212903109878756578125435698\n434099874984012301740125654567812893469017647030165556\n323101065765101787434436763058906732154178736543256765\n013298187890678696325569892143215440043289128954343894\n010367296541549045016678765014300351234567037760012983\n329458305032132134787867410967891267653438546891898012\n458789414145010120196986323898763298344129655432743210\n567076523236678743285898456777654101201098766653658901\n456167654107549658374567689985403100312434985567877014\n343228943298938569843210576876312238920125676656986323\n651014543498127478755765435410278947437876985543235434\n743223672567078369876890324320127656506987234350145632\n894102981089569232125981210451256543215432122341076541\n765091234676352101034674323401237894101341001456789830\n014780365445443032126501012532345743103210112565898321\n123689876034321145098432107645236743254101101876543210\n094545763125210056788943258896107858969010332987674301\n785430432106782169877652349787567967678321245696784561\n876021569045893458965601654343478914565414326781093870\n987109878010432167014298710234323901258905015492012901\n789219894324569052123109620123217892109876012323458692\n654308765543678743013234538994106543010870965410069783\n789877787632189852101110145885287676521961871012178654\n034565896601087601211001406776896789437652676523468743\n121053795542194512389132312345445676548943989430549012\n262342187634765433498545434531230107401232104321678298\n876533014123892324567696526540043298340981010780890187\n983414523016101019876587017832134781256672125690743296\n032101654327892100345678789901295690764543434321652345\n123089789236743981234589654800387789835678987787601336\n854176590187656876104310343211456876544549016098543223\n965765410194545675465231230898565963233432145123210112\n876894321243232980370110141767653210112321039654101101\n146765232100101061289879852632154100203410128789210322\n036787103761267876518768763543069911012543454376325412\n123498654854356987405650654985678892327632567215436701\n210567569983543292314321569876301765438761078904589898\n325456478754330101223433478765432654349652987615678765"}
map =
for {row, y} <- puzzle_input |> String.split("\n", trim: true) |> Enum.with_index(),
{h, x} <- row |> String.to_charlist() |> Enum.with_index(),
into: %{},
do: {{x, y}, h - ?0}
%{
{37, 47} => 3,
{38, 2} => 4,
{1, 26} => 5,
{32, 15} => 4,
{35, 30} => 3,
{37, 53} => 4,
{4, 5} => 4,
{8, 50} => 3,
{11, 39} => 1,
{22, 38} => 1,
{20, 41} => 8,
{29, 25} => 6,
{29, 26} => 0,
{47, 27} => 0,
{31, 42} => 4,
{9, 34} => 0,
{47, 38} => 2,
{20, 3} => 7,
{47, 44} => 1,
{22, 37} => 3,
{22, 36} => 0,
{45, 7} => 7,
{19, 38} => 0,
{27, 21} => 9,
{9, 33} => 1,
{26, 41} => 4,
{52, 42} => 8,
{21, 35} => 2,
{42, 8} => 6,
{19, 22} => 9,
{16, 10} => 3,
{21, 45} => 5,
{11, 8} => 7,
{26, 21} => 0,
{21, 48} => 1,
{50, 40} => 9,
{10, 25} => 9,
{11, 19} => 0,
{16, 38} => 5,
{35, 15} => 8,
{37, 15} => 6,
{35, 26} => 7,
{28, 20} => 1,
{45, 19} => 9,
{27, 22} => 8,
{16, 24} => 5,
{6, 18} => 0,
{10, 32} => 2,
{32, ...} => 8,
{...} => 7,
...
}
starts = Enum.filter(map, fn {_, elev} -> elev == 0 end)
[
{{38, 5}, 0},
{{24, 7}, 0},
{{11, 35}, 0},
{{3, 9}, 0},
{{15, 36}, 0},
{{41, 32}, 0},
{{51, 14}, 0},
{{51, 24}, 0},
{{5, 12}, 0},
{{4, 35}, 0},
{{40, 4}, 0},
{{46, 4}, 0},
{{22, 30}, 0},
{{27, 12}, 0},
{{7, 4}, 0},
{{24, 43}, 0},
{{31, 20}, 0},
{{29, 42}, 0},
{{29, 9}, 0},
{{10, 2}, 0},
{{0, 20}, 0},
{{15, 20}, 0},
{{14, 0}, 0},
{{42, 48}, 0},
{{40, 15}, 0},
{{43, 14}, 0},
{{19, 2}, 0},
{{14, 22}, 0},
{{34, 35}, 0},
{{38, 42}, 0},
{{23, 5}, 0},
{{0, 11}, 0},
{{44, 11}, 0},
{{15, 30}, 0},
{{17, 44}, 0},
{{31, 32}, 0},
{{48, 28}, 0},
{{21, 39}, 0},
{{49, 13}, 0},
{{29, 45}, 0},
{{4, 37}, 0},
{{53, 31}, 0},
{{45, 46}, 0},
{{43, 29}, 0},
{{8, 0}, 0},
{{47, 42}, 0},
{{7, 14}, 0},
{{52, ...}, 0},
{{...}, ...},
{...},
...
]
defmodule Trail do
def traverse(start, map), do: MapSet.new(traverse(start, map, []))
defp traverse({curr, 9}, _map, acc), do: [[{curr, 9} | acc]]
defp traverse({curr, elev}, map, acc) do
curr
|> surroundings()
|> then(&Map.take(map, &1))
|> Enum.filter(fn {_, h} -> elev + 1 == h end)
|> Enum.flat_map(&traverse(&1, map, [{curr, elev} | acc]))
end
defp surroundings({x, y}) do
[
{x, y - 1},
{x - 1, y},
{x, y + 1},
{x + 1, y}
]
end
end
{:module, Trail, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:surroundings, 1}}
Part 1
starts
|> Enum.map(fn start ->
Trail.traverse(start, map)
|> MapSet.new(&hd/1)
|> MapSet.size()
end)
|> Enum.sum()
733
Section
starts
|> Enum.map(fn start ->
Trail.traverse(start, map)
|> MapSet.size()
end)
|> Enum.sum()
1514