{-# LANGUAGE ScopedTypeVariables, LambdaCase #-}
module Main where {
import System.Environment(getArgs);
import Control.Exception(assert);
import Debug.Trace(trace);
import Data.Function((&));
import Control.Category((>>>));
import Prelude hiding((.),(>>));
--import System.IO(hSetBuffering,stdout,BufferMode(LineBuffering));
import Data.List;
import Control.Monad;
--import Data.Maybe;
--import qualified Data.Map as Map;
--import Data.Map(Map);
--import qualified Data.Set as Set;
--import Data.Set(Set);

-- to avoid the redundancy warning
trace_placeholder :: ();
trace_placeholder = (trace,assert) & (id >>> error) "trace_placeholder";

main :: IO();
main = getArgs >>= \case{
[n] -> deal_lines $ read n;
_ -> error "need number to deal";
};

deal_lines :: Integer -> IO ();
deal_lines n = getContents >>= (lines >>> unfoldr (deal1 n) >>> transpose >>> do_out);

deal1 :: Integer -> [a] -> Maybe ([a],[a]);
deal1 n l = if null l then Nothing else Just $ genericSplitAt n l;

do_out :: [[String]] -> IO();
do_out = zipWithM_ one_out (enumFrom 0);

one_out :: Integer -> [String] -> IO();
one_out n l = writeFile ("ns"++show n++".part") $ unlines l;
} --end
-- 4 threads, 401 minutes wall clock
