From 37525da6814151f7107cb9c42d72262e59f3434c Mon Sep 17 00:00:00 2001 From: Aviral Goel Date: Tue, 29 Sep 2020 08:02:22 -0400 Subject: Add Haskell solutions for week 80 --- challenge-080/aviral-goel/haskell/ch-1.hs | 23 +++++++++++++++++++++++ challenge-080/aviral-goel/haskell/ch-2.hs | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-080/aviral-goel/haskell/ch-1.hs create mode 100644 challenge-080/aviral-goel/haskell/ch-2.hs (limited to 'challenge-080') diff --git a/challenge-080/aviral-goel/haskell/ch-1.hs b/challenge-080/aviral-goel/haskell/ch-1.hs new file mode 100644 index 0000000000..c763a37560 --- /dev/null +++ b/challenge-080/aviral-goel/haskell/ch-1.hs @@ -0,0 +1,23 @@ +import Text.Megaparsec +import Data.Void (Void) +import Text.Megaparsec.Char +import Text.Megaparsec.Char.Lexer (signed, lexeme, decimal) +import System.Environment (getArgs) +import Data.Maybe (fromMaybe) +import Data.List ((\\)) + + +smallestPositiveInteger :: [Int] -> Int +smallestPositiveInteger xs = head ([1..] \\ xs) + +type Parser = Parsec Void String + +parseArray :: Parser [Int] +parseArray = char '(' *> space *> sepBy (signed space (lexeme space decimal)) (char ',' <* space) <* char ')' + +main :: IO () +main = do + input <- fmap concat getArgs + let array = parseMaybe parseArray input + let array' = fromMaybe [1..] array + putStrLn $ show $ smallestPositiveInteger array' diff --git a/challenge-080/aviral-goel/haskell/ch-2.hs b/challenge-080/aviral-goel/haskell/ch-2.hs new file mode 100644 index 0000000000..fae047a4f3 --- /dev/null +++ b/challenge-080/aviral-goel/haskell/ch-2.hs @@ -0,0 +1,24 @@ +import Text.Megaparsec +import Data.Void (Void) +import Text.Megaparsec.Char +import Text.Megaparsec.Char.Lexer (decimal) +import System.Environment (getArgs) +import Data.Maybe (fromMaybe) + + +candyCount :: [Int] -> Int +candyCount xs = length xs + count' xs (tail xs) + count' (tail xs) xs + where count' xs ys = sum $ map compare (zip xs ys) + compare (x, y) = if x > y then 1 else 0 + +type Parser = Parsec Void String + +parseArray :: Parser [Int] +parseArray = char '(' *> space *> sepBy decimal (char ',' <* space) <* char ')' + +main :: IO () +main = do + input <- fmap concat getArgs + let array = parseMaybe parseArray input + let array' = fromMaybe [1..] array + putStrLn $ show $ candyCount array' -- cgit