74
SOLUTION MEGATHREAD-🎄- 2022 Day 11 Solutions -🎄-(self.adventofcode)
submitted 3 years, 1 month ago* (edited 17 minutes after) by daggerdragon to /r/adventofcode (134.8k)
WIKI NEWS
- The FAQ section of the wiki on [Code Formatting](https://www.reddit.com/r/adventofco...
since 3 years, 1 month ago
10 of 10
Tip Reveddit Real-Time can notify you when your content is removed.
your account history
Tip Check if your account has any removed comments.
view my removed comments you are viewing a single comment's thread.
view all comments


Hi Svetlin
I manage to get part2 in less than 4ms in Go without any particular trick (but without parsing). I am sure you can do better with Rust 😁
https://github.com/pemoreau/advent-of-code/blob/main/go/2022/11/day11.go
Hi
Did you notice that the input is hard coded since I did not implemented a general parser.
To get more precise timing I use the following command to measure the execution time:
go test -bench .
In fact the purpose of my message was not to compare go and rust, but to indicate you that your implementation may be not optimized enough. If my version is as fast as a Rust implementation this may come from the use of high order functions to describe monkeys, or the fact that the arrays are never deallocated or resized. This may give you some ideas
For instance when you use the drain method line 142, does this keep the underlying array or does the upcoming push will reallocate memory? I am not expert enough in rust to answer this question
Cheers
I did something really similar but kept everything on the stack (using fixed arrays). My runtime is circa 8ms. Code here if you're interested
Yikes! Will do. Appreciate the thorough work checking my code. I really didn't expect that level of service
Now that I'm back at a computer, I've given it a go and you're spot on: your part 2 runs in 5 ms on my machine (Intel i7-9750H 2.60 GHz).
And there was me thinking I was writing efficient code :( Really I've just paid for more processing power.
You did something similar to me to make the borrow-checker happy, but I found this can be achieved by 1. Using indices to access the monkeys and 2. Accessing the current monkey within its inner item loop. https://github.com/shrugalic/advent_of_code_2022/commit/f3560c8a4072e36b8d880aeaf55cc3fda144126c#diff-822fc9256accc2cb36ea596f0bc6bc017dc3cab0e32f89ca7ee7da3e2c5e8cdaR105
I also ran into the multiple borrow issue, so I ended up just using indices instead of iterating over the monkeys themselves, which kept the borrows short. (I assume the compiler can still optimise that, and doesn't keep on loading the data each time?)