|
@@ -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)
|