Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 09

2024/day09.livemd

Day 09

Mix.install([:kino_aoc])

Section

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2024", "9", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
{:ok,
 "6920945132536042828994482345396169724991532616269072173941615129441070989533549353544192338215647480107561245832285861135469128938656230362333818111433746344751894125805448321674418337459440436476402523264257512621383849394968854739721644705231266361534476141022646838663844981685665728936624151235476791584929822482292987334479824016888196957024425673938289753698621244157130975989396226731549454298144391686468562820376390235135108330238590316674261440783039581594584646247629719986582560203598244312596985279832984526808559405915881067383593502384107494586724976925694948216635952475703445963854271259175649271576378184603074946598276445248640841860479933368524996925945274749282264063148922416399632485376097486952663787643633316356949469798134153950341455917737349115548390535536536397237654519556679716185652595572754749672866345553911576628161614338861880658781637382837898686889463249126116447176285128303560134154431099612772162289797131303641655015516771696046959228585241299646581370538088916416989047515564526116366867367181678616439431284930379889626059473847818663132232979123503952455854489755245141781899866948543116887346827534697893607177987563154596774691267167851154138330695352108499217988406372425774968626985943665687196181233111972699532427257992757984672882888037943925818450345177398760216079692074854569485530377584183586279552981343712063437565138087959796501957177177884278405560475435141829787014884681355059571489723985901554488154898784759668954752934048679146776060562786272011429026535298115262548593878183498916571398351436431313482968486771572527901996664899792152552410493090499727835643526428881322227999149881884793853729989624533029587010105028429614225841889414576176753414877452414680577843459538128911939238704598815126186492327558383740971333501769852667796188543180462490517062819496532859194781303437743159526136553370517461484469733677187178992265612439155938358015666261906673153823768535183244601645561579916316476813124539675061263411515873626197742860301068615568696174716481151333516718804424987323957615961444822618946925923196806747458558553262735686467327309580525855605085698452928593956820699112556481136173708346321771719645737818963669665547522134505223646796667286359173528172301211931188731691358636677670346032949579894428251583802532214067603254278658698940647270167640764638397570409829821264846762426836572463386475815673329747876757249732659277388030843590439617143487927833367461978526979212861844382550791390358527895937281110537852835869713059958217969899704145215985377666628719451233132412294358444746793946343553683032671225858498134340537229859741987180829468238642605723478228646886791227546199853372232566387139711011753555879744151132642224546223435667375781861424845134566438891443785750344893853718679532879854483395621276216275644617996376282773738211864275304297979547422875934634992111232368475557713129934172577624852042405732858060209959806520925730394293739081861693532946112557827992852621975727432957291082907728921029905725787812513023871891551681262814572424452648875694586632916946898788794936727799526748807158546930659285568029733948646265541485187570661411984840326374573377475525442055851882711851241546246736884066686885724058588220382743451817154627144869212944944985727560911326298754699513901913775411631051155448259136813383243697125190592983671813131786588158152523929049549018258627964330945831994544156999373027523966329015316477207024639470312244244043635392684240435826402148884612989874566739774998162494686244143442812672951249922585423333957811202767768597518787769627151336335561386093158376767595684951873065594471827052965041948820794865486179401375326111497235854418829486872664452298163169179763609277741983868518845423468770227926607088302941698125295594475259881783566721809397453893791727816025381929613950311537338366491240658016437269618697401059446097141870959520762921795565136242743648578086232855147488427619213536145259919693337741268680894834764319547389247696113145205712656834305853252319218569748862345539664553175240528621328137233671331496239591159242157540311894387168" <> ...}
#puzzle_input = "2333133121414131402"

tags =
  Stream.from_index()
  |> Stream.intersperse(:gap)

files =
  puzzle_input
  |> String.split("", trim: true)
  |> Enum.map(&amp;String.to_integer/1)
  |> Enum.zip(tags)
[
  {6, 0},
  {9, :gap},
  {2, 1},
  {0, :gap},
  {9, 2},
  {4, :gap},
  {5, 3},
  {1, :gap},
  {3, 4},
  {2, :gap},
  {5, 5},
  {3, :gap},
  {6, 6},
  {0, :gap},
  {4, 7},
  {2, :gap},
  {8, 8},
  {2, :gap},
  {8, 9},
  {9, :gap},
  {9, 10},
  {4, :gap},
  {4, 11},
  {8, :gap},
  {2, 12},
  {3, :gap},
  {4, 13},
  {5, :gap},
  {3, 14},
  {9, :gap},
  {6, 15},
  {1, :gap},
  {6, 16},
  {9, :gap},
  {7, 17},
  {2, :gap},
  {4, 18},
  {9, :gap},
  {9, 19},
  {1, :gap},
  {5, 20},
  {3, :gap},
  {2, 21},
  {6, :gap},
  {1, 22},
  {6, :gap},
  {2, 23},
  {6, :gap},
  {9, ...},
  {...},
  ...
]
defmodule Disk do
  def checksum(files) do
    files
    |> Enum.reduce({0, 0}, fn
      {gap, :gap}, {pos, sum} ->
        {pos + gap, sum}

      {len, idx}, {pos, sum} ->
        new_pos = pos + len
        score = Enum.sum(pos..(new_pos - 1)) * idx

        {new_pos, score + sum}
    end)
    |> elem(1)
  end

  def display(lst) do
    Enum.map(lst, fn
      {len, val} ->
        sym = if val == :gap, do: ".", else: to_string(val)
        
        List.duplicate(sym, len)
    end)
    |> IO.iodata_to_binary()
  end
end
{:module, Disk, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:display, 1}}

Part 1

defmodule Compactor do
  def run(files) do
    occupied =
      Enum.reduce(files, 0, fn
        {_, :gap}, acc -> acc
        {len, _idx}, acc -> acc + len
      end)

    compact([], occupied, files, Enum.reverse(files))
  end

  defp compact(acc, 0, _, _), do: Enum.reverse(acc)

  defp compact(acc, left, [{0, :gap} | rest], rfiles),
    do: compact(acc, left, rest, rfiles)

  defp compact(acc, left, rest, [{_, :gap} | rfiles]),
    do: compact(acc, left, rest, rfiles)

  defp compact(acc, left, rest, [{0, _idx} | rfiles]) do
    compact(acc, left, rest, rfiles)
  end

  defp compact(acc, left, [{gap, :gap} | rest], [{len, idx} | rfiles]) do
    fill = min(gap, len)

    compact([{fill, idx} | acc], left - fill, [{gap - fill, :gap} | rest], [
      {len - fill, idx} | rfiles
    ])
  end
  
  defp compact(acc, left, [{len, idx} = file | rest], rfiles) when is_integer(idx) do
    compact([file | acc], left - len, rest, rfiles)
  end
end
{:module, Compactor, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:compact, 4}}
files
|> Compactor.run()
|> Disk.checksum()
6332189866718

Part 2

defmodule Defrag do
  def run(files) do
    files
    |> Enum.reverse()
    |> Enum.reduce(files, fn
      {_, :gap}, acc -> acc
      file, acc -> do_run(acc, file, [])
    end)
  end

  defp do_run([], _, acc), do: Enum.reverse(acc)

  defp do_run([file | _] = list, file, acc), do: Enum.reverse(acc, list)

  defp do_run([{len, :gap} | rest], {len, idx}, acc),
    do: Enum.reverse(acc, [{len, idx} | rep(rest, {len, idx})])

  defp do_run([{gap, :gap} | rest], {len, idx}, acc) when len < gap,
    do: Enum.reverse(acc, [{len, idx}, {gap - len, :gap} | rep(rest, {len, idx})])

  defp do_run([hd | rest], file, acc), do: do_run(rest, file, [hd | acc])

  defp rep([{a, :gap}, file, {b, :gap} | rest], {len, _} = file),
    do: [{a + len + b, :gap} | rest]

  defp rep([{a, :gap}, file | rest], {len, _} = file), do: [{a + len, :gap} | rest]
  defp rep([file, {b, :gap} | rest], {len, _} = file), do: [{len + b, :gap} | rest]
  defp rep([file | rest], {len, _} = file), do: [{len, :gap} | rest]
  defp rep([hd | rest], file), do: [hd | rep(rest, file)]
end
{:module, Defrag, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:rep, 2}}
files
|> Defrag.run()
|> Disk.checksum()
6353648390778