Skip to content

Instantly share code, notes, and snippets.

@bobbywilson0
Created August 19, 2013 19:40
Show Gist options
  • Select an option

  • Save bobbywilson0/6273147 to your computer and use it in GitHub Desktop.

Select an option

Save bobbywilson0/6273147 to your computer and use it in GitHub Desktop.
{_, input} = File.read("input.txt")
rows = Enum.map(String.split(input, "\n"), fn(row) ->
Enum.map(String.split(row), fn(n) ->
{number, _} = String.to_integer(n)
number
end)
end)
defmodule MyList do
def max_product([], previous), do: previous
def max_product(list) do
[_ | tail] = list
max_product(tail, product(list))
end
def max_product(list, previous) do
[_ | tail] = list
max_product(tail, Enum.max([product(list), previous]))
end
def product(list) when length(list) > 4 do
Enum.reduce(
Enum.take(list, 4),
1,
fn(n, acc) -> n * acc end
)
end
def product(_), do: 0
end
IO.puts Enum.max(
Enum.map(rows, fn(row) -> MyList.max_product(row) end)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment