Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule AdventOfCode.Day01 do
def solve(part) when part == :one do
nums = parse_input()
init = delta(Enum.at(nums, 0), Enum.at(nums, -1))
Enum.chunk_every(nums, 2, 1, :discard)
|> Enum.reduce(init, fn([a, b], acc) ->
acc + delta(a, b)
end)
end
def solve(part) when part == :two do
nums = parse_input()
count = Enum.count(nums)
middle = Float.floor(count/2) |> Kernel.trunc
{ _, total } = Enum.reduce(nums, { 0, 0 }, fn(n, { index, total }) ->
b_index = rem((index + middle), count)
b = Enum.at(nums, b_index)
{ index + 1, total + delta(n, b) }
end)
total
end
defp parse_input do
File.read!("./data/day1.txt")
|> String.split("", trim: true)
|> Enum.map(&String.to_integer/1)
end
defp delta(a, b) when a == b do; a end
defp delta(_, _) do; 0 end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.