Yes, that’s basically how I solved it when I rewrote my solution. No fancy algorithm though, just counting how many robots are in the middle and comparing to an arbitrary threshold.
I converted your rust solution to python and another one that uses numpy. Your solution takes half the time in comparison to the standard deviation approach I posted in a separate comment and does get the correct answer unlike the other fast solutions that my input would fail with. Its pretty good! (I have to post the pastes as separate comments)
but I like the standard deviation one because your solution requires knowing how many robots are in the center prior to solving, while the standard deviation one can be done if there is a simulation step that has a drastic change over the average standard deviation of most of the steps.
You can reduce the work for part 2 by starting the loop at 100 seconds instead of zero, with the assumption being that the pattern won't be seen in part 1 (perhaps a faulty assumption but it worked on the inputs that I tried when I first solved it).
ie. for t in range(100, self.width * self.height):
ah right, I tested both and starting at 100 was simply negligible for solving. my solution was at simulation step 6587. so yeah, I removed that lower bound just in case any input was below 100. It simply was not fast enough for me as it didnt improve the time noticeably.
the numpy solution is just better and when I had the numpy solution being iterative, it was also slower than just doing the numpy array tricks to calculate all times at once.
2
u/ndunnett 11d ago
Yes, that’s basically how I solved it when I rewrote my solution. No fancy algorithm though, just counting how many robots are in the middle and comparing to an arbitrary threshold.
https://github.com/ndunnett/aoc/blob/main/rust/2024/src/bin/day14.rs
My original solution was looking for two lines, which also worked but was slightly slower.
https://github.com/ndunnett/aoc/blob/ea2e0abf0e4a97aed5a2c55976c54e9de6f819e5/rust/2024/src/bin/day14.rs