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

you are viewing a single comment's thread
view the rest of the comments
[–] 4 points 11 months ago

Uiua

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

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 ← ∩(βŠœβ‹•βŠΈβˆŠ+@0⇑10)Β°β–‘β‚‚βŠœβ–‘Β¬βŠΈβ¦·"\n\n"

Merge ← ⨬(
  ⨬(⊟        # -> distinct, keep both.
  | βŠ‚βŠ’βŸœ(β†₯∩⊣) # -> overlap, merge them.
  )β—‘(β‰€βŠ“βŠ£βŠ’)
| βŠ™β—Œ # -> inside, ignore it.
)β—‘(β‰€βˆ©βŠ£)

Ranges ← βŠ™β—Œβ₯⍜⊣(MergeβŠ™Β°βŠ‚)β—‘β‹…β§»βŠƒβ†™β†˜1β†β†―βˆž_2 # Merge pairs of ranges.

P₁ ← /+β‰‘βŒž(/β†₯β‰‘βŒŸ(β†§βŠ“βŒŸβ‰₯β‰€Β°βŠŸ))
Pβ‚‚ ← /+≑(+1/-)βŠ™β—Œ
βŠƒP₁ Pβ‚‚ Ranges Parse D
  • source