blob: e2b84d5f5b885ee446bace96d477c3eb7445149d (
plain)
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
|
import Data.Void (Void)
import Data.List (intercalate)
import Text.Megaparsec
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer (decimal)
import System.Environment (getArgs)
import Data.Maybe (fromMaybe)
rotateLeft :: [Int] -> Int -> [Int]
rotateLeft (x:xs) n =
rotateLeft' n (x:xs) []
where rotateLeft' 0 xs ys = xs ++ reverse ys
rotateLeft' n [] ys = error "invalid element index"
rotateLeft' n (x:xs) ys = rotateLeft' (n - 1) xs (x:ys)
rotations :: [Int] -> [Int] -> [[Int]]
rotations xs indices = map (rotateLeft xs) indices
type Parser = Parsec Void String
parseArray :: Parser [Int]
parseArray = char '[' *> space *> sepBy decimal (char ',' <* space) <* char ']'
parseArrays :: Parser ([Int], [Int])
parseArrays = do
xs <- parseArray
ys <- parseArray
return (xs, ys)
arrayToString :: String -> String -> String -> [Int] -> String
arrayToString left right sep xs = left ++ intercalate sep (map show xs) ++ right
main :: IO ()
main = do
input <- fmap concat getArgs
let zs = do
(xs, ys) <- parseMaybe parseArrays input
return $ rotations xs ys
let output = intercalate "\n" $ map (arrayToString "[" "]" ",") (fromMaybe [] zs)
putStrLn output
|