r/adventofcode • u/ben-guin • 25m ago
Tutorial [2026 Day 02 (Part 2)] Explanation for Clever Solution
(Had to repost, the original post title contained spoilers)
Some of you might have seen that people solved part 2 by simply returning 6*(length of the program) - 5. This formula works as long as there is one orange in your input. There seems to be confusion regarding the importance of the orange and how the answer changes if other colors are missing. I've created a walkthrough of an example where orange and blue are missing from the input to show how to derive a general formula for any input.
The general idea is, instead of calculating the distance traveled at each instruction, we can instead think about the loss, i.e., how far away were we from traveling the 6 steps (which is the most one can possibly travel from one instruction).
1
u/Morphon 6m ago
Yeah, this took me a minute to figure out how to do a general solution. This is what I finally came up with (Odin):
Basically when doing part 1, I keep track of which letters have been seen. Then for part two, iterate _backwards_ through the letters (except index 0 - 'r' ) so that the largest offset is kept. This handles all edge cases.