this post was submitted on 13 Nov 2025
3 points (63.6% liked)

Advent Of Code

1124 readers
4 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

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/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] lwhjp@piefed.blahaj.zone 1 points 1 week ago* (last edited 1 week 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