Cleanup Day 6 - still bad naive algorithm
This commit is contained in:
29
src/Day6.hs
29
src/Day6.hs
@@ -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
|
||||||
findStart :: String -> Maybe Int
|
findMarker' :: Int -> String -> String -> Maybe Int
|
||||||
findStart = findMarker' 14 0 ""
|
findMarker' offset window []
|
||||||
|
|
||||||
findMarker' :: Int -> Int -> String -> String -> Maybe Int
|
|
||||||
findMarker' windowSize offset window []
|
|
||||||
| isUnique window && length window == windowSize = Just offset
|
| isUnique window && length window == windowSize = Just offset
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
findMarker' windowSize offset window (c:cs)
|
findMarker' offset window (c:cs)
|
||||||
| length window < windowSize = findMarker' windowSize (offset + 1) (c:window) cs
|
| length window < windowSize = findMarker' (offset + 1) (c:window) cs
|
||||||
| not (isUnique window) = findMarker' windowSize (offset + 1) (c:init window) cs
|
| not (isUnique window) = findMarker' (offset + 1) (c:init window) cs
|
||||||
| isUnique window = Just offset
|
| isUnique window = Just offset
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
|
|
||||||
|
findStart :: String -> Maybe Int
|
||||||
|
findStart = findMarker 14
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user