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

day02

2022/day02.livemd

day02

Part 1

defmodule Game do
  def round_score({_opponent, played} = round) do
    round_state_score =
      round
      |> round_state()
      |> round_state_score()

    shape_score = shape_score(played)

    round_state_score + shape_score
  end

  defp shape_score(:rock), do: 1
  defp shape_score(:paper), do: 2
  defp shape_score(:scissors), do: 3

  defp round_state({shape, shape}), do: :draw
  defp round_state({:rock, :paper}), do: :won
  defp round_state({:paper, :scissors}), do: :won
  defp round_state({:scissors, :rock}), do: :won
  defp round_state({_, _} = _round), do: :lost

  defp round_state_score(:lost), do: 0
  defp round_state_score(:draw), do: 3
  defp round_state_score(:won), do: 6
end
File.stream!("./2022/day02input.txt")
|> Stream.map(fn <> ->
  opponent =
    case raw_opponent do
      ?A -> :rock
      ?B -> :paper
      ?C -> :scissors
    end

  played =
    case raw_played do
      ?X -> :rock
      ?Y -> :paper
      ?Z -> :scissors
    end

  Game.round_score({opponent, played})
end)
|> Enum.sum()

Part 2

defmodule Game do
  def round_score({opponent, status}) do
    played = need_to_play(opponent, status)

    round_state_score =
      {opponent, played}
      |> round_state()
      |> round_state_score()

    shape_score = shape_score(played)

    round_state_score + shape_score
  end

  defp need_to_play(shape, status)
       when {shape, status} in [{:paper, :lost}, {:rock, :draw}, {:scissors, :win}],
       do: :rock

  defp need_to_play(shape, status)
       when {shape, status} in [{:scissors, :lost}, {:paper, :draw}, {:rock, :win}],
       do: :paper

  defp need_to_play(shape, status)
       when {shape, status} in [{:rock, :lost}, {:scissors, :draw}, {:paper, :win}],
       do: :scissors

  defp shape_score(:rock), do: 1
  defp shape_score(:paper), do: 2
  defp shape_score(:scissors), do: 3

  defp round_state({shape, shape}), do: :draw
  defp round_state({:rock, :paper}), do: :won
  defp round_state({:paper, :scissors}), do: :won
  defp round_state({:scissors, :rock}), do: :won
  defp round_state({_, _} = _round), do: :lost

  defp round_state_score(:lost), do: 0
  defp round_state_score(:draw), do: 3
  defp round_state_score(:won), do: 6
end
File.stream!("./2022/day02input.txt")
|> Stream.map(fn <> ->
  opponent =
    case raw_opponent do
      ?A -> :rock
      ?B -> :paper
      ?C -> :scissors
    end

  status =
    case raw_status do
      ?X -> :lost
      ?Y -> :draw
      ?Z -> :win
    end

  Game.round_score({opponent, status})
end)
|> Enum.sum()