aboutsummaryrefslogtreecommitdiff
path: root/challenge-080
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-080')
-rw-r--r--challenge-080/aviral-goel/haskell/ch-1.hs23
-rw-r--r--challenge-080/aviral-goel/haskell/ch-2.hs24
2 files changed, 47 insertions, 0 deletions
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'