From b033d3eca7dbd1c563ba1be92a0eb76cba2911c5 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 1 Dec 2018 03:28:56 -0300 Subject: [PATCH] add day 1 solutions --- day1/day1.exs | 10 ++++++++ day1/day1_2.exs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 day1/day1.exs create mode 100644 day1/day1_2.exs diff --git a/day1/day1.exs b/day1/day1.exs new file mode 100644 index 0000000..3c60845 --- /dev/null +++ b/day1/day1.exs @@ -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}" diff --git a/day1/day1_2.exs b/day1/day1_2.exs new file mode 100644 index 0000000..1869074 --- /dev/null +++ b/day1/day1_2.exs @@ -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}"