Finished day 2

This commit is contained in:
Erwin Boskma 2022-12-11 23:02:26 +01:00
parent 4379396e1d
commit ff843ec879
Signed by: erwin
SSH key fingerprint: SHA256:CyeNoWXd3kjX2Nwu6pDxxdS7OqmPVOy0NavA/KU/ntU

View file

@ -42,6 +42,23 @@ defmodule Util do
end)
end
def process2(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(fn entry ->
entry
|> String.split()
|> Enum.map(fn
"A" -> :rock
"B" -> :paper
"C" -> :scissors
"X" -> :lose
"Y" -> :draw
"Z" -> :win
end)
end)
end
# Check your opponent vs you
def check_pair(:rock, :rock), do: :draw
def check_pair(:rock, :paper), do: :win
@ -53,6 +70,17 @@ defmodule Util do
def check_pair(:scissors, :paper), do: :lose
def check_pair(:scissors, :scissors), do: :draw
# Check reverse
def check_pair(:rock, :lose), do: :scissors
def check_pair(:rock, :draw), do: :rock
def check_pair(:rock, :win), do: :paper
def check_pair(:paper, :lose), do: :rock
def check_pair(:paper, :draw), do: :paper
def check_pair(:paper, :win), do: :scissors
def check_pair(:scissors, :lose), do: :paper
def check_pair(:scissors, :draw), do: :scissors
def check_pair(:scissors, :win), do: :rock
def points(:win), do: 6
def points(:draw), do: 3
def points(:lose), do: 0
@ -74,7 +102,10 @@ end
defmodule Part2 do
def run(input) do
0
Util.process2(input)
|> Enum.map(fn [they, you] -> {you, Util.check_pair(they, you)} end)
|> Enum.map(fn {played, result} -> Util.points(played) + Util.points(result) end)
|> Enum.sum()
end
end
@ -104,7 +135,7 @@ C Z)
end
test "part 2" do
assert Part2.run(@input) === 0
assert Part2.run(@input) === 13693
end
end