Browse Source

Solve 2021 Day 2 Part 2 in elixir

master
Garrit Franke 2 years ago
parent
commit
f066a1c400
Signed by: garrit
GPG Key ID: 65586C4DDA55EA2C
  1. 28
      2021/Day2/Elixir/dive.ex

28
2021/Day2/Elixir/dive.ex

@ -34,8 +34,30 @@ defmodule Dive do
|> dive
end
@spec second([Integer]) :: Integer
def second(input) do
:second
def dive_aimed(input) do
dive_aimed(input, {0, 0, 0})
end
def dive_aimed([], {depth, horizontal, _}), do: depth * horizontal
def dive_aimed(["forward", amount_str | tail], {depth, horizontal, aim}) do
amount = String.to_integer amount_str
dive_aimed(tail, {depth + aim * amount, horizontal + amount, aim})
end
def dive_aimed(["down", amount_str | tail], {depth, horizontal, aim}) do
amount = String.to_integer amount_str
dive_aimed(tail, {depth, horizontal, aim + amount})
end
def dive_aimed(["up", amount_str | tail], {depth, horizontal, aim}) do
amount = String.to_integer amount_str
dive_aimed(tail, {depth, horizontal, aim - amount})
end
@spec second() :: Integer
def second() do
Dive.load_file
|> dive_aimed
end
end

Loading…
Cancel
Save