defmodule Main do
def position(tick, depth) do
x = depth - 1
x - abs(x - rem(tick, 2*x))
end
def collisions(tick, pos, firewalls) do
firewalls |> Enum.map(&(position(tick+Enum.at(&1, 0), Enum.at(&1, 1)) == pos))
end
def has_collision(tick, pos, firewalls) do
collisions(tick, pos, firewalls) |> Enum.member?(true)
end
end
firewalls = :stdio
|> IO.read(:all)
|> String.split("\n")
|> Enum.map(&(String.trim(&1)))
|> Enum.filter(&(&1 != ""))
|> Enum.map(&(String.split(&1, ":") |> Enum.map(fn x -> String.to_integer(String.trim(x)) end)))
Stream.iterate(0, &(&1 + 1))
|> Stream.map(fn delay -> [delay, Main.has_collision(delay, 0, firewalls)] end)
|> Stream.drop_while(&(Enum.at(&1,1) == true))
|> Stream.take(1)
|> Stream.map(&(Enum.at(&1,0)))
|> Enum.join("")
|> IO.puts