Skip to content

Instantly share code, notes, and snippets.

@CallumDenby
Created December 12, 2017 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CallumDenby/cee317750376cb50b29796f5a28aa361 to your computer and use it in GitHub Desktop.
Save CallumDenby/cee317750376cb50b29796f5a28aa361 to your computer and use it in GitHub Desktop.
defmodule AOC do
def compare([first, second], acc) when first == second, do: acc + first
def compare(_, acc), do: acc
def calculate(nums) do
nums
|> String.split("", trim: true)
|> Enum.map(&String.to_integer/1)
|> Stream.cycle
|> Stream.take(String.length(nums) + 1)
|> Stream.chunk_every(2, 1, :discard)
|> Enum.to_list
|> Enum.reduce(0, &compare/2)
end
end
"4443"
|> AOC.calculate
|> IO.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment