Day 5: If You Give A Seed A Fertilizer

AdventuRust - AoC 2023

This is Day 5 of Advent of Code 2023! If you would like to solve the problems before looking at the solution, you can find them here.

In part 1 of Day 5, we're working with a puzzle that I feel is meant to annoy the heck out of me. If I were coding in Python, I'm sure I could have come up with a solution that was less verbose but we're using Rust, so I decided to keep it pretty straightforward. Even so, I feel like parts of this solution should have been more functional, but we've got what we've got. We're playing a game of telephone combined with a weird range mapping system. I learn about the match functionality in Rust, it's equivalent of a switch-case logic in other languages - something that was only recently introduced in Python 3.10. The game of telephone leads us from seeds to soil to fertilizer to water to light to temperature to humidity to finally the location. We need to find the smallest location that any of the seeds map to. Once the parsing and chaining are done, we have the answer to the locations, and the minimum element in the location vector is the answer to part 1. Click here to jump to the part 2 discussion.

For part 2 of Day 5, we don't really need to change a lot more, but the search space increases dramatically. In my case, my search space was in the billions! I had to learn about multithreading in Rust using Rayon and atomic variables. Atomic variables are safe to share between threads and hence can be used for multithreaded processes. Even with multithreading on a 16 CPU machine, the execution took a little under twenty minutes, phew! Another familiar friend in this exercise was the Rust equivalent of tqdm, a progress bar for loops that helps you know where you stand without printing debug statements every so often. The rest of the code was essentially the same as part 1.

That's all folks! If you made it this far, enjoy some AI art generated by u/dimpopo using the prompt for this puzzle.

Cheers,
Devang