Created
December 31, 2017 19:08
-
-
Save t3h2mas/a792c210d7096e3fb8e71c7a429061d9 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 One do | |
@moduledoc """ | |
advent of code, day 1 | |
""" | |
def captcha(nums) do | |
split_and_indexed = Integer.to_string(nums) | |
|> String.graphemes | |
|> Enum.map(fn x -> String.to_integer(x) end) | |
|> Enum.with_index | |
split_and_indexed | |
|> Enum.filter(fn x -> | |
{val, i} = x | |
n = case Enum.fetch(split_and_indexed, i + 1) do | |
{:ok, next} -> | |
next | |
:error -> | |
Enum.at(split_and_indexed, 0) | |
end | |
{nval, _} = n | |
val == nval | |
end) | |
|> Enum.map(fn x -> | |
{val, _} = x | |
val | |
end) | |
|> Enum.sum | |
end | |
def captcha2(nums) do | |
li = Integer.to_string(nums) | |
|> String.graphemes | |
|> Enum.map(&String.to_integer/1) | |
[start | _] = li | |
worker(li, 0, start) | |
end | |
def worker([head | []], acc, start_head) do | |
case head == start_head do | |
:true -> | |
acc + head | |
:false -> | |
acc | |
end | |
end | |
def worker([head | tail], acc, start_head) do | |
[next_head | _] = tail | |
acc = case head == next_head do | |
:true -> | |
acc + head | |
:false -> | |
acc | |
end | |
worker(tail, acc, start_head) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note:
captcha/1
was my first implementation, andcaptcha2/1
my second.