day 2 part 1

This commit is contained in:
Caleb Webber 2023-10-28 12:27:59 -04:00
parent 6aa3fbaa58
commit d84058d959
3 changed files with 104 additions and 4 deletions

View file

@ -8,10 +8,9 @@ defmodule AOCRunner do
Enum.reduce_while(
{nil, advent.init_state()},
fn line, acc -> line
|> advent.to_command()
|> then(fn command ->
|> then(fn line ->
{u, state} = acc
{u, advent.apply_command(command, state)}
{u, advent.execute(line, state)}
end)
|> then(fn {u, s} ->
if (line |> is_empty()) && (u |> is_empty()) do

101
lib/day2.ex Normal file
View file

@ -0,0 +1,101 @@
defmodule Day2 do
def init_state() do
score_with_points(0)
end
defp score_with_points(p) do
%{score: p}
end
defp parse_match(line) do
[p1, p2] = String.split(line, " ") |> Enum.map(&String.trim/1)
match = parse_match(p1, p2)
match
end
defp parse_match(left, right) do
{parse_move(left, :left), parse_move(right, :right)}
end
defp parse_move(move, side) do
case side do
:left -> case move do
"A" -> :rock
"B" -> :paper
"C" -> :scissors
end
:right -> case move do
"X" -> :rock
"Y" -> :paper
"Z" -> :scissors
end
end
end
defp points_from_player_move(match) do
{_, player_move} = match
case player_move do
:rock -> 1
:paper -> 2
:scissors -> 3
end
end
defp sort_(a, b) do
if a == b do
0
else
case a do
:rock -> case b do
:paper -> -1
:scissors -> 1
end
:paper -> case b do
:rock -> 1
:scissors -> -1
end
:scissors -> case b do
:rock -> -1
:paper -> 1
end
end
end
end
defp outcome(match) do
{opponent, player} = match
case sort_(player, opponent) do
1 -> :win
0 -> :draw
-1 -> :loss
end
end
defp points_from_outcome(match) do
case outcome(match) do
:win -> 6
:draw -> 3
:loss -> 0
end
end
def execute(line, state) do
if line |> String.trim() |> String.length == 0 do
state
else
match = parse_match(line)
score_with_points(
state.score +
points_from_player_move(match) +
points_from_outcome(match))
end
end
def get_answer(state) do
state.score
end
end

View file

@ -1 +1 @@
AOCRunner.run(Day1)
AOCRunner.run(Day2)