Created
January 2, 2018 15:15
-
-
Save CallumDenby/a41faae6a7bf215e492f8b5f3062bd81 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
# Part One | |
defmodule DayOnePartOne do | |
def compare([number, number], acc), do: acc + number | |
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.reduce(0, &compare/2) | |
end | |
end | |
"4443" | |
|> Giggles.calculate | |
|> IO.inspect | |
# Part Two | |
defmodule DayOnePartTwo do | |
def compare({number, number}, acc), do: acc + number | |
def compare(_, acc), do: acc | |
def multiply(num1, num2), do: num1 * num2 | |
def calculate(nums) do | |
split = nums | |
|> String.length | |
|> div(2) | |
nums | |
|> String.graphemes | |
|> Enum.map(&String.to_integer/1) | |
|> Enum.split(split) | |
|> Tuple.to_list | |
|> Enum.zip | |
|> Enum.reduce(0, &compare/2) | |
|> multiply(2) | |
end | |
end | |
"1212" | |
|> Giggles.calculate | |
|> IO.inspect(label: "Result") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment