this post was submitted on 05 Dec 2024
26 points (100.0% liked)

Advent Of Code

985 readers
31 users here now

An unofficial home for the advent of code community on programming.dev!

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.

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 1 year ago
MODERATORS
 

Day 5: Print Queue

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
[–] [email protected] 4 points 3 weeks ago (2 children)

Uiua

Well it's still today here, and this is how I spent my evening. It's not pretty or maybe even good, but it works on the test data...

spoilerUses Kahn's algorithm with simplifying assumptions based on the helpful nature of the data.

Try it here

Data ← ⊜(β–‘)βŠΈβ‰ @\n "47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
Rs   ← ≑◇(βŠœβ‹•βŠΈβ‰ @|)β–½βŠΈβ‰‘β—‡(β§»βŠšβŒ•@|)Data
Ps   ← β‰‘βš(βŠœβ‹•βŠΈβ‰ @,)β–½βŠΈβ‰‘β—‡(Β¬β§»βŠšβŒ•@|)Data

NoPred  ← βŠ’β–½:⟜(≑(=0/+βŒ•)βŠ™Β€)β—΄β™­βŸœβ‰‘βŠ£                # Find entry without predecessors.
GetLead ← ⊸(β–½:⟜(≑(Β¬/+=))βŠ™Β€)⟜NoPred             # Remove that leading entry.
Rules   ← β‡ŒβŠ‚βŠƒ(β‡ŒβŠ’Β°β–‘βŠ’|β‰‘Β°β–‘β†˜1)[░⍒(GetLead|β‰ 1⧻)] Rs # Repeatedly find rule without predecessors (Kaaaaaahn!).

Sorted   ← βŠββŠ—,Rules
IsSorted ← /Γ—>0≑/-β—«2βŠ—Β°β–‘: Rules
MidVal   ← ⊑:⟜(⌊÷ 2⧻)

β‡ŒβŠ•β–‘βŠΈβ‰‘IsSorted Ps        # Group by whether the pages are in sort order.
≑◇(/+≑◇(MidVal Sorted)) # Find midpoints and sum.

[–] [email protected] 3 points 3 weeks ago (2 children)

Does this language ever look pretty? Great for signaling UFOs though :D

[–] [email protected] 2 points 3 weeks ago

Those unicode code points won't use themselves.

[–] [email protected] 2 points 3 weeks ago

Ah, but the terseness of the code allows the beauty of the underlying algorithm to shine through :-)

[–] [email protected] 2 points 3 weeks ago* (last edited 3 weeks ago)

Oh my. I just watched yernab's video, and this becomes so much easier:

# Order is totally specified, so sort by number of predecessors,
# check to see which were already sorted, then group and sum each group.
Data ← ⊜(β–‘βŠœβ–‘βŠΈβ‰ @\n)⊸(Β¬β¦·"\n\n")"47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
Rs   ← ≑◇(βŠœβ‹•βŠΈβ‰ @|)Β°β–‘βŠ’Data
Ps   ← β‰‘βš(βŠœβ‹•βŠΈβ‰ @,)Β°β–‘βŠ£Data
βŠ•(/+≑◇(⊑⌊÷2⧻.))Β¬β‰‘β‰βŸœ:β‰‘βš(⊏⍏/+⊞(∈Rs⊟)..).Ps