Created
December 24, 2017 07:34
-
-
Save ynonp/010bde27c598a983a399872466fb2c0b 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
defmodule Day24 do | |
def strength(bridge) do | |
bridge | |
|> Enum.reduce(0, fn { p1, p2 }, acc -> acc + p1 + p2 end) | |
end | |
def find_max_bridge(start, []) do | |
{ start, strength(start) } | |
end | |
def find_max_bridge(start, free_blocks) do | |
next_port = Enum.at(start, 0) |> elem(0) | |
free_blocks | |
|> Enum.map(fn | |
{ pr, pl } when pl == next_port -> | |
find_max_bridge([ { pr, pl } | start], List.delete(free_blocks, { pr, pl })); | |
{ pr, pl } when pr == next_port -> | |
find_max_bridge([ { pl, pr } | start], List.delete(free_blocks, { pr, pl })); | |
_ -> find_max_bridge(start, []) | |
end) | |
|> Enum.sort_by( | |
fn x -> x end, | |
fn | |
{ b1, _ }, { b2, _ } when length(b1) > length(b2) -> true; | |
{ b1, s1 }, { b2, s2 } when length(b1) == length(b2) -> s1 > s2; | |
_, _ -> false | |
end | |
) | |
|> Enum.at(0) | |
end | |
end | |
free_blocks = IO.stream(:stdio, :line) | |
|> Stream.map(&String.trim/1) | |
|> Enum.map(&String.split(&1, "/")) | |
|> Enum.map(fn x -> x |> Enum.map(&String.to_integer/1) end) | |
|> Enum.map(&List.to_tuple/1) | |
Day24.find_max_bridge([{ 0, 0 }], free_blocks) | |
|> IO.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment