aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAviral Goel <goel.aviral@gmail.com>2020-09-29 08:02:22 -0400
committerAviral Goel <goel.aviral@gmail.com>2020-09-29 08:02:22 -0400
commit37525da6814151f7107cb9c42d72262e59f3434c (patch)
tree9c023b2062b45b4731800f5881bd675295759f14
parent37e9f9b26e307f7c9d0bbaf65000c50c4b694964 (diff)
downloadperlweeklychallenge-club-37525da6814151f7107cb9c42d72262e59f3434c.tar.gz
perlweeklychallenge-club-37525da6814151f7107cb9c42d72262e59f3434c.tar.bz2
perlweeklychallenge-club-37525da6814151f7107cb9c42d72262e59f3434c.zip
Add Haskell solutions for week 80
-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'