Skip to content

Instantly share code, notes, and snippets.

@joeesteves
Last active May 18, 2022 20:25
Show Gist options
  • Save joeesteves/4cc0ec0736e2764a8d01d352b2582cc8 to your computer and use it in GitHub Desktop.
Save joeesteves/4cc0ec0736e2764a8d01d352b2582cc8 to your computer and use it in GitHub Desktop.
Elixir Challenge
defmodule Challenge do
def resolve(stocks,target) do
map = build_map(stocks, target)
count_matching(stocks, map)
end
defp build_map(stocks, target) do
Enum.reduce(stocks, %{}, fn stock, acc ->
Map.put(acc, (stock - target) * -1, true)
end)
end
defp count_matching(stocks, map) do
Enum.filter(stocks, fn stock -> Map.get(map, stock) end)
|> Enum.uniq()
|> Enum.count()
end
end
stocks = [5, 6, 9, 13, 11, 6, 6, 3, 3]
target = 12
IO.puts Challenge.resolve(stocks, target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment