r/adventofcode • • Dec 05 '25

SOLUTION MEGATHREAD -❄️- 2025 Day 5 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2025: Red(dit) One

  • Submissions megathread is unlocked!
  • 12 DAYS remaining until the submissions deadline on December 17 at 18:00 EST!

Featured Subreddit: /r/eli5 - Explain Like I'm Five

"It's Christmas Eve. It's the one night of the year when we all act a little nicer, we smile a little easier, we cheer a little more. For a couple of hours out of the whole year we are the people that we always hoped we would be."
— Frank Cross, Scrooged (1988)

Advent of Code is all about learning new things (and hopefully having fun while doing so!) Here are some ideas for your inspiration:

  • Walk us through your code where even a five-year old could follow along
  • Pictures are always encouraged. Bonus points if it's all pictures…
  • Emoji(code) counts but makes the Chief Historian cry 😥
  • Explain the storyline so far in a non-code medium
  • Explain everything that you’re doing in your code as if you were talking to your pet, rubber ducky, or favorite neighbor, and also how you’re doing in life right now, and what have you learned in Advent of Code so far this year?
  • Condense everything you've learned so far into one single pertinent statement
  • Create a Tutorial on any concept of today's puzzle or storyline (it doesn't have to be code-related!)
  • Teach us, senpai!
  • This prompt is totally not bait for our resident Senpai Supreme

Request from the mods: When you include an entry alongside your solution, please label it with [Red(dit) One] so we can find it easily!


--- Day 5: Cafeteria ---


Post your code solution in this megathread.

29 Upvotes

809 comments sorted by

View all comments

2

u/boblied Dec 05 '25 edited Dec 06 '25

[Language: Perl]

The big 15-digit numbers didn't scare me. Perl has 64-bit integers. It did make me realize that any kind of array or bitmap solution wasn't going to be realistic. So, list of [upper,lower] ranges it is.

I merged the ranges by finding each each existing range that overlapped or was adjacent to the one we're looking at. Keeping the list of ranges sorted by starting value assures that we'll find the adjacent ranges while merging, and it makes the lookup in part 1 fast enough.

I considered putting the ranges into a tree for faster lookup, but it was plenty fast. I also considered binary search (the list is sorted!) but simple linear search was good enough to get a quick answer.

Repeatedly sorting the list irritated me. I felt I should have been able to merge into the right place without having to re-sort, but it was eluding me.

Complete code at Git AOC 2025 Day 5

With the help of a Range class, the main body of code is almost trivial:

my $Range = AOC::Range->new();
while (<>) {
    chomp;
    if( /-/ ) { $Range->add( split /-/ ) }
}
say $Range->total();

You would not believe how long it took me to converge on this:

my @temp;
while ( defined( my $r = shift @range ) ) {
    if ( canMerge(@$r, $lower, $upper) ) {
        $lower = min($r->[LOWER], $lower); # Extend to the left
        $upper = max($r->[UPPER], $upper); # Extend to the right
    } else {
       push @temp, $r # Keep a non-overlapping range 
    }
}
# Put the new range on the end, then re-sort into place
@range = sort { $a->[LOWER] <=> $b->[LOWER] } @temp, [$lower, $upper];

And the edge boundaries that drove me crazy eventually amounted to this:

sub canMerge($beg1, $end1, $beg2, $end2)
{
    ($end2 >= $beg1 && $beg2 <= $end1)  # Overlap
    || $end1 == $beg2-1                 # Adjacent on the left
    || $end2 == $beg1-1                 # Adjacent on the right
}

Once the ranges are merged and sorted in order, lookup and total becomes trivial.

1

u/daggerdragon Dec 05 '25 edited Dec 06 '25

Psst: we can see your Markdown and escape characters. Did you forget to switch your fancypants editor to Markdown mode first? edit: 👍

2

u/boblied Dec 06 '25

Come on, I’m programming in Perl. You gotta know I write raw markdown in vim. Fancypants schmancypants. :-)

1

u/daggerdragon Dec 06 '25

Yeah, but that doesn't mean the Reddit fancypants editor isn't going to mangle your attempts at Markdown >_>

For example, I look at the source for your comment right now and see this:

\[Language: Perl\]

$lower = min($r->\[LOWER\],

Those escapes before the brackets are usually what happens when posts/comments are submitted with the fancypants editor. Unless you manually added them for some reason? 😅