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

Day 2

day2.livemd

Day 2

Mix.install([{:kino, "~> 0.5.0"}])

Part 1

input = """
A Y
B X
C Z
"""
input = Kino.Input.textarea("Please paste your input file:")
input = Kino.Input.read(input)
parsed =
  input
  |> String.split("\n", trim: true)
  |> Enum.map(&String.split/1)
score = %{
  ["A", "X"] => 1 + 3,
  ["A", "Y"] => 2 + 6,
  ["A", "Z"] => 3 + 0,
  ["B", "X"] => 1 + 0,
  ["B", "Y"] => 2 + 3,
  ["B", "Z"] => 3 + 6,
  ["C", "X"] => 1 + 6,
  ["C", "Y"] => 2 + 0,
  ["C", "Z"] => 3 + 3
}
parsed
|> Enum.map(&Map.get(score, &1))
|> Enum.sum()

Part 2

score = %{
  ["A", "X"] => 3 + 0,
  ["A", "Y"] => 1 + 3,
  ["A", "Z"] => 2 + 6,
  ["B", "X"] => 1 + 0,
  ["B", "Y"] => 2 + 3,
  ["B", "Z"] => 3 + 6,
  ["C", "X"] => 2 + 0,
  ["C", "Y"] => 3 + 3,
  ["C", "Z"] => 1 + 6
}
parsed
|> Enum.map(&Map.get(score, &1))
|> Enum.sum()