Done day 8
[advent-of-code-22.git] / advent06 / Main.hs
1 -- Writeup at https://work.njae.me.uk/2022/12/06/advent-of-code-2022-day-6/
2
3 import AoC
4 import Data.List
5
6 -- test = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
7
8 main :: IO ()
9 main =
10 do dataFileName <- getDataFileName
11 text <- readFile dataFileName
12 -- print $ part1 test
13 -- print $ part2 test
14 print $ part1 text
15 print $ part2 text
16
17 part1 :: String -> Int
18 part1 = interestingPosition 4
19
20 part2 :: String -> Int
21 part2 = interestingPosition 14
22
23 interestingPosition :: Int -> String -> Int
24 interestingPosition n text = n + (fst packetPos)
25 where candidates = zip [0..] $ fmap (take n) $ tails text
26 packetPos = head $ dropWhile (hasSame . snd) candidates
27
28 allDifferent, hasSame :: String -> Bool
29 allDifferent cs = nub cs == cs
30 hasSame = not . allDifferent