2025 Day 3: Lobby

Kind of surprised that I’m opening this topic 12 hours after the problem opened!

I did this one in Ada first, and will port to HAC later. I first did Part 1 by tracking a “larger” and “smaller” battery and noting with a Boolean which appeared left of the other, but then I saw Part 2 and realized that a general method would satisfy both:

  • I track:

    • the sequence of batteries with the highest joltages, along with their positions, in an array, initializing to the first 2 (Part 1) or 12 (Part 2); and
    • the joltage and position of the battery with the minimum joltage in that sequence.
  • For each subsequent battery, I check whether I can increase the joltage by deactivating an existing battery and activating the new one in one of two ways.

    1. If some battery’s joltage is smaller than that of the battery immediately to its right.
    2. If Minimum_Battery.Joltage < New_Battery.Joltage.

    If either case is true, then we also have to re-identify the minimum.

I also worked out a generic method after seeing the second part. I saw that jcmoyer used the same technique I did but in a much more succinct way than I did.

Basically, I scan for the highest digit starting to the right of the previous one, but if after this digit there are still N digits left to determine, I make sure I don’t look at the last N digits (i.e. if I am scanning the first of 2 digits, I don’t look at the last digit, if I am scanning the first of 12 digits, I don’t look at the last 11).

1 Like

The first part was really easy! Did it in under 50 lines but the second part was also easy.

But I had to write a bit more tho…

Note: I only did this because I thought it would be funny. And it is LOL

1 Like

I didn’t have any trouble with these. I wrote a procedure that took a string consisting entirely of '1’ .. ‘9’ and found the first instance of the maximum Character and its index, and applied this to appropriate substrings of the input, twice for part one and 12 times for part two. Then it was simply a matter of concatenating the resulting characters and converting to a number.

I did something similar, but with a shift & add instead of concatenating the collected numbers.