This commit is contained in:
ctsk
2023-08-31 08:34:48 +02:00
parent eb35f60231
commit 1242145bff
6 changed files with 35 additions and 0 deletions

29
src/Days/D06.hs Normal file
View File

@@ -0,0 +1,29 @@
module Days.D06 (
day,
) where
import Common
import Data.List (findIndex, nub, tails)
distinct :: (Eq a) => [a] -> Bool
distinct l = length l == length (nub l)
windows :: Int -> [a] -> [[a]]
windows n l
| length l < n = []
| otherwise = zipWith const (windowList l) [0 .. (length l - n)]
where
windowList :: [a] -> [[a]]
windowList = map (take n) . tails
detector :: (Eq a) => Int -> [a] -> Maybe Int
detector n = fmap (n +) . findIndex distinct . windows n
part1 :: String -> Maybe Int
part1 = detector 4
part2 :: String -> Maybe Int
part2 = detector 14
day :: Day
day = StringDay (\s -> Right (Answer <$> part1 s, Answer <$> part2 s))