-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.hs
45 lines (31 loc) · 960 Bytes
/
encryption.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Main (main) where
import Data.Array
getRowsColumn :: Int -> (Int, Int)
getRowsColumn n =
iter 0 n
where
iter begin end
| begin == end - 1 = finalIter begin end
| otherwise =
iter' begin end $ (begin + end) `div` 2
finalIter begin end
| begin * begin >= n = (begin, begin)
| begin * end >= n = (begin, end)
| otherwise = (end, end)
iter' begin end middle
| middle * middle < n = iter middle end
| otherwise = iter begin middle
solve :: String -> String
solve str =
let
n = length str
(r, c) = getRowsColumn n
emptyCellsCount = r * c - n
arr = listArray ((0, 0), (r - 1, c - 1)) str
strs = [map (\i -> arr ! (i, j1)) [0..(r - 1)] | j1 <- [0..(c - 1 - emptyCellsCount)]] ++ [map (\i -> arr ! (i, j2)) [0..(r - 2)] | j2 <- [(c - emptyCellsCount)..(c - 1)]]
in
unwords strs
main :: IO ()
main = do
str <- getLine
putStrLn $ solve str