Created
December 12, 2017 17:35
-
-
Save CallumDenby/cee317750376cb50b29796f5a28aa361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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