Skip to content

Instantly share code, notes, and snippets.

@samidarko
Created March 14, 2018 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samidarko/fc78a7d0fffab3012970e04c6402f6d3 to your computer and use it in GitHub Desktop.
Save samidarko/fc78a7d0fffab3012970e04c6402f6d3 to your computer and use it in GitHub Desktop.
Warning: this code is in progress and only works for triangles
import System.IO
-- lst = processInput ["1043 770","551 990","681 463"]
lst = processInput ["458 695","621 483","877 469","1035 636","1061 825","875 1023","645 1033","
485 853"]
-- area :: [Double] -> Double
area d@(a:b:c:[]) = let s = sum d / 2 in round $ sqrt $ (s * (s-a) * (s-b) * (s-c) )
processInput = map (tuplintfy . words)
strToDouble = read :: String -> Double
dist (x1, y1) (x2, y2) = sqrt ( (x1 - x2)^2 + (y1 - y2)^2 )
distances coords@(p:_) = fn coords
where fn (p1:p2:[]) = dist p1 p2 : dist p2 p : []
fn (p1:p2:ps) = dist p1 p2 : fn (p2:ps)
tuplintfy :: [String] -> (Double, Double)
tuplintfy [x,y] = (strToDouble x, strToDouble y)
main :: IO ()
main = do
n_temp <- getLine
let n = read n_temp :: Int
l <- getMultipleLines n
putStrLn $ (show . area .distances . processInput) l
getMultipleLines :: Int -> IO [String]
getMultipleLines n
| n <= 0 = return []
| otherwise = do
x <- getLine
xs <- getMultipleLines (n-1)
let ret = (x:xs)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment