29 lines
745 B
Elixir
29 lines
745 B
Elixir
defmodule DayOne do
|
|
def part_one(input) do
|
|
String.split(input, "\n")
|
|
|> Enum.map(&DayOne.parseNum/1)
|
|
|> List.foldr(0, &(&1 + &2))
|
|
end
|
|
|
|
def part_two(input), do: partTwo(String.split(input, "\n") |> Enum.map(&DayOne.parseNum/1), 0, [])
|
|
|
|
def part_two(input, acc , frequencies) do
|
|
[head | tail] = input
|
|
freq = head + acc
|
|
# IO.puts("Got frequency #{freq}")
|
|
if Enum.member?(frequencies, freq), do: freq, else: partTwo(tail ++ [head], freq, [freq | frequencies])
|
|
end
|
|
|
|
def parse_num("+" <> num) do
|
|
{intVal, ""} = Integer.parse(num)
|
|
intVal
|
|
end
|
|
def parse_num("-" <> num) do
|
|
{intVal, ""} = Integer.parse(num)
|
|
0 - intVal
|
|
end
|
|
def parse_num(num) do
|
|
IO.puts(num)
|
|
0
|
|
end
|
|
end
|