Browse Source

Solve 2021 Day 1 Part 2 in elixir

master
Garrit Franke 2 years ago
parent
commit
c53b6d5191
Signed by: garrit
GPG Key ID: 65586C4DDA55EA2C
  1. 19
      2021/Day1/Elixir/main.ex

19
2021/Day1/Elixir/main.ex

@ -26,8 +26,25 @@ defmodule Day1 do
end)
end
def sliding_window([first_depth, second_depth, third_depth | tail]) do
sum = first_depth + second_depth + third_depth
sliding_window([second_depth, third_depth] ++ tail, sum, 0)
end
def sliding_window([first_depth, second_depth, third_depth | tail], previous_sum, counter) do
sum = first_depth + second_depth + third_depth
new_counter = if sum > previous_sum do
counter + 1
else
counter
end
sliding_window([second_depth, third_depth] ++ tail, sum, new_counter)
end
def sliding_window(rest, previous_sum, counter), do: counter
@spec second([Integer]) :: Integer
def second(input) do
:second
sliding_window(input)
end
end

Loading…
Cancel
Save