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

Advent of Code 2022 - Day 2

2022/day02.livemd

Advent of Code 2022 - Day 2

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

Input

test_input = """
A Y
B X
C Z
"""
{:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "2", System.fetch_env!("LB_AOC_SESSION"))
input_field =
  Kino.Input.select("input", [
    {test_input, "test_input"},
    {puzzle_input, "puzzle_input"}
  ])

Parsing

parsed =
  input_field
  |> Kino.Input.read()
  |> String.split("\n", trim: true)

Part 1

parsed
|> Enum.reject(&(&1 == ""))
|> Enum.map(fn
  "A X" -> 1 + 3
  "B X" -> 1
  "C X" -> 1 + 6
  "A Y" -> 2 + 6
  "B Y" -> 2 + 3
  "C Y" -> 2
  "A Z" -> 3
  "B Z" -> 3 + 6
  "C Z" -> 3 + 3
end)
|> Enum.sum()

Part 2

parsed
|> Enum.reject(&(&1 == ""))
|> Enum.map(fn
  "A X" -> 3
  "B X" -> 1
  "C X" -> 2
  "A Y" -> 1 + 3
  "B Y" -> 2 + 3
  "C Y" -> 3 + 3
  "A Z" -> 2 + 6
  "B Z" -> 3 + 6
  "C Z" -> 1 + 6
end)
|> Enum.sum()