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

solved part 2 of day 6

parent 000a393b
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,9 @@ type EnumeratedChar = (Int, Char) ...@@ -5,6 +5,9 @@ type EnumeratedChar = (Int, Char)
sample :: String sample :: String
sample = "mjqjpqmgbljsphdztnvjfqwrcgsmlb" sample = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
sample1 :: String
sample1 = "nppdvjthqldpwncqszvftbrmjlhg"
allDifferent :: (Eq a) => [a] -> Bool allDifferent :: (Eq a) => [a] -> Bool
allDifferent [] = True allDifferent [] = True
allDifferent (x:xs) = x `notElem` xs && allDifferent xs allDifferent (x:xs) = x `notElem` xs && allDifferent xs
...@@ -13,23 +16,37 @@ takeLeftRight :: Int -> [a] -> ([a], [a]) ...@@ -13,23 +16,37 @@ takeLeftRight :: Int -> [a] -> ([a], [a])
takeLeftRight n = taking . splitAt n takeLeftRight n = taking . splitAt n
where taking (left, right) = (take 4 $ reverse left, take 4 right) 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)
checkForMarker :: [EnumeratedChar] -> Int checkForMarker :: [EnumeratedChar] -> Int
checkForMarker l = if (length $ getString l) < 4 checkForMarker l = if (length $ getString l) < 4
then 0 then 0
else else
if allDifferent $ getString l if allDifferent $ getString l
then fst $ head l then fst $ head l
else
if allDifferent $ getString $ reverse l
then fst $ head $ reverse l
else 0 else 0
evalTuple :: ([EnumeratedChar], [EnumeratedChar]) -> (Int, Int) evalTuple :: ([EnumeratedChar], [EnumeratedChar]) -> (Int, Int)
evalTuple (x1, x2) = (checkForMarker $ reverse x1, checkForMarker x2) -- need to reverse it back somehow evalTuple (x1, x2) = (checkForMarker x1, checkForMarker x2)
getString :: [(Int, Char)] -> String getString :: [(Int, Char)] -> String
getString = snd . unzip getString = snd . unzip
solve :: String -> Int solve1 :: String -> Int
solve s = fst $ head $ filter (\(x, y) -> x > 0 && y > 0) $ map (\x -> evalTuple $ takeLeftRight x i) [1..(length s)-1] solve1 s = fst $ head $ filter (\(x, _) -> x > 0) $ map (\x -> evalTuple $ takeLeftRight x 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]
where i = zip [1..] s where i = zip [1..] s
main :: IO () main :: IO ()
main = readFile "./input.txt" >>= print . solve . head . lines main = do
input <- head <$> lines <$> readFile "./input.txt"
print $ solve1 input
print $ solve2 input
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment