Add solutions for 2022:2 Rock Paper Scissors
Verbose code, and with bad naming. Good enough for now.
This commit is contained in:
parent
fddb9ec042
commit
df5cfbd030
2 changed files with 123 additions and 0 deletions
96
2022-elixir/lib/solutions/day_02.ex
Normal file
96
2022-elixir/lib/solutions/day_02.ex
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
defmodule Aoc.Solution.Day02 do
|
||||||
|
import Aoc.Utils
|
||||||
|
|
||||||
|
@name "Day 2: Rock Paper Scissors"
|
||||||
|
@behaviour Solution
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def get_name, do: @name
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def present(solution), do: "Asumed strategy guide will grant #{solution} total score"
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def present_again(solution), do: "Actual strategy guide will grant #{solution} total score"
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def parse!(raw) do
|
||||||
|
raw |> split_lines() |> Enum.map(&parse_values/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def solve(rounds) do
|
||||||
|
rounds |> Enum.map(&asumed_score/1) |> Enum.sum()
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def solve_again(rounds) do
|
||||||
|
rounds |> Enum.map(&score/1) |> Enum.sum()
|
||||||
|
end
|
||||||
|
|
||||||
|
def asumed_score([opponent, "X"]) do
|
||||||
|
result =
|
||||||
|
case opponent do
|
||||||
|
"A" -> 3
|
||||||
|
"B" -> 0
|
||||||
|
"C" -> 6
|
||||||
|
end
|
||||||
|
|
||||||
|
result + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def asumed_score([opponent, "Y"]) do
|
||||||
|
result =
|
||||||
|
case opponent do
|
||||||
|
"A" -> 6
|
||||||
|
"B" -> 3
|
||||||
|
"C" -> 0
|
||||||
|
end
|
||||||
|
|
||||||
|
result + 2
|
||||||
|
end
|
||||||
|
|
||||||
|
def asumed_score([opponent, "Z"]) do
|
||||||
|
result =
|
||||||
|
case opponent do
|
||||||
|
"A" -> 0
|
||||||
|
"B" -> 6
|
||||||
|
"C" -> 3
|
||||||
|
end
|
||||||
|
|
||||||
|
result + 3
|
||||||
|
end
|
||||||
|
|
||||||
|
def score([opponent, "X"]) do
|
||||||
|
result =
|
||||||
|
case opponent do
|
||||||
|
"A" -> 3
|
||||||
|
"B" -> 1
|
||||||
|
"C" -> 2
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def score([opponent, "Y"]) do
|
||||||
|
result =
|
||||||
|
case opponent do
|
||||||
|
"A" -> 1
|
||||||
|
"B" -> 2
|
||||||
|
"C" -> 3
|
||||||
|
end
|
||||||
|
|
||||||
|
result + 3
|
||||||
|
end
|
||||||
|
|
||||||
|
def score([opponent, "Z"]) do
|
||||||
|
result =
|
||||||
|
case opponent do
|
||||||
|
"A" -> 2
|
||||||
|
"B" -> 3
|
||||||
|
"C" -> 1
|
||||||
|
end
|
||||||
|
|
||||||
|
result + 6
|
||||||
|
end
|
||||||
|
end
|
||||||
27
2022-elixir/test/solutions/day_02_test.exs
Normal file
27
2022-elixir/test/solutions/day_02_test.exs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
defmodule Day02Test do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest Aoc.Solution.Day02
|
||||||
|
import Aoc.Solution.Day02
|
||||||
|
|
||||||
|
@input ~s(
|
||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
||||||
|
)
|
||||||
|
|
||||||
|
test "02: Rock Paper Scissors, part 1" do
|
||||||
|
expected = 15
|
||||||
|
|
||||||
|
result = @input |> parse!() |> solve()
|
||||||
|
|
||||||
|
assert result == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "02: Rock Paper Scissors, part 2" do
|
||||||
|
expected = 12
|
||||||
|
|
||||||
|
result = @input |> parse!() |> solve_again()
|
||||||
|
|
||||||
|
assert result == expected
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Reference in a new issue