Cleanup Day 6 - still bad naive algorithm
This commit is contained in:
33
src/Day6.hs
33
src/Day6.hs
@@ -1,27 +1,28 @@
|
||||
module Day6 (
|
||||
findMarker,
|
||||
findMarker',
|
||||
findEnd,
|
||||
findStart,
|
||||
isUnique,
|
||||
day6
|
||||
) where
|
||||
|
||||
findMarker :: String -> Maybe Int
|
||||
findMarker = findMarker' 4 0 ""
|
||||
findMarker :: Int -> String -> Maybe Int
|
||||
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 = findMarker' 14 0 ""
|
||||
|
||||
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
|
||||
findStart = findMarker 14
|
||||
|
||||
findEnd :: String -> Maybe Int
|
||||
findEnd = findMarker 4
|
||||
|
||||
isUnique :: (Eq a) => [a] -> Bool
|
||||
isUnique [] = True
|
||||
@@ -31,7 +32,7 @@ day6 :: IO ()
|
||||
day6 = do
|
||||
input <- readFile "ressources/day06-input"
|
||||
putStrLn "Day6"
|
||||
let offset = findMarker input
|
||||
let offset = findEnd input
|
||||
putStrLn ("Found end marker at " ++ show offset)
|
||||
let startOffset = findStart input
|
||||
putStrLn ("Found start marker at " ++ show startOffset)
|
||||
|
||||
@@ -11,21 +11,18 @@ spec =
|
||||
isUnique "abc" `shouldBe` True
|
||||
isUnique "abca" `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
|
||||
findMarker "abcb" `shouldBe` Nothing
|
||||
findMarker "abcabcb" `shouldBe` Nothing
|
||||
findEnd "abcb" `shouldBe` Nothing
|
||||
findEnd "abcabcb" `shouldBe` Nothing
|
||||
it "finds the marker in the beginning" $ do
|
||||
findMarker "abcd" `shouldBe` Just 4
|
||||
findMarker "aabcd" `shouldBe` Just 5
|
||||
findEnd "abcd" `shouldBe` Just 4
|
||||
findEnd "aabcd" `shouldBe` Just 5
|
||||
it "finds a marker" $ do
|
||||
findMarker "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 7
|
||||
findMarker "bvwbjplbgvbhsrlpgdmjqwftvncz" `shouldBe` Just 5
|
||||
findMarker "nppdvjthqldpwncqszvftbrmjlhg" `shouldBe` Just 6
|
||||
findMarker "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg" `shouldBe` Just 10
|
||||
findMarker "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" `shouldBe` Just 11
|
||||
findEnd "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 7
|
||||
findEnd "bvwbjplbgvbhsrlpgdmjqwftvncz" `shouldBe` Just 5
|
||||
findEnd "nppdvjthqldpwncqszvftbrmjlhg" `shouldBe` Just 6
|
||||
findEnd "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg" `shouldBe` Just 10
|
||||
findEnd "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" `shouldBe` Just 11
|
||||
describe "Part 2" $ do
|
||||
it "finds start of message marker" $ do
|
||||
findStart "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 19
|
||||
|
||||
Reference in New Issue
Block a user