Regolith Reservoir Visualization
As is tradition every year, I've been doingAdvent of Code every day from the moment that they come out. It's quite common for AoC to have some amount of days to be simulation style problems. Some of my favorite were:
- Day 7: simulate a filesystem, with a user inputing commands and moving around the filesystem.
- Day 11: Monkeys steal my stuff! This one was quite interesting and a really fun one to code up.
- Day 14: Regolith Reservoir, sand pouring in the cave. This year we traverse a whole jungle and go into a cave system, and at some point we get stuck and have to find our way out. During this day we simulate grains of sand falling in the cave system above us.
I found day 14 quite interesting, specially for visualizations. So I stayed up that same day, right after I solved the problem I began working on a full ASCII visualization in Golang. I chose Go because I had just discovered a library called bubbletea, and used it to write a TUI for my distributed fuzzer. So I thought it'd be a great idea to use bubbletea again and implement a challenging visualization using it.
When I created the visualization I stayed true to the nature of the language and designed it as a concurrent system. Where the model for calculating the trajectory and location of the grain of sand is running on a seperate go routine than the view rendering the ASCII Art. They communicate through a single buffered channel, in a way I think of this as a producer-consumer system. And because rendering the ASCII Art actually is quite a bit more expensive than calculating the trajectory and updating the state of the cave, I only need it to be buffered by 1. This makes the computation lazy as the producer thread (model) is only moving to the next location of the grain once one view has been rendered.
Overall developing the visualizer was a really fun and interesting exercise to better understand bubbletea and Golang's thread synchronization model. Also it looks pretty cool: