Created
December 3, 2017 21:23
-
-
Save zgohr/89cf5d9e65561ae24c381169cbfc5d4b 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
inp | |
|> String.split("\n", trim: true) | |
|> Enum.map(&String.split(&1, "\t")) | |
|> Enum.map(fn a -> a |> Enum.map(&String.to_integer/1) end) | |
|> Enum.map(fn a -> [Enum.max(a), Enum.min(a)] end) | |
|> Enum.map(fn [a, b] -> a - b end) | |
|> Enum.sum() | |
|> IO.inspect() | |
defmodule Helper do | |
def combination(0, _), do: [[]] | |
def combination(_, []), do: [] | |
def combination(n, [x|xs]) do | |
(for y <- combination(n - 1, xs), do: [x|y]) ++ combination(n, xs) | |
end | |
end | |
inp | |
|> String.split("\n") | |
|> Enum.map(&String.split(&1, "\t")) | |
|> Enum.map(fn a -> a |> Enum.map(&String.to_integer/1) end) | |
|> Enum.map(&Helper.combination(2, &1)) | |
|> Enum.map(&Enum.map(&1, fn l -> l |> Enum.sort() |> Enum.reverse() end)) | |
|> Enum.map(&Enum.filter(&1, fn [a, b] -> rem(a, b) == 0 && a != b end)) | |
|> Enum.map(&Enum.map(&1, fn [a, b] -> a / b end)) | |
|> List.flatten() | |
|> Enum.sum() | |
|> IO.inspect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment