aboutsummaryrefslogtreecommitdiff
path: root/challenge-062
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-02-01 18:50:47 -0500
committerchirvasitua <stuart-little@users.noreply.github.com>2021-02-01 18:50:47 -0500
commitf06be0c25270b9a07a7c6d9b81fbc86007ea9b2f (patch)
tree256af9c8754b762bc615e48d5155945864f0991e /challenge-062
parentac60bddb13f96402c3026283fe223388ed54fc27 (diff)
downloadperlweeklychallenge-club-f06be0c25270b9a07a7c6d9b81fbc86007ea9b2f.tar.gz
perlweeklychallenge-club-f06be0c25270b9a07a7c6d9b81fbc86007ea9b2f.tar.bz2
perlweeklychallenge-club-f06be0c25270b9a07a7c6d9b81fbc86007ea9b2f.zip
1st commit on 062_haskell
Diffstat (limited to 'challenge-062')
-rwxr-xr-xchallenge-062/stuart-little/haskell/ch-1.hs27
-rwxr-xr-xchallenge-062/stuart-little/haskell/ch-2.hs34
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-062/stuart-little/haskell/ch-1.hs b/challenge-062/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..bf57550078
--- /dev/null
+++ b/challenge-062/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,27 @@
+#!/usr/bin/env runghc
+
+-- run <script> <optional '-u' flag> <space-separated list of file paths>
+
+import Data.Char (toLower)
+import Data.List (sortOn)
+import Data.List.Extra (nubSortOn)
+import Data.List.Split (splitOn)
+import System.Environment (getArgs)
+
+normEmail :: String -> (String,String)
+normEmail em = (map toLower dom, base) where
+ (base,dom) = (\(x:xs)-> (x,concat xs)) $ splitOn "@" em
+
+sortEmails :: Bool -> [String] -> [String]
+sortEmails flag = srt normEmail where
+ srt = if (flag == True)
+ then nubSortOn
+ else sortOn
+
+main = do
+ args <- getArgs
+ let (flag,paths) = if (head args == "-u")
+ then (True,tail args)
+ else (False,args)
+ addresses <- (mapM readFile paths) >>= return . concat . (map lines)
+ mapM_ putStrLn $ sortEmails flag addresses
diff --git a/challenge-062/stuart-little/haskell/ch-2.hs b/challenge-062/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..0dbdddc11c
--- /dev/null
+++ b/challenge-062/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,34 @@
+#!/usr/bin/env runghc
+
+{-
+run <script> <size N of N x N x N board, or nothing to default to N=2>
+
+Returns a list of three-coordinate positions for the queens, and is very slow for boards 4 x 4 x 4 or larger.
+-}
+
+import Data.List (group)
+import Data.Tree (Tree,unfoldTree,levels)
+import Safe (headDef)
+import System.Environment (getArgs)
+
+type Poss = [[Int]]
+
+board :: Int -> Int -> [[Int]]
+board dm n = sequence $ replicate dm [0..n-1]
+
+attacks :: (Eq a, Num a) => [a] -> [a] -> Bool
+attacks xs ys = (<=1) . length . group . filter (/=0) $ ls where
+ ls = abs <$> (zipWith (-) xs ys)
+
+elimAttacks :: (Eq a, Num a) => [[a]] -> [[a]] -> [[a]]
+elimAttacks xss yss = filter (\ys -> all (not . attacks ys) xss) yss
+
+nextNonAtt :: Poss -> Poss -> (Poss, [Poss])
+nextNonAtt brd poss = (poss, map (:poss) $ elimAttacks poss brd)
+
+quTree :: Int -> Int -> Tree Poss
+quTree dm n = unfoldTree (nextNonAtt $ board dm n) []
+
+main = do
+ n <- getArgs >>= return . (read::String->Int) . headDef "2"
+ print $ head $ last $ levels $ quTree 3 n