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

AOC DAY 1

day_one/aoc-day-one.livemd

AOC DAY 1

Mix.install(
  [
    {:req, "~> 0.4.8"},
  ]
)

Section

{:ok, response} = Req.get("https://raw.githubusercontent.com/samh7/A0C_2024/refs/heads/master/day_one/text.txt")
contents = response.body
str_list =
  contents
  |> String.split(" ", trim: true)
  |> Enum.map(
    &(String.split(&1, "\n", trim: true)
      |> Enum.map(fn str -> String.to_integer(str) end)
      |> List.to_tuple())
  )
defmodule AOC_DayOne do
  def day_one([{head} | tail]) do
    day_one(tail, head)
  end

  def day_one([], _) do
    []
  end

  def day_one([{a,b} | list], prev) do
    [[prev,  a] | day_one(list, b)]
  end

  def day_one([{a}], prev) do
    [[prev,  a]]
  end

  def day_one([{a} | list], _) do
    day_one(list, a)
  end
end
AOC_DayOne.day_one(str_list)
|> Enum.zip()
|> Enum.map(
  &(Tuple.to_list(&1)
    |> Enum.sort())
)
|> Enum.zip()
|> Enum.reduce([], fn i, acc ->
  {a,b} = i
  [abs(a - b) | acc]
end)
|> Enum.sum()