{-# Language QuasiQuotes, ImportQualifiedPost #-}
module Main where
import Data.List (findIndex, tails)
import Data.Set qualified as Set
import Advent (format)
main :: IO ()
IO ()
main =
do [Char]
input <- [format|2022 6 %s%n|]
Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> [Char] -> Int
forall a. Ord a => Int -> [a] -> Int
solve Int
4 [Char]
input)
Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> [Char] -> Int
forall a. Ord a => Int -> [a] -> Int
solve Int
14 [Char]
input)
solve :: Ord a => Int -> [a] -> Int
solve :: forall a. Ord a => Int -> [a] -> Int
solve Int
n [a]
input = Int -> (Int -> Int) -> Maybe Int -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
forall a. HasCallStack => a
undefined (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+) (([a] -> Bool) -> [[a]] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex (Int -> [a] -> Bool
forall a. Ord a => Int -> [a] -> Bool
start Int
n) ([a] -> [[a]]
forall a. [a] -> [[a]]
tails [a]
input))
start :: Ord a => Int -> [a] -> Bool
start :: forall a. Ord a => Int -> [a] -> Bool
start Int
n [a]
xs = Set a -> Int
forall a. Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
xs)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n