Advent of Code 2022 fan bonus

    As a quick bonus, user MBoffin on Reddit posted about a bonus puzzle their daughter made.

    It seemed like a good fit for Haskell, so I decided to have a go.

    Finding the duplicated characters slotted directly into Haskell's list- and stream-processing idioms, so I used Data.List's group function to split the string into a list of strings, then threw away all the groups that were only one character long.

    After that, it was a filter to pull out just the characters I wanted.

    I had to define my own isLetterIsh predicate to find letters and dashes, as needed for part 1.

    main :: IO ()
    main = 
      do  dataFileName <- getDataFileName
          text <- readFile dataFileName
          let duplicated = fmap head $ filter ((> 1) . length) $ group text
          putStrLn $ filter isLetterIsh duplicated
          putStrLn $ filter isDigit duplicated
    isLetterIsh :: Char -> Bool
    isLetterIsh c = (isLetter c) || (c == '-')


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

    Neil Smith

    Read more posts by this author.