Browse Source

Solve 2021 Day 1 Part 1 in elixir

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

3
.gitignore vendored

@ -1,2 +1,3 @@
main
target/
target/
input.txt

33
2021/Day1/Elixir/main.ex

@ -0,0 +1,33 @@
# iex()> Day1.load_file |> Day1.first
defmodule Day1 do
def load_file() do
File.read!("input.txt")
|> String.split("\n", trim: true)
|> Enum.map(fn n -> Integer.parse(n) end)
|> Enum.map(fn tup -> Kernel.elem(tup, 0) end)
end
@spec first([Integer]) :: Integer
def first(input) do
Enum.zip(0..length(input), input)
|> Enum.reduce(0, fn ({index, distance}, acc) ->
case index - 1 do
-1 ->
0
previousIndex ->
previous = Enum.at(input, previousIndex)
if distance > previous do
acc + 1
else
acc
end
end
end)
end
@spec second([Integer]) :: Integer
def second(input) do
:second
end
end
Loading…
Cancel
Save