add day 1 solutions
This commit is contained in:
parent
584f3fe56b
commit
b033d3eca7
2 changed files with 74 additions and 0 deletions
10
day1/day1.exs
Normal file
10
day1/day1.exs
Normal file
|
@ -0,0 +1,10 @@
|
|||
# input treatment
|
||||
input = IO.gets("input?")
|
||||
|> String.trim
|
||||
|> String.split(" ")
|
||||
|> Enum.map(fn part ->
|
||||
String.to_integer part
|
||||
end)
|
||||
|
||||
res = Enum.sum(input)
|
||||
IO.puts "answer: #{res}"
|
64
day1/day1_2.exs
Normal file
64
day1/day1_2.exs
Normal file
|
@ -0,0 +1,64 @@
|
|||
input = IO.gets("input?")
|
||||
|> String.trim
|
||||
|> String.split(" ")
|
||||
|> Enum.map(fn part ->
|
||||
String.to_integer part
|
||||
end)
|
||||
|
||||
defmodule Day1_2 do
|
||||
def pass(input, state) do
|
||||
|
||||
# pass through the entire list with reduce
|
||||
# so we have some kind of state in acc
|
||||
result = Enum.reduce(input, state, fn part, acc ->
|
||||
case acc do
|
||||
|
||||
# if we already found it, we don't need
|
||||
# to keep iterating
|
||||
{:ok, res} ->
|
||||
{:ok, res}
|
||||
|
||||
# acc grows like this:
|
||||
# [0]
|
||||
# [0 + first_change | 0]
|
||||
|
||||
# [0 + first_change + second_change |
|
||||
# [0 + first_change | 0]], etc
|
||||
[current | _rest] ->
|
||||
# the head of the list is the current
|
||||
# frequency of the device that we'll
|
||||
# apply the change to (part)
|
||||
new_freq = current + part
|
||||
|
||||
# then we search in the OLD list
|
||||
# if we are a member of it
|
||||
#
|
||||
# if we are, then we are actually the first
|
||||
# repeated frequency in the list.
|
||||
became_member = Enum.member?(acc, new_freq)
|
||||
|
||||
if became_member do
|
||||
# this should make the reduce()
|
||||
# call stop creating new frequencies
|
||||
# and just returning what it found
|
||||
{:ok, new_freq}
|
||||
else
|
||||
# if we didn't find, we add the new frequency
|
||||
# to the list, and keep going.
|
||||
[new_freq | acc]
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, res} -> res
|
||||
|
||||
# if
|
||||
_ -> pass(input, result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
res = Day1_2.pass(input, [0])
|
||||
|
||||
IO.puts "total res: #{inspect res}"
|
Loading…
Reference in a new issue