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

AOC_DAY 2

day_two/day_two.livemd

AOC_DAY 2

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

Section

{:ok, response} = Req.get("https://raw.githubusercontent.com/samh7/A0C_2024/refs/heads/master/day_two/text.txt")
contents = response.body
str_list = contents
  |> String.split("\n", tim: true)
  |> Enum.reject(&(String.trim(&1) == ""))
  |> Enum.map(
    &(String.split(&1, " ", trim: true)
      |> Enum.map(fn str ->
        String.to_integer(str)
      end))
  )
  |> Enum.map(
    &(&1
      |> Enum.chunk_every(2, 1, :discard)
      |> Enum.map(fn [a, b] ->
        cond do
          b > a -> {:increasing, b - a}
          b < a -> {:decreasing, a - b}
          true -> {:no_change, 0}
        end
      end))
  )
unique_types =
  Enum.map(
    str_list,
    &amp;(&amp;1
      |> Enum.map(fn c ->
        c |> elem(0)
      end)
      |> Enum.uniq())
  )

max_changes =
  Enum.map(
    str_list,
    &amp;(&amp;1
        |> Enum.map(fn c ->
        c |> elem(1)
      end)
      |> Enum.max())
  )

[unique_types, max_changes]
|> Enum.zip()
|> Enum.map(fn i ->
  {j, k} = i
  length(j) == 1 &amp;&amp; k <= 3
end)
|> Enum.filter(&amp; &amp;1)
|> length()