Skip to content

Instantly share code, notes, and snippets.

@t3h2mas
Created December 31, 2017 19:08
Show Gist options
  • Save t3h2mas/a792c210d7096e3fb8e71c7a429061d9 to your computer and use it in GitHub Desktop.
Save t3h2mas/a792c210d7096e3fb8e71c7a429061d9 to your computer and use it in GitHub Desktop.
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
@t3h2mas
Copy link
Author

t3h2mas commented Dec 31, 2017

note: captcha/1 was my first implementation, and captcha2/1 my second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment