From ff843ec879afb52a8c4f02e24f47311a99dca874 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Sun, 11 Dec 2022 23:02:26 +0100 Subject: [PATCH] Finished day 2 --- day2.livemd | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/day2.livemd b/day2.livemd index 65e3388..1dd7077 100644 --- a/day2.livemd +++ b/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