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

Day 2

day-2.livemd

Day 2

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

Helper

{:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "2", System.fetch_env!("LB_AOC_SESSION"))

A -> Rock

B -> Paper

C -> Scissor


X -> Rock

Y -> Paper

Z -> Scissor


Score

1 -> Rock

2 -> Paper

3 -> Scissor

Plus 0(lost) | 3(draw) | 6(win)

puzzle_input =
  puzzle_input
  |> String.split("\n", trim: true)
  |> Enum.map(&String.split/1)

Part 1

defmodule GameLogic do
  def score(enemy_move, player_move) do
    case move_decision(enemy_move, player_move) do
      {:win, point} -> point + 6
      {:loss, point} -> point + 0
      {:draw, point} -> point + 3
    end
  end

  def move_decision("A", player_move) do
    case player_move do
      "X" -> {:draw, 1}
      "Y" -> {:win, 2}
      "Z" -> {:loss, 3}
    end
  end

  def move_decision("B", player_move) do
    case player_move do
      "X" -> {:loss, 1}
      "Y" -> {:draw, 2}
      "Z" -> {:win, 3}
    end
  end

  def move_decision("C", player_move) do
    case player_move do
      "X" -> {:win, 1}
      "Y" -> {:loss, 2}
      "Z" -> {:draw, 3}
    end
  end
end
Enum.reduce(puzzle_input, 0, fn [enemy_move, player_move], total_score ->
  total_score + GameLogic.score(enemy_move, player_move)
end)

Part 2

defmodule GameLogicPart2 do
  def score(enemy_move, player_move) do
    case move_decision(enemy_move, player_move) do
      {:win, point} -> point + 6
      {:loss, point} -> point + 0
      {:draw, point} -> point + 3
    end
  end

  # Rock
  def move_decision("A", player_move) do
    case player_move do
      "X" -> {:loss, 3}
      "Y" -> {:draw, 1}
      "Z" -> {:win, 2}
    end
  end

  # Paper
  def move_decision("B", player_move) do
    case player_move do
      "X" -> {:loss, 1}
      "Y" -> {:draw, 2}
      "Z" -> {:win, 3}
    end
  end

  # Scissor
  def move_decision("C", player_move) do
    case player_move do
      "X" -> {:loss, 2}
      "Y" -> {:draw, 3}
      "Z" -> {:win, 1}
    end
  end
end
Enum.reduce(puzzle_input, 0, fn [enemy_move, player_move], total_score ->
  total_score + GameLogicPart2.score(enemy_move, player_move)
end)