1 -- Writeup at https://work.njae.me.uk/2022/12/06/advent-of-code-2022-day-6/
3 import System.Environment
6 -- test = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
10 do dataFileName <- getDataFileName
11 text <- readFile dataFileName
17 getDataFileName :: IO String
20 progName <- getProgName
21 let baseDataName = if null args
24 let dataFileName = "data/" ++ baseDataName ++ ".txt"
27 part1 :: String -> Int
28 part1 = interestingPosition 4
30 part2 :: String -> Int
31 part2 = interestingPosition 14
33 interestingPosition :: Int -> String -> Int
34 interestingPosition n text = n + (fst packetPos)
35 where candidates = zip [0..] $ fmap (take n) $ tails text
36 packetPos = head $ dropWhile (hasSame . snd) candidates
38 allDifferent, hasSame :: String -> Bool
39 allDifferent cs = nub cs == cs
40 hasSame = not . allDifferent