1
1
Fork 0
aoc2018/day1.exs

40 lines
946 B
Elixir
Raw Normal View History

defmodule DayOne do
2018-12-02 05:43:29 +00:00
def part_one(input) do
2018-12-07 13:39:04 +00:00
String.split(input, "\n") |> Enum.filter(&(&1 != ""))
|> Enum.map(&DayOne.parse_num/1)
2018-12-03 12:23:00 +00:00
|> List.foldr(0, &(&1 + &2))
end
2018-12-03 12:23:00 +00:00
def part_two(input),
2018-12-07 13:39:04 +00:00
do: part_two(String.split(input, "\n") |> Enum.filter(&(&1 != "")) |> Enum.map(&DayOne.parse_num/1), 0, [])
2018-12-03 12:23:00 +00:00
def part_two(input, acc, frequencies) do
[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,
2018-12-07 13:39:04 +00:00
else: part_two(tail ++ [head], freq, [freq | frequencies])
end
2018-12-03 12:23:00 +00:00
def parse_num("+" <> num) do
{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
{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
IO.puts(num)
0
end
end
2018-12-07 13:39:04 +00:00
{:ok, content} = File.read("input1.txt")
IO.puts("Part 1: #{DayOne.part_one(content)}")
IO.puts("Part 2: #{DayOne.part_two(content)}")