Advent of Code 2022 day 6

    Day 6. I was wondering when tails would turn up!

    There is very little to this solution. The combination fmap (take n) $ tails message finds all n-character sliding windows of the message. All I have to do is throw away the ones with a repeated character (which nub handles), and somehow keep track of how many characters have been discarded (I zip the windows with the numbers [0..] to count how many preceeding characters there were.

    interestingPosition :: Int -> String -> Int
    interestingPosition n text = n + (fst packetPos)
      where candidates = zip [0..] $ fmap (take n) $ tails text
            packetPos = head $ dropWhile (hasSame . snd) candidates
    
    allDifferent, hasSame :: String -> Bool
    allDifferent cs = nub cs == cs
    hasSame = not . allDifferent

    The two parts are the same, apart from different window sizes.

    Code

    You can get the code from my locally-hosted Git repo, or from Gitlab.

    Neil Smith

    Read more posts by this author.