Finished day 2
This commit is contained in:
parent
4379396e1d
commit
ff843ec879
1 changed files with 33 additions and 2 deletions
35
day2.livemd
35
day2.livemd
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue