Day 2
Setup
https://adventofcode.com/2022/day/2
defmodule Parse do
def input(inputString) do
inputString
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line
|> String.split()
|> List.to_tuple()
end)
end
end
defmodule Load do
def input do
File.read!(Path.join(Path.absname(__DIR__), "input/02.txt"))
end
end
realInput =
Load.input()
|> Parse.input()
testInput =
"""
A Y
B X
C Z
"""
|> Parse.input()
Part 1
Appreciative of your help yesterday, one Elf gives you an encrypted strategy guide (your puzzle input) that they say will be sure to help you win. "The first column is what your opponent is going to play: A for Rock, B for Paper, and C for Scissors. The second column--" Suddenly, the Elf is called away to help with someone's tent.
The second column, you reason, must be what you should play in response: X for Rock, Y for Paper, and Z for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen.
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, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won).
defmodule Part1 do
def playScore(mine) do
case mine do
"X" -> 1
"Y" -> 2
"Z" -> 3
end
end
def winScore(theirs, mine) do
%{
"A" => %{
"X" => 3,
"Y" => 6,
"Z" => 0
},
"B" => %{
"X" => 0,
"Y" => 3,
"Z" => 6
},
"C" => %{
"X" => 6,
"Y" => 0,
"Z" => 3
}
}[theirs][mine]
end
def solve(input) do
input
|> Enum.reduce(0, fn {theirs, mine}, acc ->
acc + playScore(mine) + winScore(theirs, mine)
end)
end
end
# Should be 15
Part1.solve(testInput)
Part1.solve(realInput)
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.
defmodule Part2 do
def choiceForOutcome(theirs, outcome) do
%{
# Rock
"A" => %{
# lose, play scissors
"X" => "Z",
# draw, play rock
"Y" => "X",
# win, play paper
"Z" => "Y"
},
# Paper
"B" => %{
# lose, play rock
"X" => "X",
# draw, play paper
"Y" => "Y",
# win, play scissors
"Z" => "Z"
},
# Scissors
"C" => %{
# lose, play paper
"X" => "Y",
# draw, play scissors
"Y" => "Z",
# win, play rock
"Z" => "X"
}
}[theirs][outcome]
end
def solve(input) do
input
|> Enum.map(fn {theirs, outcome} -> {theirs, choiceForOutcome(theirs, outcome)} end)
|> Part1.solve()
end
end
# Should equal 12
Part2.solve(testInput)
Part2.solve(realInput)