{-# Language QuasiQuotes, ImportQualifiedPost #-}
{-|
Module      : Main
Description : Day 6 solution
Copyright   : (c) Eric Mertens, 2022
License     : ISC
Maintainer  : emertens@gmail.com

<https://adventofcode.com/2022/day/6>

>>> :main + "mjqjpqmgbljsphdztnvjfqwrcgsmlb\n"
7
19
>>> :main + "bvwbjplbgvbhsrlpgdmjqwftvncz\n"
5
23
>>> :main + "nppdvjthqldpwncqszvftbrmjlhg\n"
6
23
>>> :main + "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg\n"
10
29
>>> :main + "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw\n"
11
26

-}
module Main where

import Data.List (findIndex, tails)
import Data.Set qualified as Set

import Advent (format)

-- |
-- >>> :main
-- 1909
-- 3380
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