aboutsummaryrefslogtreecommitdiff
path: root/challenge-064
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-09-17 12:50:08 +1000
committerMyoungjin JEON <jeongoon@gmail.com>2020-09-17 12:50:08 +1000
commite54c743d3296903ffd1c41d83256d0d1ff8b4076 (patch)
tree920537ada67c95af3b18db5aefddc77c236c3f6d /challenge-064
parentbf89defb8c6d7583086f7387e557399e16e6ff27 (diff)
downloadperlweeklychallenge-club-e54c743d3296903ffd1c41d83256d0d1ff8b4076.tar.gz
perlweeklychallenge-club-e54c743d3296903ffd1c41d83256d0d1ff8b4076.tar.bz2
perlweeklychallenge-club-e54c743d3296903ffd1c41d83256d0d1ff8b4076.zip
[ch-064/jeongoon] Haskell solution added.
Diffstat (limited to 'challenge-064')
-rw-r--r--challenge-064/jeongoon/haskell/ch-1.hs58
-rw-r--r--challenge-064/jeongoon/haskell/ch-2.hs16
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-064/jeongoon/haskell/ch-1.hs b/challenge-064/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..a355bee3f4
--- /dev/null
+++ b/challenge-064/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,58 @@
+import System.IO
+import Data.List (all, nub, groupBy, sortOn,
+ replicate, permutations, mapAccumL)
+import Data.Char (isNumber)
+
+{- tested with:
+echo -e "[1 2 3][4 5 6][7 8 9]" | runhaskell ch-1.hs
+-}
+
+data LinearMatrix2D = LinearMatrix2D { getNumRows :: Int,
+ getNumCols :: Int,
+ getVector :: [Int] } deriving (Eq, Show)
+
+getLinearMatrixFromStdin = fmap (parseLinearMatrix.getMatrixLines) getContents where
+ getMatrixLines = filter (all (`elem` " "++"0123456789")) .
+ groupBy (\a b -> (isNumber a) && b `notElem` "]\n")
+ -- groupBy get two vars to compare to determine
+ -- whether thery are the same or not.
+ parseLinearMatrix mlines =
+ let vec = parseAsArray mlines in
+ LinearMatrix2D { getNumRows = length mlines,
+ getNumCols = (length vec `div` length mlines),
+ getVector = vec }
+ parseAsArray :: [String] -> [Int]
+ parseAsArray = foldr (\x acc -> (parseEachLine x) ++ acc) []
+ parseEachLine = map (\c -> read c :: Int ) . filter (all isNumber)
+ . groupBy (\a b -> all isNumber [a,b])
+
+type Sum = Int
+type Trace = [Int]
+type PathOffset = [Int]
+
+getAllPathSum :: LinearMatrix2D -> [(Sum, Trace)]
+getAllPathSum lm = (map (\t -> (sum t, t)) . map getTrace) allPaths where
+ matvec = getVector lm -- lm: linear matrix
+ allPaths :: [PathOffset]
+ allPaths = {- a simple way to make
+ (combi: choose n from m) X (combi: choose n' from m') -}
+ (nub.permutations)
+ ((replicate numColOffset 1) ++ (replicate numRowOffset rowOffset))
+
+ rowOffset = getNumCols lm
+ numColOffset = (getNumCols lm) -1
+ numRowOffset = (getNumRows lm) -1
+ getTrace :: PathOffset -> Trace
+ getTrace path = snd getTrace' ++ [ (head.fst) getTrace' ] where
+ getTrace' = mapAccumL (\l b -> let l' = drop b l in (l', head l))
+ matvec path
+
+main = do
+ lmatrix <- getLinearMatrixFromStdin
+ putStrLn "All Possible ways are:"
+ mapM_ print $ getAllPathSum lmatrix
+ let minPaths = (head.groupBy (\(a,_) (b,_) -> a == b).
+ sortOn fst.getAllPathSum) lmatrix in do
+ putStrLn "All possible minimum sum paths are:"
+ mapM_ print minPaths
+ putStr $ "Therefore the minimum sum is: " ++ ((show.fst.head) minPaths)
diff --git a/challenge-064/jeongoon/haskell/ch-2.hs b/challenge-064/jeongoon/haskell/ch-2.hs
new file mode 100644
index 0000000000..626ae2d27e
--- /dev/null
+++ b/challenge-064/jeongoon/haskell/ch-2.hs
@@ -0,0 +1,16 @@
+import System.Environment
+import System.Exit
+import Data.List (nub, intersect, subsequences)
+
+main = do
+ args <- getArgs
+ if length args < 2 then die "[WRN] 0 as No data given."
+ else
+ let s = head args
+ ws = tail args
+ res = nub $ intersect (subsequences s) ws
+ in do
+ putStrLn $ "[INF] $S = " ++ (show s)
+ putStrLn $ "[INF] @W = " ++ (show ws)
+ if (length res == 0) then putStrLn "0 as none matching word found."
+ else putStrLn (show res)