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