Created
December 2, 2017 07:08
-
-
Save sasa1977/5ff11145bf6f0a0b5f0120c6318a9032 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
# http://adventofcode.com/2017/day/1 | |
defmodule Day1 do | |
def sum1(digits), do: | |
digits | |
|> Stream.concat(Enum.take(digits, 1)) | |
|> Stream.chunk_every(2, 1, :discard) | |
|> Stream.filter(&match?([el, el], &1)) | |
|> Stream.map(&hd(&1)) | |
|> Enum.sum() | |
def sum2(digits) do | |
{first_half, second_half} = Enum.split(digits, digits |> Enum.count() |> div(2)) | |
first_half | |
|> Stream.zip(second_half) | |
|> Stream.filter(&match?({el, el}, &1)) | |
|> Stream.map(&elem(&1, 0)) | |
|> Enum.sum() | |
|> Kernel.*(2) | |
end | |
def digits(string), do: | |
string | |
|> String.codepoints() | |
|> Stream.map(&String.to_integer/1) | |
end | |
System.argv |> hd() |> Day1.digits() |> Day1.sum1() |> IO.inspect() | |
System.argv |> hd() |> Day1.digits() |> Day1.sum2() |> IO.inspect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment