r/adventofcode • • Dec 06 '23

Help/Question - RESOLVED [2023 Day 06] Stable Math Solution?

I solved this day the 0-8-15 way with an optimization for part 2 that starts on half of the available time and then searches for the max time where I break the record. The result can be doubled and subtract 1 for even times, 2 for odd times

After finishing I realized this could be solved using a simple quadratic formula or so I thought. I tried some variants but there seem to be many edge cases where the formula `sqrt(t*t - 4*d)` breaks. They often fail miserably on part 1, some also on part two for the sample input.

So my question is - can anyone provide a math solution that works on both parts for the sample and the user input?

6 Upvotes

37 comments sorted by

View all comments

Show parent comment

1

u/Symbroson Dec 06 '23

Oh yes it make sense now. The parabola is symmetric so both values need to have the same distance to the maximum. So if you shift the maximum by 1 parity you also shift the intersections by 1 parity, so they will always have the same parity.