Skip to content

Instantly share code, notes, and snippets.

@bbugh
Created February 1, 2016 19:14
Show Gist options
  • Save bbugh/41320af0cfc272229350 to your computer and use it in GitHub Desktop.
Save bbugh/41320af0cfc272229350 to your computer and use it in GitHub Desktop.
# Elixir solution for http://www.codewars.com/kata/dont-drink-the-water
defmodule Liquids do
@density %{ ?H => 1.36, ?W => 1.00, ?A => 0.87, ?O => 0.8 }
def separate_liquids([]), do: []
def separate_liquids([h|_] = glass) when is_list(h) and length(h) > 0 do
List.flatten(glass) |> Enum.sort_by(&(@density[&1])) |> Enum.chunk(length(h))
end
end
# Liquids.separate_liquids([]) == []
# Liquids.separate_liquids([['H', 'H', 'W', 'O'],['W','W','O','W'],['H','H','O','O']]) == ['OOOO', 'WWWW', 'HHHH']
# Liquids.separate_liquids(['HHWO', 'WWOW', 'HHOO']) == ['OOOO', 'WWWW', 'HHHH']
# Liquids.separate_liquids([['A'],['H'],['W'],['O']]) == ['O', 'A', 'W', 'H']
# Liquids.separate_liquids([['A','A','O','H'],['A', 'H', 'W', 'O'],['W','W','A','W'],['H','H','O','O']]) == ['OOOO', 'AAAA', 'WWWW', 'HHHH']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment