Quest 7: Namegraph

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

  • Amy@piefed.blahaj.zone
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    4 days ago

    Haskell

    A nice dynamic programming problem in part 3.

    import Data.List  
    import Data.List.Split  
    import Data.Map.Lazy qualified as Map  
    import Data.Maybe  
    
    readInput s =  
      let (names : _ : rules) = lines s  
       in (splitOn "," names, map readRule rules)  
      where  
        readRule s =  
          let [[c], post] = splitOn " > " s  
           in (c, map head $ splitOn "," post)  
    
    validBy rules name = all (`check` name) rules  
      where  
        check (c, cs) = all (`elem` cs) . following c  
        following c s = [b | (a : b : _) <- tails s, a == c]  
    
    part1 (names, rules) = fromJust $ find (validBy rules) names  
    
    part2 (names, rules) =  
      sum $ map fst $ filter (validBy rules . snd) $ zip [1 ..] names  
    
    part3 (names, rules) =  
      sum . map go . filter (validBy rules) $ dedup names  
      where  
        dedup xs =  
          filter (\x -> not $ any (\y -> x /= y && y `isPrefixOf` x) xs) xs  
        go n = count (length n) (last n)  
        gen 11 _ = 1  
        gen len c =  
          (if len >= 7 then (1 +) else id)  
            . maybe 0 (sum . map (count (len + 1)))  
            $ lookup c rules  
        count =  
          curry . (Map.!) . Map.fromList $  
            [ ((k, c), gen k c)  
              | k <- [1 .. 11],  
                c <- map fst rules ++ concatMap snd rules  
            ]  
    
    main = do  
      readFile "everybody_codes_e2025_q07_p1.txt" >>= putStrLn . part1 . readInput  
      readFile "everybody_codes_e2025_q07_p2.txt" >>= print . part2 . readInput  
      readFile "everybody_codes_e2025_q07_p3.txt" >>= print . part3 . readInput