1
0
Fork 0
advent-of-code/2022/haskell/src/Aoc/Day6.hs
2022-12-10 09:22:17 -05:00

18 lines
609 B
Haskell

module Aoc.Day6 (solve) where
import Data.List (nub, tails)
-- https://stackoverflow.com/questions/27726739/implementing-an-efficient-sliding-window-algorithm-in-haskell#comment43882101_27733778
windows :: Int -> [a] -> [[a]]
windows n = foldr (zipWith (:)) (repeat []) . take n . tails
sequenceOffset :: Eq a => Int -> [a] -> Int
sequenceOffset len xs = len + (length $ takeWhile dupes $ windows len xs)
where
dupes ys = length ys > (length $ nub ys)
solve :: Integer -> String -> String
solve 1 input = show $ sequenceOffset 4 input
solve 2 input = show $ sequenceOffset 14 input
solve _ _ = "?"