From Code to Play: Bringing Marupeke to Life

How Rust, multi-threading and JSON bring interactive marupeke puzzles closer to reality.

Every side project starts with a spark of curiosity. For me, it was the monotony of day-to-day work and a craving to learn something new—programming in Rust in this case. You can only focus on OKRs and KPIs for so long before becoming creatively bogged down.

The subconscious cravings for newness become impossible to ignore and begin manifesting in the desire to work on side-projects that may or may not prove economically viable in the future but doing the work itself is enough of a reward because it fills the innate desire of just executing without getting impeded by all the mundanities of the corporate environment.

These last couple of months I have been writing code in Rust with the help of Copilot, ChatGPT and Claude to generate playable Marupeke puzzles. The journey has been full of trial and error, with moments where I wondered if the simplest solution would ever emerge. But sporadic hacking on the project finally paid off. I got to a point where the multi-threaded code now generates puzzles with solutions and exports them to JSON that will allow me to build interactive playable puzzles online without adding a backend server.

Generating a single 5x5 grid Marupeke puzzle
The preceding puzzle and solution encoded to JSON

Before embarking on this project I knew nothing about Rust and now I know a little. Using Copilot and ChatGPT as a pair programmer accelerated the learning curve as you can just ask it to explain parts of the code you don’t understand. It’s a game changer for learning new programming languages. These tools act as on-demand mentors for understanding Rust’s quirks, you can quickly ideate and ask it to explain concepts when hitting roadblocks.

In the last post the algorithm was able to generate playable puzzles and I hinted at wanting to experiment with concurrency to try to generate puzzles on more than a single cpu core to create them faster.

Multi-threading

The M1 Pro in my MacBook has a 10-core CPU. Generating puzzles takes a long time because the algorithm essentially builds a complete puzzle grid and tries to iteratively remove cells while checking that the puzzle remains solvable. This takes a long time since it’s a random process so why not try to run that process on every available CPU core?

I found out about num_cpus and channels in Rust and was able to hack a message passing system together where 9 worker threads each receive a task to generate a puzzle in a round-robin fashion. Each thread sends the resulting puzzle down another channel back to the main thread which collects them for subsequent transformation to JSON to prepare them for interactivity online.

Worker threads receiving tasks

I plan to add this JSON to marupeke.today to enable playable puzzles that can be verified on the client-side without needing a backend. Since the JSON contains the puzzle and a hash of the solution we’ll be able to quickly verify the correctness of a completed puzzle by calculating the hash of a string representation of the grid and comparing it to the pre-calculated solution hash. This will keep the project small, self-contained and avoids any infrastructure headaches.

Since I want to continue learning Rust and explore other ideas I don’t want to waste time creating a complicated puzzle backend. Generating hundreds of puzzles, transforming them to JSON and saving on S3 is enough to have a self-contained site where people can play these puzzles without me needing to manage my own backend server and this is way cheaper than even hosting a bare-bones VPS.

I’ve also been working on this too long and want to implement other ideas such as a site that lists simplified insights for Brazilian REITs by auto-summarizing the monthly reports. Knowing what is going on with these REITs is something of a personal issue since I am invested in several funds and tracking and reading the monthly reports can be time consuming.

That’s it for this time, thanks for reading.

Subscribe to popado

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe