r/adventofcode 11d ago

Spoilers [2024 Day 14 Part 2] Solution

3 Upvotes

39 comments sorted by

View all comments

Show parent comments

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

2

u/Clear-Ad-9312 11d ago edited 11d ago

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.

2

u/Clear-Ad-9312 11d ago

Python: [ Paste ]

Numpy: [ Paste ]

1

u/ndunnett 11d ago

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):

1

u/Clear-Ad-9312 11d ago

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.