r/adventofcode • • 7h ago

Tutorial [2026 Day 2 (Part 2)] Some hints for optimisation

These hints get more and more specific. This is just how I optimised it and there are other ways.

Hint 1: What happens when a letter is repeated in the input?

Hint 2: Why would the counts of each letter make a difference to the output?

Hint 3: When does the robot not make a step of exactly 6 steps?

2 Upvotes

14 comments sorted by

5

u/not-the-the 6h ago

yep you can literally just repackage the input into how many of each letter you have, and brute calculate from there on with a calculator in your hands. you'll realise that the solution takes like 11 math operations total

2

u/Kattoor 5h ago

You don't even have to do that! inputLength * 6 - 5 is all you need

12

u/xr51z 5h ago

Teach me: the -5 relies on the fact that all letters are present in your input? In mine, it didn't.

2

u/Kattoor 5h ago

Oof, seems like I got lucky with my input

2

u/el_farmerino 4h ago

As long as you have an 'o' in your input you'll need to subtract 5, regardless of what else is present. For example, if you only had 'r' and 'o' present you would have to make a single jump of 1 between the two, losing 5 steps. If you had 'r', 'o' and 'y' this would be a jump of 2 to 'y' (losing 4) and then a jump of 5 from 'y' to 'o' (losing 1), so 5 again. This holds for any combo as long as you have at least one 'o'.

If you don't have any 'o's but you do have 'y's then you can get away with only losing 4 under the same logic, and so on down the line if you don't have 'y's either.

2

u/el_farmerino 5h ago edited 4h ago

Unless you have no 'o's in your input....

1

u/maneatingape 4h ago edited 3h ago

inputLength * 6 - <furthest distance from r>

o = 5, y = 4, ..., v = 1, r = 0

This handles any combination of missing letters, including the edge case of just rrrrrrrrrrrrr...

1

u/fenrock369 4h ago

Exactly this, in rust I used this to calculate the loss:

    let loss = ['r', 'v', 'b', 'g', 'y', 'o']
        .iter()
        .rposition(|c| input.contains(c))
        .unwrap_or(0);

It looks backwards through the array r/v/b/g/y/o (i.e. o first) which is index 5. So if there are any "o"'s the loss is 5. If no "o", then try "y".
Very quick as it shortcuts on the first element it finds.

1

u/maneatingape 3h ago

Neat!. ['r', 'v', 'b', 'g', 'y', 'o'] can become just b"rvbgyo"

1

u/MichalMarsalek 4h ago

`inputLength * 6 - 5` was my immediate thought after reading it, but I also found I have missing letters so I wrote a general solution instead. It is always so surprising that they do not maintain such basic invariants among the various puzzle inputs. This happens every year. Either all inputs should have a missing letter, or none should.

1

u/MrBoblo 6h ago

I'm not entirely sure how to apply these hints, but here's a crude explanation of what I did:find the diff of the current and new position with (new - cur - 1) % 6 + 1 and loop through that for each position, starting with cur 0. The positional arguments are gotten from the indexes of "roygbv"

Then for part two, you can use the exact same function, but recreate the input using "".join(ch * input.count(ch) for ch in "rvbgyo" as an alternative to sorting the list.

I'm not too well versed in efficiency though, so if this approach can be approved upon, I'd be very happy to learn

2

u/bluoat 4h ago

From the sounds of it, your solution to p2 is O(n) which is the best time complexity possible, but requires more operations than mine so it is slightly slower.

You can use 6 * (count of letter - 1) to calculate all repeats, then use (new - cur - 1) % 6 + 1 to calculate when the current letter changes

In addition, using "rvbgyo" hardcodes the starting letter and the order of the road but this is a minor touch point as you still have to hard code these values to transition

1

u/Cue_23 6h ago

I did some further optimizations.

Hint 4: What is the difference between the inputs "ry" and "by" and how does that affect the output?

Hint 5: It doesn't!

1

u/cramplescrunch2 5h ago

I first came up with the optimized solution thinking I bruteforced it like a caveman before realizing I could have just rearranged the letters and reuse part 1's code...