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

Advent of Code: Day 02

2022/Day02.livemd

Advent of Code: Day 02

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

Helpers

defmodule Helpers do
  def read_file_contents(filename) do
    file_path = "/Users/charlie/github.com/charlieroth/advent-of-code/2022/#{filename}"

    case File.read(file_path) do
      {:ok, contents} -> contents
      {:error, _} -> raise("Failed to read file contents")
    end
  end
end

Day 02: Rock Paper Scissors

Rules

  • Rock defeats Scissors
  • Scissors defeats Paper
  • Paper defeats Rock

If both players choose the same shape, the round instead ends in a draw.

First column is what your opponent is going to play:

  • A for Rock
  • B for Paper
  • C for Scissors

The second column must be what you should play in response:

  • X for Rock
  • Y for Paper
  • Z for Scissors

The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round

The score for a single round is the score for the shape you selected…

  • 1 for Rock
  • 2 for Paper
  • 3 for Scissors

…plus the score for the outcome of the round

  • 0 if you lost
  • 3 if the round was a draw
  • 6 if you won

Part 01

example = ~S"""
A Y
B X
C Z
"""
Helpers.read_file_contents("inputs/Day02.txt")
|> String.split("\n", trim: true)
|> Enum.map(fn match_str ->
  [op_move, your_move] = String.split(match_str, " ")
  {op_move, your_move}
end)
|> Enum.reduce(0, fn moves, acc ->
  case moves do
    # rock, rock
    {"A", "X"} -> acc + 1 + 3
    # rock, paper
    {"A", "Y"} -> acc + 2 + 6
    # rock, scissors
    {"A", "Z"} -> acc + 3 + 0
    # paper, rock
    {"B", "X"} -> acc + 1 + 0
    # paper, paper
    {"B", "Y"} -> acc + 2 + 3
    # paper, scissors
    {"B", "Z"} -> acc + 3 + 6
    # scissors, rock
    {"C", "X"} -> acc + 1 + 6
    # scissors, paper
    {"C", "Y"} -> acc + 2 + 0
    # scissors, scissors
    {"C", "Z"} -> acc + 3 + 3
  end
end)

Part 02

The second column says how the round needs to end:

  • X means you need to lose
  • Y means you need to end the round in a draw
  • Z means you need to win

The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated

In the first round, your opponent will choose Rock A, and you need the round to end in a draw Y, so you also choose Rock. This gives you a score of 1 + 3 = 4.

In the second round, your opponent will choose Paper B, and you choose Rock so you lose X with a score of 1 + 0 = 1.

In the third round, you will defeat your opponent’s Scissors Z with Rock X for a score of 1 + 6 = 7.

You would get a total score of 12

example = ~S"""
A Y
B X
C Z
"""
Helpers.read_file_contents("inputs/Day02.txt")
|> String.split("\n", trim: true)
|> Enum.map(fn match_str ->
  [op_move, your_move] = String.split(match_str, " ")
  {op_move, your_move}
end)
|> Enum.reduce(0, fn moves, acc ->
  case moves do
    # rock, loss, 0 + scissors, 3
    {"A", "X"} -> acc + 0 + 3
    # rock, tie, 3 + rock, 1
    {"A", "Y"} -> acc + 3 + 1
    # rock, win, 6 + paper, 2
    {"A", "Z"} -> acc + 6 + 2
    # paper, loss, 0 + rock, 1
    {"B", "X"} -> acc + 0 + 1
    # paper, tie, 3 + paper, 2
    {"B", "Y"} -> acc + 3 + 2
    # paper, win, 6 + scissors, 3
    {"B", "Z"} -> acc + 6 + 3
    # scissors, loss, 0 + paper, 2
    {"C", "X"} -> acc + 0 + 2
    # scissors, tie, 3 + scissors, 3
    {"C", "Y"} -> acc + 3 + 3
    # scissors, win, 6 + rock, 1
    {"C", "Z"} -> acc + 6 + 1
  end
end)