Cleanup Day 6 - still bad naive algorithm

This commit is contained in:
Jens Kadenbach
2022-12-06 11:01:54 +01:00
parent 3209fa9493
commit 95f13675dd
2 changed files with 26 additions and 28 deletions

View File

@@ -1,27 +1,28 @@
module Day6 ( module Day6 (
findMarker, findEnd,
findMarker',
findStart, findStart,
isUnique, isUnique,
day6 day6
) where ) where
findMarker :: String -> Maybe Int findMarker :: Int -> String -> Maybe Int
findMarker = findMarker' 4 0 "" findMarker windowSize = findMarker' 0 ""
where
findMarker' :: Int -> String -> String -> Maybe Int
findMarker' offset window []
| isUnique window && length window == windowSize = Just offset
| otherwise = Nothing
findMarker' offset window (c:cs)
| length window < windowSize = findMarker' (offset + 1) (c:window) cs
| not (isUnique window) = findMarker' (offset + 1) (c:init window) cs
| isUnique window = Just offset
| otherwise = Nothing
findStart :: String -> Maybe Int findStart :: String -> Maybe Int
findStart = findMarker' 14 0 "" findStart = findMarker 14
findMarker' :: Int -> Int -> String -> String -> Maybe Int
findMarker' windowSize offset window []
| isUnique window && length window == windowSize = Just offset
| otherwise = Nothing
findMarker' windowSize offset window (c:cs)
| length window < windowSize = findMarker' windowSize (offset + 1) (c:window) cs
| not (isUnique window) = findMarker' windowSize (offset + 1) (c:init window) cs
| isUnique window = Just offset
| otherwise = Nothing
findEnd :: String -> Maybe Int
findEnd = findMarker 4
isUnique :: (Eq a) => [a] -> Bool isUnique :: (Eq a) => [a] -> Bool
isUnique [] = True isUnique [] = True
@@ -31,7 +32,7 @@ day6 :: IO ()
day6 = do day6 = do
input <- readFile "ressources/day06-input" input <- readFile "ressources/day06-input"
putStrLn "Day6" putStrLn "Day6"
let offset = findMarker input let offset = findEnd input
putStrLn ("Found end marker at " ++ show offset) putStrLn ("Found end marker at " ++ show offset)
let startOffset = findStart input let startOffset = findStart input
putStrLn ("Found start marker at " ++ show startOffset) putStrLn ("Found start marker at " ++ show startOffset)

View File

@@ -11,21 +11,18 @@ spec =
isUnique "abc" `shouldBe` True isUnique "abc" `shouldBe` True
isUnique "abca" `shouldBe` False isUnique "abca" `shouldBe` False
isUnique "cabc" `shouldBe` False isUnique "cabc" `shouldBe` False
it "finds the marker in the next iteration" $ do
findMarker' 4 3 "abc" "d" `shouldBe` Just 4
findMarker' 4 4 "bcd" "e" `shouldBe` Just 5
it "finds nothing if nothing is there" $ do it "finds nothing if nothing is there" $ do
findMarker "abcb" `shouldBe` Nothing findEnd "abcb" `shouldBe` Nothing
findMarker "abcabcb" `shouldBe` Nothing findEnd "abcabcb" `shouldBe` Nothing
it "finds the marker in the beginning" $ do it "finds the marker in the beginning" $ do
findMarker "abcd" `shouldBe` Just 4 findEnd "abcd" `shouldBe` Just 4
findMarker "aabcd" `shouldBe` Just 5 findEnd "aabcd" `shouldBe` Just 5
it "finds a marker" $ do it "finds a marker" $ do
findMarker "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 7 findEnd "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 7
findMarker "bvwbjplbgvbhsrlpgdmjqwftvncz" `shouldBe` Just 5 findEnd "bvwbjplbgvbhsrlpgdmjqwftvncz" `shouldBe` Just 5
findMarker "nppdvjthqldpwncqszvftbrmjlhg" `shouldBe` Just 6 findEnd "nppdvjthqldpwncqszvftbrmjlhg" `shouldBe` Just 6
findMarker "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg" `shouldBe` Just 10 findEnd "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg" `shouldBe` Just 10
findMarker "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" `shouldBe` Just 11 findEnd "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" `shouldBe` Just 11
describe "Part 2" $ do describe "Part 2" $ do
it "finds start of message marker" $ do it "finds start of message marker" $ do
findStart "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 19 findStart "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 19