r/adventofcode • u/Symbroson • 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?
8
u/kroppyer Dec 06 '23 edited Dec 06 '23
Kotlin:
I'm using record + 1 as the minimum distance we need to achieve. When time is odd we need to "round up" to the nearest even value, and when time is even round up to the nearest odd value, so when time and width are both odd or both even, we add 1, otherwise add 0.
3
u/IsatisCrucifer Dec 06 '23 edited Dec 06 '23
Your "edge cases" most likely come from the problem is only asking about integers. This is why many people suggests to actually calculate the roots, do some floor/ceiling, then find the difference of the modified two roots, add 1 to get the answer.
But, there is actually a way to fix the approximation sqrt(t*t-4*d).
+1pending somewhere.So the final algorithm would be:
Bonus: I posted my solution implementing this algorithm in the megathread.
2
u/MagiMas Dec 06 '23 edited Dec 06 '23
Here's mine, works like a charm:
/E Sorry the Spoiler Tags don't seem to work on Code?
!! SPOILER !!
2
u/DeepDay6 Dec 06 '23 edited Dec 06 '23
The basic idea is to find an equation, calculate its roots and count the integers between those roots, because that's the full milliseconds at which the distance is higher than the record.
Plot your equation for the first example. You will see the roots are at ~1.6 and ~5.3. In this case,
5 - 2 + 1 == 4works.If you plot the third example, you will see the roots are exactly on 10.00 and 20.00. Suddenly only
20 - 10 + 1won't work, because those are full milliseconds where the distance is the same as the record.One way to solve the edge cases is in the veins of
x1' = if x1 == ceil x1 then (ceil x1) + 1 else ceil x1andx2' = if x2 == floor x2 then (floor x2) - 1 else floor x21
u/EnergyIsMassiveLight Dec 06 '23
have you made sure that if the range starts with t=9 for example, that it doesn't include 9 as a winning solution? (it would be tied) What i did was floor() that and +1 to account for it. also you mention formula sqrt(t^2 - 4d), just confirming, the full formula is (-t±sqrt(t^2-4d))/-2
1
u/DarkLord76865 Dec 06 '23
Yes it can be solved in O(1) using simple quadratic formula. I even did it with paper and calculator. There aren't really edge cases, for smaller root you take ceil, for larger you take floor, and if the root is an integer you add or subtract one for smaller and larger root respectively. To count how many integers are between you then simply bigger minus smaller +1.
3
u/clbrri Dec 06 '23 edited Dec 06 '23
I derived a closed form math solution for Commodore 64 (no floating point or sqrt() functionality) that ran effectively instantaneously. Video at twitch.tv/clbi
Summarizing the code, I essentially calculated
See the video on an integer-based implementations of the sqrt approximations. (I think the
while()loops above might be replaced with just a singleif(), since the while loop will only run at most one iteration, but didn't yet test rigorously)1
u/Alternative-Case-230 Dec 06 '23
I've just used binary search and there is no edge cases using it. It takes ~370nanoseconds to complete part 2.
[LANGUAGE: Rust]
1
u/AutoModerator Dec 06 '23
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Alternative-Case-230 Dec 06 '23
Rust: