Skip to content

Latest commit

 

History

History
51 lines (40 loc) · 831 Bytes

day01.livemd

File metadata and controls

51 lines (40 loc) · 831 Bytes

Day1

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

Input

# thanks to https://twitter.com/ryoung786/status/1598415749638397962?s=20&t=TLW9UlzqB9uOYhPjuGQuSQ
day = 1
aoc_session = System.fetch_env!("LB_AOC_SESSION_COOKIE")
input_url = "https://adventofcode.com/2022/day/#{day}/input"

input = Req.get!(input_url, headers: [cookie: "session=#{aoc_session}"]).body
chunk_func = fn element, acc ->
  case Integer.parse(element) do
    :error -> {:cont, acc, 0}
    {element, _} -> {:cont, acc + element}
  end
end

after_func = fn
  [] -> {:cont, []}
  acc -> {:cont, acc, []}
end

input =
  input
  |> String.split("\n")
  |> Stream.chunk_while(0, chunk_func, after_func)
  |> Enum.sort(:desc)
  |> Enum.take(3)

Part 1

input |> hd

Part 2

Enum.sum(input)