Last active
March 8, 2018 09:53
-
-
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.
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
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