Created
August 19, 2013 19:40
-
-
Save bobbywilson0/6273147 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| {_, 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