I'm trying to do the Advent of Code in Go this year.
I just started learning Go on Nov 28, 2023. After doing the first 2 days I feel like I'm starting to get the hang of Go.
Had some performance issues with my solution for the second problem.
First I added a global backSteps
variable to be used by the LocationToSeed
method. Which didn't make any noticable difference. I also removed some of the print statements which also made no effect on the speed. Replacing for i := 0; i <= 157211394; i++
with for i := 0; true; i++
for the condition in the loop that does most of the work did nothing either.
Since after creation there are no writes to the almanac struct it's safe not to use locks. This shaved 35 seconds from the time.
Total rewrite of the second problem to use ranges of numbers instead of indivual numbers made this super fast.
Added an interal library for ranges since they might be useful for future problems.
Version | Sample 1 | Sample 2 | Input 1 | Input 2 |
---|---|---|---|---|
Initial | 432.21µs | 318.257µs | 1.476169ms | 1m25.011957731s |
No read locks | 471.923µs | 507.104µs | 1.281487ms | 50.185503074s |
Ranges | 804.622µs | 251.19µs | 808.556µs | 658.571µs |
Made a visualization of filling the outside spaces and finding the inclosed spaces.