Day 5: Cafeteria

Megathread guidelines

  • 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

FAQ

  • mykl@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    11 days ago

    Uiua

    It took me a while to work out the merging of ranges, but I’m very pleased with this solution.

    merging ranges

    Merging all the ranges first makes both parts faster. Sorting the ranges first means that you only need to compare adjacent pairs of ranges to decide whether they need to remain separate, or can be merged. If the end of the next range is < end of this range you can drop it; if the start of the next range < end of this range replace this with a new range of (start of this range, max of both ends); otherwise just add next to the merged list and repeat…

    (EDIT: I realised that Part1 was really slow, so rewrote it, and total time dropped from 140ms to about 4ms, which is nice.)

    Run it here

    D  "3-5\n10-14\n16-20\n12-18\n\n1\n5\n8\n11\n17\n32"
    # D       &fras"2025day05.txt" # drop your input file on this pane and uncomment this line to test against your own data.
    Parse   (⊜⋕⊸∊+@010)°$"_\n\n_"          # split at \n\n; find all digits.
    Merge   ((⊟|⊂⊢⟜(↥∩⊣))(≤⊓⊣⊢)|⊙◌)(≤∩⊣) # distinct/overlap/contained.
    Ranges  ⊙◌⍥⍜⊣(Merge⊙°⊂)◡⋅⧻⊃↙↘1⍆↯∞_2      # Merge pairs of ranges.
    
    P₁  /+≠∩⌟1˜⊞≥)⍜⍉°⊟ # membership test
    P₂  /+≡(+1/-)⊙◌       # range counts
    &pnow(P₁ P₂ Ranges Parse D)