You are viewing a single comment's thread. Return to all comments →
Elixir solution 'Area of a Polygon'
defmodule Solution do def array_xy do IO.read(:stdio, :all) |> String.split("\n") |> Enum.drop(1) |> Enum.drop(-1) |> Enum.map(fn(pair) -> pair |> String.split |> Enum.map(&String.to_integer(&1)) end) end def left_rotate([h|t]), do: t ++ [h] def create_pairs(array) do array |> Enum.map(fn(x) -> List.duplicate(x, 2) end) |> List.flatten |> left_rotate |> left_rotate |> Enum.chunk_every(2) end def shoelace_formula_of_polygon_area(array) do res = array |> Enum.chunk_every(2) |> Enum.map(fn([[x1, y1], [x2, y2]]) -> x1*y2-x2*y1 end) |> Enum.reduce(0, fn(v, acc) -> acc + v end) |> abs 0.5*res end def main do array_xy |> create_pairs |> shoelace_formula_of_polygon_area |> IO.inspect(limit: :infinity) end end Solution.main
Seems like cookies are disabled on this browser, please enable them to open this website
Compute the Area of a Polygon
You are viewing a single comment's thread. Return to all comments →
Elixir solution 'Area of a Polygon'