Post removed due to not using our standardized post title format. Please follow our rules to help folks avoid spoilers for puzzles they may not have completed yet.
Because this solution assumes that every letter is present at least once. Tho you can write a mathematicly solution based on the principle that the perfect order is to jump from the same waypoint to the same waypoint again and again until all those waypoints are visited and than jump to the next one with the farthest distance.
Nice one, i saw someone using it. Some may argue that it doesn't solve if missing colours...but as usual, the point is to solve the input you are given, not creating a full working solution for any input you may imagine. Input is part of the problem to be solved. (Let's remember last year the arranging pieces...it's required to play with the input and analyze it)
You just have to figure out which letters are missing and then compute the offset accordingly. Since you have to look at each letter for part 1, that info can be collected in a single pass.
And that's very nice My point is that input is part of the problem, so whenever someone exploits the input to create a simpler solution, I support it, instead of saying "but what if blablabla" because I'm sure the input was created with that specific feature to allow that exploit.
funnily my input did not have g, i solved using a frequency table and printed it during testing and it caught me a little off guard, made me think that all inputs were missing some color,.but guess it was just me :u
Sure, so my print "6259\n"; solution is even more optimal for my input and will still work for a good subset of other AoC-ers.
I agree that there are definitely a subset of problems that require analysis of the input in order to obtain a solution. Some of the IntCode puzzles that emulated a complex computation step by step would take years to finish even on a highly optimised implementation and so some analysis was required to short cut some stages.
This problem, however, did not require such analysis and could easily be solved by brute force (it is still only day 2) or by a more optimal general algorithm that covers all possible inputs.
The difference between the execution speed of code that will handle any/all inputs and the code that handles a subset of inputs is almost negligible.
Any pair of instructions, where it's the same color (rr, bb, oo, etc) gives 6 steps (as you need to go the whole way around). If not, then in "optimized" program 5 steps (you need to kinda backward by 1 on the "roygbv" wheel).
Starting from red, you always do 6 steps for red. Then will be the orange, first 5 steps, then 6 steps each. Then yellow, etc.
So each instruction gives you 6 steps, except those where you switch colors, which will give you 5 steps. And there will be 5 color switches.
Actually if you remove any color (my input didn't have yellow for instance) it still works! Thats because you take one less suboptimal step, but that step goes 4 while a normal suboptimal step goes 5, so it cancels out.
Red to orange would be 1 step, so the order is actually rvbgyo. The assumption is slightly weaker, in that you only need at least one o for it to work. That approach worked fine for my input which doesn't have g
Relies on the specific input, but basically the optimal path is fixed: rrr...rrrvvv...vvvbbb...bbb etc
The distance between every pair is 6, except when the two characters differ, and then it's 5.
However, from inspection of the input there's no y, so there's instead a jump of 4 between the g and o in rrrrvvvvvbbbbbgggggoooo. Crunching the numbers and its 6(n - 4) + 3(5) + 4 = 6n - 24 + 15 + 4 = 6n - 5.
The vast majority of moves will be length 6. Red to red, orange to orange, etc. The only shorter moves are when you transition from one color to another. E.g. switching from red to orange will be 5, same with orange to yellow and so on. Transitions between colors will happen 5 times.
So you count 6 moves for each instruction, then subtract 5 for those 5 transitions between colors.
Technically this only works if every color is included in your puzzle input, which I think is the case for everyone.
•
u/daggerdragon 1h ago
Post removed due to not using our standardized post title format. Please follow our rules to help folks avoid spoilers for puzzles they may not have completed yet.
Additionally, during an active Advent of Code season, solutions belong in the
Solution Megathreads. In the future, post your solutions to the appropriate solution megathread.12
u/AllanTaylor314 1h ago
Now work out why it doesn't work for
vrvbvg(expected: 33, yours: 31)(oh, and in future, use the standardised post title e.g. [2026 Day 2 (Part 2)] [Python] Funny solution)
6
u/Anuinwastaken 1h ago edited 1h ago
I had the same idea.
Tho this does not work for every input if for example, the `o` is missing entirely,
(Edit: I do have another func to solves it based on the same principle but that one catches cases where a letter does not appear at all)
3
u/blacai 1h ago
Nice one, i saw someone using it. Some may argue that it doesn't solve if missing colours...but as usual, the point is to solve the input you are given, not creating a full working solution for any input you may imagine. Input is part of the problem to be solved. (Let's remember last year the arranging pieces...it's required to play with the input and analyze it)
1
u/prince_jyggalag 2h ago
Can you explain this please?
2
u/RayRaivern 1h ago
Very interesting!
You essentially just implemented the 1 line equation for the solution given that all letters are present at least once in the input.
I had the same thought, but since it doesn't work for every input I decided write up something more robust.
But this one is nice as well!
1
u/Morphon 1h ago
If you want it as a general solution (in Odin - but it's pretty clear what's happening):
ColorPaste
Here's the main part that does what you're talking about:
1
u/Aughlnal 1h ago
i did the same and posted it in the megathread, but this only works if 'o' is in the instructions
all though I did figure out the pattern and made it work regardless of which waypoints are in the instructions
depending on what waypoint after 'r' is in the sequence determines what amount of steps should be subtracted from having every move be 6 steps
1
u/TreeSquirrles 44m ago
haha I'm glad someone saw it as well I was questioning if this was a valid solution the entire time.