It seems to be a bit of a theme this year that there’s a correct way of solving a puzzle, and then there’s my way. Yesterday it was doing brute force pattern matching when there’s a whole library that does it and it’s even available on Lua (my programming language for this event). Today it’s doing pointer chasing when CS 101 students learn recursion their first day.

We’ll define all that.

In today’s puzzle, the part 1 challenge is, given a long number, find the largest two digit number you can make from the digits within it without changing their order. So for 12345, the largest number would be 45. For 8675309, it would be 89. The algorithm I chose: find the highest digit in the digits except for the last one, because we need two digits, and if the highest digit IS the last one, we’ll have run out. (For 56789, 89 would be the largest number).

For part 2, we needed to find the highest 12-digit number.

From my Picotron IDE

This function finds the largest n digits in the string by using the variable “start” to track the beginning of the sub string we’re scanning, while the “#s-(n-i)” defines the end of the sub string we’re scanning. The “start” pointer is chasing the other one.

This works; I don’t have a problem with the solution and I was happy with it… until I went to the social medias and other solvers were saying “yeah, easy recursion gg done in two minutes”.

What is recursion?

It’s recursion when a function calls itself. If I’d used that above, max_digit_except would have found its first digit, then called itself with a substring to find all the others, until all the necessary digits were found and it bounced back up to the top. That for loop wouldn’t have been necessary, neither would the pointer chasing. Probably save two or three lines of code.

Faster? Probably not. Since I am doing calculations tied to the frames per second of the Picotron environment, as long as each line was completed in a sixtieth of a second, no problem.

My solver, with coroutine implemented

Coroutine

I didn’t need to use Lua’s coroutine library for this, but I did anyway. I was wondering if I could have the solver run in its own thread and work in the background while I ran a video game in the foreground. After some research, it became clear the answer to that was “no”. However, they did have a way to call a function that would yield back its execution thread and save all its variables so that it could pick right up again. This is broadly analogous to Python’s “generator” functions.

In my implementation, the _update method calls the coroutine every few frames with a new line of input; it solves it, adds the values and adds some log statements to the debug log for _draw to later display. It then yields back to the update method, which exits and lets Picotron keep doing what it does.

Who is this?

I added more platforming stuff. This will get me blocked on BlueSky, but I had ChatGPT generate a sprite sheet for a cat character and I’m slowly integrating it into Picotron. Right now it jumps (and so does the safe). The plan is for all the puzzles to be slowly integrated in some way to this. Today I would be happy if I could make the cat turn and run and stop. Then the idea is you do something with the safe, etc.

I decided to do AoC in Picotron and Lua because I wanted to get some familiarity with it. Picotron cartridges, like Pico-8 before it, are easy to distribute. Writing Sword for Hire was a lot of fun, but when it came to finishing and distribution, I couldn’t figure out how to deliver a game that ran in a terminal window. For one, the terminal window I used for development was slightly (well, quite a lot) larger than the 80×24 we normally expect. Since everything in SfH was a menu choice, it would lend itself to the minimal hands-off interaction Picotron likes.

That would be a hell of a job, I’d have to start over, and Python did a whole lot of stuff I wouldn’t get to use in Lua. But I might be able to go all Scott Adams on it, and that might be worth it. I have a bunch of projects I want to work on, but it irks me that nobody but me has been able to play SfH. I’d like to change that.

Leave a Reply

Discover more from West Karana

Subscribe now to keep reading and get access to the full archive.

Continue reading