Day 6
This commit is contained in:
29
src/Days/D06.hs
Normal file
29
src/Days/D06.hs
Normal 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))
|
||||
Reference in New Issue
Block a user