Skip to content

Instantly share code, notes, and snippets.

@samidarko
Last active March 8, 2018 09:53
Show Gist options
  • Save samidarko/af40c19c1ef729e8e291d25bc532d06c to your computer and use it in GitHub Desktop.
Save samidarko/af40c19c1ef729e8e291d25bc532d06c to your computer and use it in GitHub Desktop.
dentify whether four sides (given by four integers) can form a square, a rectangle, or neither.
import Data.List.Split (splitOn)
-- polygons.txt
-- 36 30 36 30
-- 15 15 15 15
-- 46 96 90 100
-- 86 86 86 86
-- 100 200 100 200
-- -100 200 -100 200
data Shape = Polygon | Rectangle | Square deriving Show
data Result = Result {polygons::Int, rectangles::Int, squares:: Int} deriving Show
fn xs = case xs of
(a:b:c:d:t) -> let allPositive = all (>0) xs
isRectangle = a == c && b == d
isSquare = isRectangle && a == b
in if not allPositive
then Polygon
else if isSquare then Square
else if isRectangle then Rectangle
else Polygon
_ -> Polygon
update :: Shape -> Result -> Result
update s r = case s of
Rectangle -> r { rectangles = rectangles r + 1 }
Square -> r { squares = squares r + 1 }
Polygon -> r { polygons = polygons r + 1 }
main :: IO ()
main = do
content <- readFile "polygons.txt"
let parsed = map ( map (\x -> read x :: Int) . splitOn " ") $ lines content
let result = map fn parsed
-- let r = Result 0 0 0
let count = foldl (\r s -> update s r) (Result 0 0 0) result
putStrLn $ show count -- Result {polygons = 2, rectangles = 2, squares = 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment