Organizing Containers of Balls

  • + 0 comments

    Haskell:

    module Main where
    
    import Control.Monad (replicateM, replicateM_)
    import Data.List (sort, transpose)
    
    rowsums :: [[Integer]] -> [Integer]
    rowsums = map sum
    
    colsums :: [[Integer]] -> [Integer]
    colsums rows = map sum (transpose rows)
    
    testsums :: [[Integer]] -> Bool
    testsums rows = sort (rowsums rows) == sort (colsums rows)
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            n <- readLn :: IO Int
            rows <- replicateM n $ map read . words <$> getLine :: IO [[Integer]]
            putStrLn $ if testsums rows then "Possible" else "Impossible"