Uncommented Day 1 code, writeup to come!
This commit is contained in:
parent
a4c6480d3d
commit
c756cffce7
1 changed files with 29 additions and 0 deletions
29
day1.exs
Normal file
29
day1.exs
Normal file
|
@ -0,0 +1,29 @@
|
|||
defmodule DayOne do
|
||||
def partOne(input) do
|
||||
String.split(input, "\n")
|
||||
|> Enum.map(&DayOne.parseNum/1)
|
||||
|> List.foldr(0, &(&1 + &2))
|
||||
end
|
||||
|
||||
def partTwo(input), do: partTwo(String.split(input, "\n") |> Enum.map(&DayOne.parseNum/1), 0, [])
|
||||
|
||||
def partTwo(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 parseNum("+" <> num) do
|
||||
{intVal, ""} = Integer.parse(num)
|
||||
intVal
|
||||
end
|
||||
def parseNum("-" <> num) do
|
||||
{intVal, ""} = Integer.parse(num)
|
||||
0 - intVal
|
||||
end
|
||||
def parseNum(num) do
|
||||
IO.puts(num)
|
||||
0
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue