Ugly day 6 solution

This commit is contained in:
Jens Kadenbach
2022-12-06 10:54:58 +01:00
parent 29afc2ff7b
commit 3209fa9493
5 changed files with 78 additions and 0 deletions

37
src/Day6.hs Normal file
View File

@@ -0,0 +1,37 @@
module Day6 (
findMarker,
findMarker',
findStart,
isUnique,
day6
) where
findMarker :: String -> Maybe Int
findMarker = findMarker' 4 0 ""
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
isUnique :: (Eq a) => [a] -> Bool
isUnique [] = True
isUnique (c:cs) = c `notElem` cs && isUnique cs
day6 :: IO ()
day6 = do
input <- readFile "ressources/day06-input"
putStrLn "Day6"
let offset = findMarker input
putStrLn ("Found end marker at " ++ show offset)
let startOffset = findStart input
putStrLn ("Found start marker at " ++ show startOffset)

View File

@@ -7,6 +7,7 @@ import Day2
import Day3
import Day4
import Day5
import Day6
someFunc :: IO ()
someFunc = do
@@ -19,5 +20,7 @@ someFunc = do
day4
putStrLn "-----------"
day5
putStrLn "-----------"
day6