Skip to content
Snippets Groups Projects
Commit 1d40fd0b authored by Felix Valentini's avatar Felix Valentini
Browse files

generalized takeLeftRight

parent 1a0e96e2
No related branches found
No related tags found
No related merge requests found
......@@ -2,23 +2,13 @@
type EnumeratedChar = (Int, Char)
sample :: String
sample = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
sample1 :: String
sample1 = "nppdvjthqldpwncqszvftbrmjlhg"
allDifferent :: (Eq a) => [a] -> Bool
allDifferent [] = True
allDifferent (x:xs) = x `notElem` xs && allDifferent xs
takeLeftRight :: Int -> [a] -> ([a], [a])
takeLeftRight n = taking . splitAt n
where taking (left, right) = (take 4 $ reverse left, take 4 right)
takeLeftRight' :: Int -> [a] -> ([a], [a])
takeLeftRight' n = taking . splitAt n
where taking (left, right) = (take 14 $ reverse left, take 14 right)
takeLeftRight :: Int -> Int -> [a] -> ([a], [a])
takeLeftRight splitIndex takeN = taking . splitAt splitIndex
where taking (left, right) = (take takeN $ reverse left, take takeN right)
checkForMarker :: [EnumeratedChar] -> Int
checkForMarker l = if (length $ getString l) < 4
......@@ -38,11 +28,11 @@ getString :: [(Int, Char)] -> String
getString = snd . unzip
solve1 :: String -> Int
solve1 s = fst $ head $ filter (\(x, _) -> x > 0) $ map (\x -> evalTuple $ takeLeftRight x i) [1..(length s)-1]
solve1 s = fst $ head $ filter (\(x, _) -> x > 0) $ map (\x -> evalTuple $ takeLeftRight x 4 i) [1..(length s)-1]
where i = zip [1..] s
solve2 :: String -> Int
solve2 s = fst $ head $ filter (\(x, _) -> x > 0) $ map (\x -> evalTuple $ takeLeftRight' x i) [1..(length s)-1]
solve2 s = fst $ head $ filter (\(x, _) -> x > 0) $ map (\x -> evalTuple $ takeLeftRight x 14 i) [1..(length s)-1]
where i = zip [1..] s
main :: IO ()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment