Skip to content

Instantly share code, notes, and snippets.

@ggb
Created November 24, 2013 21:47
Show Gist options
  • Save ggb/7632813 to your computer and use it in GitHub Desktop.
Save ggb/7632813 to your computer and use it in GitHub Desktop.
defmodule DataMuging do
def processLines [], result, _cols do
result
end
def processLines [head | tail], result, { fst, scd, thd } do
if Regex.match?(%r/^\s*\d+.?\s+/, head) do
s = String.split head
processLines tail, [ { Enum.at(s, fst),
Enum.at(s, scd) |> String.replace("*", ""),
Enum.at(s, thd) |> String.replace("*", "") } | result ], { fst, scd, thd }
else
processLines tail, result, { fst, scd, thd }
end
end
def evalTriple data do
data |> Enum.min_by (fn( { _x, y, z } ) -> abs(binary_to_integer(y) - binary_to_integer(z)) end)
end
def main file, cols do
case File.read file do
{ :ok, contents } -> String.split(contents, "\n") |> processLines([], cols) |> evalTriple
{ :error, reason } -> reason
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment