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

Day 02

2022/02.livemd

Day 02

Intro

https://adventofcode.com/2022/day/2

Input

input_test = """
A Y
B X
C Z
"""
"A Y\nB X\nC Z\n"
input = File.read!("./input02.txt")
"C Y\nC Z\nC Z\nC Z\nA Y\nC Z\nC Z\nB Y\nC Y\nA X\nC Z\nA Z\nC Y\nC Z\nC Z\nB X\nA Z\nC Z\nC Z\nC Z\nA Z\nB X\nC Y\nC Z\nC Z\nC Z\nC Y\nC Z\nC Z\nC Z\nC Z\nC Z\nA Z\nB X\nA Z\nC Z\nC Z\nC Z\nC Z\nA Z\nA X\nB X\nC Y\nA Z\nA Z\nC Y\nA Z\nA Z\nC Y\nA Z\nB X\nA Z\nC Z\nB X\nC Y\nC Z\nC Z\nB Z\nC Y\nC Z\nA Z\nC X\nC Z\nB X\nB Y\nC Z\nC Z\nC Y\nA Z\nC Y\nC Y\nB X\nA Z\nB Z\nC Z\nC Z\nC Z\nB X\nA X\nB Y\nC Z\nC Y\nC Y\nA Z\nA Z\nC Z\nC Z\nC Z\nA X\nA X\nC Y\nC Z\nA Z\nC Y\nA Z\nA Z\nC Z\nA Z\nC Y\nC Z\nA Z\nA X\nC Z\nC Z\nA Z\nB Y\nA Z\nB X\nC Z\nC Y\nA X\nC Z\nC Y\nC Z\nC Z\nB Y\nC Z\nA Z\nC Z\nB X\nC Z\nC Y\nA Z\nB Y\nB X\nB X\nA X\nB X\nA Z\nB X\nA Y\nA X\nA Z\nA Z\nC Y\nB X\nC Z\nC Z\nA X\nA Z\nA Y\nB X\nA Y\nA Z\nC Z\nA X\nC Z\nC Z\nC Z\nB X\nC Z\nC Y\nC Y\nC Z\nA Z\nC Z\nB X\nA Z\nC Y\nA X\nA Z\nA Y\nC Z\nA Z\nC Z\nC Z\nA Z\nC Y\nA Z\nC Z\nA Z\nC Z\nB X\nC Z\nA Z\nC Z\nC X\nB Y\nA Z\nC Z\nC Y\nC Z\nA Z\nC Y\nB Z\nA X\nA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A Z\nA X\nC Z\nC Z\nC X\nC Z\nB X\nA Z\nC Y\nA Z\nC Z\nC Z\nA X\nC Z\nC Z\nA Z\nC Z\nA Z\nB X\nC Y\nA Z\nC Y\nA Z\nA Y\nB X\nC Z\nA Y\nC Z\nC Z\nB X\nC Z\nA X\nC Z\nA Z\nA X\nC Y\nB X\nC Z\nC Z\nC Y\nA X\nB X\nA X\nA Z\nC Z\nB Y\nC Z\nA Z\nC Z\nA X\nB Z\nC Z\nC Y\nC Y\nA Y\nB X\nC Y\nC Z\nA Y\nC Y\nC Y\nA Z\nA Z\nC Z\nC Z\nC Z\nC Y\nA Z\nC Z\nC Z\nA Z\nB Y\nC Z\nC Y\nC Z\nC Z\nB Y\nA X\nC Z\nA X\nC Z\nC X\nA Z\nC Z\nA Z\nC Z\nB X\nC Z\nC Z\nC X\nC Z\nC Z\nA Z\nA Z\nB Z\nC X\nC Y\nC Y\nC Y\nA Z\nC X\nC Z\nA X\nC Z\nC Z\nC X\nA X\nA X\nC Z\nA X\nB Z\nC Y\nC Z\nC Y\nC Z\nC Y\nC Z\nA Z\nA X\nC Z\nB Y\nC Z\nC Z\nB X\nC Z\nB X\nC Z\nC Y\nA X\nA Z\nC X\nC Y\nC Z\nC X\nC Z\nB X\nA Z\nC X\nA Y\nB Z\nC Z\nB X\nC Z\nC Z\nC Y\nC Z\nA X\nB X\nC Z\nA Z\nC Z\nC Z\nC Z\nA X\nC Z\nA Y\nC Z\nC Z\nC Z\nC Y\nA Z\nC X\nA X\nC Y\nC Z\nA Z\nA Y\nC Z\nB Y\nC X\nA Z\nC Y\nC Z\nA X\nC Z\nA Z\nB X\nA Z\nC Z\nB Z\nA Z\nA Z\nC Y\nC Z\nA Y\nA Z\nC Y\nC Z\nC Z\nC Z\nC Y\nC X\nB X\nC Z\nB Y\nC X\nA Z\n" <> ...

Part 1

defmodule C do
  def compare(:rock, :paper), do: :second
  def compare(:rock, :scissors), do: :first
  def compare(:rock, :rock), do: :draw
  def compare(:paper, :rock), do: :first
  def compare(:paper, :scissors), do: :second
  def compare(:paper, :paper), do: :draw
  def compare(:scissors, :rock), do: :second
  def compare(:scissors, :paper), do: :first
  def compare(:scissors, :scissors), do: :draw

  def score(:first, rate), do: rate
  def score(:second, rate), do: 6 + rate
  def score(:draw, rate), do: 3 + rate

  # paper
  def decide(:rock, :win), do: 6 + 2
  # scissors
  def decide(:rock, :loose), do: 0 + 3
  # rock
  def decide(:rock, :draw), do: 3 + 1

  # scissors
  def decide(:paper, :win), do: 6 + 3
  # rock
  def decide(:paper, :loose), do: 0 + 1
  # paper
  def decide(:paper, :draw), do: 3 + 2

  # rock
  def decide(:scissors, :win), do: 6 + 1
  # paper
  def decide(:scissors, :loose), do: 0 + 2
  # scissors
  def decide(:scissors, :draw), do: 3 + 3
end
{:module, C, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:decide, 2}}
correct = 15

res =
  input_test
  |> String.split("\n", trim: true)
  |> Enum.map(fn s -> String.split(s, " ", trim: true) end)
  |> Enum.map(fn [foo, me] ->
    case foo,
      do:
        (
          "A" -> [:rock, me]
          "B" -> [:paper, me]
          "C" -> [:scissors, me]
        )
  end)
  |> Enum.map(fn [foo, me] ->
    case me,
      do:
        (
          "X" -> [foo, :rock, 1]
          "Y" -> [foo, :paper, 2]
          "Z" -> [foo, :scissors, 3]
        )
  end)
  |> Enum.map(fn [foo, me, rate] -> [C.compare(foo, me), rate] end)
  |> Enum.map(fn [result, rate] -> C.score(result, rate) end)
  |> Enum.sum()

IO.puts("Correct: #{correct}")
correct === res
Correct: 15
true
correct = 10718

res =
  input
  |> String.split("\n", trim: true)
  |> Enum.map(fn s -> String.split(s, " ", trim: true) end)
  |> Enum.map(fn [foo, me] ->
    case foo,
      do:
        (
          "A" -> [:rock, me]
          "B" -> [:paper, me]
          "C" -> [:scissors, me]
        )
  end)
  |> Enum.map(fn [foo, me] ->
    case me,
      do:
        (
          "X" -> [foo, :rock, 1]
          "Y" -> [foo, :paper, 2]
          "Z" -> [foo, :scissors, 3]
        )
  end)
  |> Enum.map(fn [foo, me, rate] -> [C.compare(foo, me), rate] end)
  |> Enum.map(fn [result, rate] -> C.score(result, rate) end)
  |> Enum.sum()

IO.puts("Correct: #{correct}")
correct === res
Correct: 10718
true

Intro - Part 2

The Elf finishes helping with the tent and sneaks back over to you. “Anyway, 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, and Z means you need to win. Good luck!”

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. The example above now goes like this:

  • 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 with Rock for a score of 1 + 6 = 7.

Now that you’re correctly decrypting the ultra top secret strategy guide, you would get a total score of 12.

Following the Elf’s instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide?

Solution - Part 2

correct = 12

res =
  input_test
  |> String.split("\n", trim: true)
  |> Enum.map(fn s -> String.split(s, " ", trim: true) end)
  |> Enum.map(fn [foo, me] ->
    case foo,
      do:
        (
          "A" -> [:rock, me]
          "B" -> [:paper, me]
          "C" -> [:scissors, me]
        )
  end)
  |> Enum.map(fn [foo, me] ->
    case me,
      do:
        (
          "X" -> [foo, :loose]
          "Y" -> [foo, :draw]
          "Z" -> [foo, :win]
        )
  end)
  |> Enum.map(fn [foo, result] -> C.decide(foo, result) end)
  |> Enum.sum()

IO.puts("Correct: #{correct}")
correct === res
Correct: 12
true
correct = 14652

res =
  input
  |> String.split("\n", trim: true)
  |> Enum.map(fn s -> String.split(s, " ", trim: true) end)
  |> Enum.map(fn [foo, me] ->
    case foo,
      do:
        (
          "A" -> [:rock, me]
          "B" -> [:paper, me]
          "C" -> [:scissors, me]
        )
  end)
  |> Enum.map(fn [foo, me] ->
    case me,
      do:
        (
          "X" -> [foo, :loose]
          "Y" -> [foo, :draw]
          "Z" -> [foo, :win]
        )
  end)
  |> Enum.map(fn [foo, result] -> C.decide(foo, result) end)
  |> Enum.sum()

IO.puts("Correct: #{correct}")
correct === res
Correct: 14652
true