Bläddra i källkod

Cleanup Day 6 - still bad naive algorithm

main
Jens Kadenbach 2 år sedan
förälder
incheckning
95f13675dd
2 ändrade filer med 26 tillägg och 28 borttagningar
  1. 17
    16
      src/Day6.hs
  2. 9
    12
      test/Day6Spec.hs

+ 17
- 16
src/Day6.hs Visa fil

@@ -1,27 +1,28 @@
1 1
 module Day6 (
2
-  findMarker,
3
-  findMarker',
2
+  findEnd,
4 3
   findStart,
5 4
   isUnique,
6 5
   day6
7 6
 ) where
8 7
 
9
-findMarker :: String -> Maybe Int
10
-findMarker = findMarker' 4 0 ""
8
+findMarker :: Int -> String -> Maybe Int
9
+findMarker windowSize = findMarker' 0 ""
10
+  where
11
+  findMarker' :: Int -> String -> String -> Maybe Int
12
+  findMarker' offset window  []
13
+    | isUnique window && length window == windowSize = Just offset
14
+    | otherwise = Nothing
15
+  findMarker' offset window (c:cs)
16
+    | length window < windowSize = findMarker' (offset + 1) (c:window) cs
17
+    | not (isUnique window) = findMarker' (offset + 1) (c:init window) cs
18
+    | isUnique window = Just offset
19
+    | otherwise = Nothing
11 20
 
12 21
 findStart :: String -> Maybe Int
13
-findStart = findMarker' 14 0 ""
14
-
15
-findMarker' :: Int -> Int -> String -> String -> Maybe Int
16
-findMarker' windowSize offset window  []
17
-  | isUnique window && length window == windowSize = Just offset
18
-  | otherwise = Nothing
19
-findMarker' windowSize offset window (c:cs)
20
-  | length window < windowSize = findMarker' windowSize (offset + 1) (c:window) cs
21
-  | not (isUnique window) = findMarker' windowSize (offset + 1) (c:init window) cs
22
-  | isUnique window = Just offset
23
-  | otherwise = Nothing
22
+findStart = findMarker 14 
24 23
 
24
+findEnd :: String -> Maybe Int
25
+findEnd = findMarker 4
25 26
 
26 27
 isUnique :: (Eq a) => [a] -> Bool
27 28
 isUnique [] = True
@@ -31,7 +32,7 @@ day6 :: IO ()
31 32
 day6 = do
32 33
    input <- readFile "ressources/day06-input"
33 34
    putStrLn "Day6"
34
-   let offset = findMarker input
35
+   let offset = findEnd input
35 36
    putStrLn ("Found end marker at " ++ show offset)
36 37
    let startOffset = findStart input
37 38
    putStrLn ("Found start marker at " ++ show startOffset)

+ 9
- 12
test/Day6Spec.hs Visa fil

@@ -11,21 +11,18 @@ spec =
11 11
         isUnique "abc" `shouldBe` True
12 12
         isUnique "abca" `shouldBe` False
13 13
         isUnique "cabc" `shouldBe` False
14
-      it "finds the marker in the next iteration" $ do
15
-        findMarker' 4 3 "abc" "d" `shouldBe` Just 4
16
-        findMarker' 4 4 "bcd" "e" `shouldBe` Just 5
17 14
       it "finds nothing if nothing is there" $ do
18
-        findMarker "abcb" `shouldBe` Nothing
19
-        findMarker "abcabcb" `shouldBe` Nothing
15
+        findEnd "abcb" `shouldBe` Nothing
16
+        findEnd "abcabcb" `shouldBe` Nothing
20 17
       it "finds the marker in the beginning" $ do
21
-        findMarker "abcd" `shouldBe` Just 4
22
-        findMarker "aabcd" `shouldBe` Just 5
18
+        findEnd "abcd" `shouldBe` Just 4
19
+        findEnd "aabcd" `shouldBe` Just 5
23 20
       it "finds a marker" $ do
24
-        findMarker "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 7
25
-        findMarker "bvwbjplbgvbhsrlpgdmjqwftvncz" `shouldBe` Just 5
26
-        findMarker "nppdvjthqldpwncqszvftbrmjlhg" `shouldBe` Just 6
27
-        findMarker "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg" `shouldBe` Just 10
28
-        findMarker "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" `shouldBe` Just 11
21
+        findEnd "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 7
22
+        findEnd "bvwbjplbgvbhsrlpgdmjqwftvncz" `shouldBe` Just 5
23
+        findEnd "nppdvjthqldpwncqszvftbrmjlhg" `shouldBe` Just 6
24
+        findEnd "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg" `shouldBe` Just 10
25
+        findEnd "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" `shouldBe` Just 11
29 26
     describe "Part 2" $ do
30 27
       it "finds start of message marker" $ do
31 28
         findStart "mjqjpqmgbljsphdztnvjfqwrcgsmlb"`shouldBe` Just 19

Laddar…
Avbryt
Spara