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

Advent of Code 2022 - Day 1

2022/day01.livemd

Advent of Code 2022 - Day 1

Mix.install([:kino, {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}])

Input

test_input =
  """
  1000
  2000
  3000

  4000

  5000
  6000

  7000
  8000
  9000

  10000
  """
  |> String.trim()
{:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "1", System.fetch_env!("LB_AOC_SESSION"))
input_field =
  Kino.Input.select("input", [
    {test_input, "test_input"},
    {puzzle_input, "puzzle_input"}
  ])

Parsing

ren_calories =
  input_field
  |> Kino.Input.read()
  |> String.split("\n")
  |> Enum.chunk_while(
    0,
    fn
      "", sum -> {:cont, sum, 0}
      nr, sum -> {:cont, sum + String.to_integer(nr)}
    end,
    &{:cont, &1, nil}
  )

Part 1

Enum.max(ren_calories)

Part 2

ren_calories |> Enum.sort(:desc) |> Enum.take(3) |> Enum.sum()