aboutsummaryrefslogtreecommitdiff
path: root/challenge-080
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-29 13:18:58 +0100
committerGitHub <noreply@github.com>2020-09-29 13:18:58 +0100
commit0f50bf1e03970b42514876ebb74e1673a2e9a44b (patch)
tree77b847c6328731c20918d79ce7762751ac41a0b2 /challenge-080
parent861368348ac449d2515ce87f50790fb6010d9a93 (diff)
parent37525da6814151f7107cb9c42d72262e59f3434c (diff)
downloadperlweeklychallenge-club-0f50bf1e03970b42514876ebb74e1673a2e9a44b.tar.gz
perlweeklychallenge-club-0f50bf1e03970b42514876ebb74e1673a2e9a44b.tar.bz2
perlweeklychallenge-club-0f50bf1e03970b42514876ebb74e1673a2e9a44b.zip
Merge pull request #2408 from aviralg/master
Add Haskell solutions for week 80
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'