Last active
November 24, 2016 03:19
-
-
Save PandaWhisperer/62a4593270310af34ea9d0479a82a112 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 Johnann do | |
def john(n) when n >= 1 do | |
john_and_ann(n)[:john] |> Map.values | |
end | |
def ann(n) when n >= 1 do | |
john_and_ann(n)[:ann] |> Map.values | |
end | |
def sum_john(n) when n >= 1 do | |
john(n) |> Enum.sum | |
end | |
def sum_ann(n) when n >= 1 do | |
ann(n) |> Enum.sum | |
end | |
defp john_and_ann(n, katas \\ %{john: %{0 => 0}, ann: %{0 => 1}}) do | |
k = map_size(katas.john) | |
if k < n do | |
john = Map.put(katas.john, k, k - katas.ann[katas.john[k-1]]) | |
ann = Map.put(katas.ann, k, k - john[katas.ann[k-1]]) | |
john_and_ann(n, %{ john: john, ann: ann }) | |
else | |
katas | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment