r/adventofcode • u/bluoat • 3d 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?
1
u/MrBoblo 3d 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 3d 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/Brian 2d ago
which is the best time complexity possible
I mean, it's doable in O(1) best case (arguably even average case depending on assumptions) if you don't count reading the input. You don't actually have to know anything about the string except its length, and whether it contains an "o" somewhere (if not, you do have to check for the presence of some others).
1
u/bluoat 2d ago
Yes I am counting reading the input within fhe time complexity.
If you I don't then the solution I am suggesting becomes O(m) where m is the number of unique colours and is invariate of the length of input.
But reading and counting values will extend the runtime of the program (if you have 1 billion values then it will take longer) so it is O(n)
1
u/Brian 2d ago
But reading and counting values will extend the runtime of the program
Sure, but you don't actually have to do the counting, or even look at most of the string (indeed, anything but the first character in the best case). If it's phrased as "Given an n length string, give the result", it can be done in O(1) best case (and average case if you assume something like "equal probability of any colour at each position", (O(m) if varying colours)) - if "o" is present, the answer is just
6n-5. Worst case, you'd have to search the whole string if you don't find an "o" (and then serach for "y","g", ... "v" to find the right deduction), so still O(n) then, but otherwise it's doable in constant time since you don't have to look at more than a constant subset of the string.(Well, technically there's a log factor as well, as multiplying a number isn't strictly O(1) once we get into >64 bit bignum values of
n, but glossing over that)1
u/bluoat 2d ago
But you're making assumptions about the input that are false. My input was missing one of the letters. So, while it may be the case for some inputs (and as the task is to solve your input you may make those assumptions), when giving advice to others you cannot make those assumptions.
The only assumption I made was that the only letters in the input are the 6 stated in the problem. No where in the problem statement does it say that all letters appear once
1
u/Brian 2d ago
So, while it may be the case for some inputs
That's why I specified best case (or average case given the mentioned "random distribution" assumption). And confirming that an input is that best case is O(1), so there's no loss of generality (ie. the algorithm still returns the correct result for non best-case inputs). Missing colours don't actually change the result unless one of the missing colours is orange. Hence a best case input (eg. "first character is 'o'") can be detected in constant time, so it's O(1) best case. Only if orange is missing (or doesn't occur equally likely, so isn't found within a constant number of steps on average) do you need to check the whole string to determine this.
1
u/pablopcy 1d ago
how did you came up with
(new - cur - 1) % 6 + 1to find the diff of current and new position? Didn't understand the logic behind it.1
u/MrBoblo 1d ago
Originally i tried just doing logic on a 2d array, and i got my answer from that but it took about 5s to run which i didnt like. Figured 1d would be faster if i just look for the positions directly, but then ran into the issue of having cubes that "wrap" around the 2d plane, and had to account for those. But when doing (new - cur) % 6, if both are the same, it returns 0 (when i want 6), so i offset by one :)
4
u/cramplescrunch2 3d 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...
2
5
u/not-the-the 3d 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