aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-126/stuart-little/haskell/ch-1.hs18
-rwxr-xr-xchallenge-126/stuart-little/haskell/ch-2.hs25
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-126/stuart-little/haskell/ch-1.hs b/challenge-126/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..3ad9b0fed8
--- /dev/null
+++ b/challenge-126/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,18 @@
+#!/usr/bin/env runghc
+
+-- run <script> <number>
+
+import Data.Char (digitToInt)
+import System.Environment (getArgs)
+
+no1 :: String -> Integer
+no1 nr
+ |length nr == 0 = 0
+ |head nr == '1' = 9^(length nr -1)
+ |otherwise = ((toInteger $ digitToInt $ head nr) - 1) * 9^(length nr -1) + (no1 $ tail nr)
+
+adjNo1 :: String -> Integer
+adjNo1 nr = if (not $ elem '1' nr) then (no1 nr) else (no1 nr - 1)
+
+main :: IO ()
+main = getArgs >>= putStrLn.show.adjNo1.head
diff --git a/challenge-126/stuart-little/haskell/ch-2.hs b/challenge-126/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..115b56e33c
--- /dev/null
+++ b/challenge-126/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,25 @@
+#!/usr/bin/env runghc
+
+-- run <script>
+
+{-# LANGUAGE QuasiQuotes #-}
+
+import System.Environment (getArgs)
+import Text.RawString.QQ
+
+nbrs :: [[a]] -> Int -> Int -> [(Int,Int)]
+nbrs mat i j = filter (\p-> (fst p /= i || snd p /= j) && (0 <= fst p) && (fst p < length mat) && (0 <= snd p) && (snd p < length (head mat))) $ map (\[x,y]->(i+x,j+y)) $ sequence $ replicate 2 [-1..1]
+
+mines :: [[String]] -> [[String]]
+mines xs = map (map (\(p,q,r)-> if r == "x" then "x" else show $ length $ filter (\nbr-> (xs !! (fst nbr)) !! (snd nbr) == "x") $ nbrs xs p q)) indexed where
+ indexed = map (\(i,ar)-> map (\(j,x) -> (i,j,x)) $ zip [0..] ar) $ zip [0..] xs
+
+inptStr :: String
+inptStr=[r|x * * * x * x x x x
+* * * * * * * * * x
+* * * * x * x * x *
+* * * x x * * * * *
+x * * * x * * * * x|]
+
+main :: IO ()
+main = putStrLn $ unlines $ map unwords $ mines $ map words $ lines inptStr