{-# Language Safe #-}
module Intcode.Parse (parseInts) where
parseInts ::
String ->
Maybe [Int]
parseInts :: String -> Maybe [Int]
parseInts String
str
| [(Int
i,String
str1)] <- ReadS Int
forall a. Read a => ReadS a
reads String
str = [Int] -> String -> Maybe [Int]
parseInts' [Int
i] String
str1
| [(String
"",String
"")] <- ReadS String
lex String
str = [Int] -> Maybe [Int]
forall a. a -> Maybe a
Just []
| Bool
otherwise = Maybe [Int]
forall a. Maybe a
Nothing
parseInts' ::
[Int] ->
String ->
Maybe [Int]
parseInts' :: [Int] -> String -> Maybe [Int]
parseInts' [Int]
xs String
str =
case ReadS String
lex String
str of
[(String
",",String
str1)] | [(Int
x,String
str2)] <- ReadS Int
forall a. Read a => ReadS a
reads String
str1 -> [Int] -> String -> Maybe [Int]
parseInts' (Int
xInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:[Int]
xs) String
str2
[(String
"",String
"")] -> [Int] -> Maybe [Int]
forall a. a -> Maybe a
Just ([Int] -> [Int]
forall a. [a] -> [a]
reverse [Int]
xs)
[(String, String)]
_ -> Maybe [Int]
forall a. Maybe a
Nothing