I enjoy puzzles. Especially the constraint-satisfaction kind like they have on the qualitative section of the GRE. I have one of those page-a-day puzzle calendars that I work each morning while brushing my teeth. Here’s one from a couple weeks ago:
Insert the letters A to J, one per square, so that no two consecutive letters in alphabetical order are in squares that touch in any way, even at the corner. Three letters have been placed to get you started.
I managed to figure it out. My paper was a little messy, but I got there eventually. But I thought: this seems like a good candidate for a computer program. It’s the kind of problem that might have appeared on a homework assignment in my Problem Solving and Search class.
I like to exercise my programming chops on occasion. The last one I did was a UNIX shell script wordle solver. The things we do on a summer Saturday morning to avoid mowing the lawn.
I sat down at the computer and was about to start when I wondered how ChatGPT would do on the assignment. So, I asked it to:
write a c program that takes as input 1) the number of consecutively numbered squares, 2) pairs of squares that are adjacent to each other, and 3) letters assigned to certain squares. The program then finds a configuration of letters assigned to all of the squares such that no two adjacent squares have consecutive numbers.
Five seconds later I had a couple of pages of code that recursively assigns letters to unassigned squares, makes sure that adjacent squares don’t contain consecutive letters, and backtracking when a violation occurs. Compiled it. Ran it. It worked.
The problem was that I didn’t ask the question completely enough. Looking at the code, it was clear that the program would cycle through all of the letters in the alphabet until a solution was found (or report that there was no solution after going through all twenty-six). That wasn’t what I wanted, but I had omitted a constraint: the letters used had to start with A and go through the letter corresponding to the number of boxes. Five boxes: A through E. Thirteen boxes: A through M. Having that first program as context, I continued with:
modify it so that the number of distinct letters used equals the number of squares
Now, instead of looping over all twenty-six letters it only looped over the first n letters, where n was the number of boxes. Compiled it. Ran it. It worked. By the way, when it finished it asked if I wanted to find all possible solutions, not just the first one found. No, just the first is fine.
But running the program was a pain. Typing in all of the problem parameters, the adjacency matrix, and the initial configuration. It’s enough to give someone buttonitis. I’d much prefer to type everything once into a file, and then just read that file when the program is executed.
At the same time, I thought of a similar puzzle from last year where the letters in adjacent squares couldn’t be closer than two letters apart. So, D could not be adjacent to B, C, E, or F. Let’s add that capability, too.
change it so that you can read the configuration from a file and also that you can specify the number of letters apart adjacent squares are prohibited from being
Admittedly, I was getting a little lazy with the grammar. Compiled it. Ran it. It worked.
As a data person, and especially as a Data Governance person, looking at the parameter file made me cringe. One more enhancement: add comments.
change the program so that you can put comments in the input file after a pound sign
Compiled it. Ran it. It worked.
I could have added a few more features: storing the parameters in the configuration file as key-value pairs, or illustrating the results graphically, or allowing parameters to be changed after they are entered, but that was enough.
Before I finished with the program, though, I had one more request:
write that same program in Applesoft basic
That was the first programming language I learned. Apple ][+ computer. 1980. ChatGPT told me was that it couldn’t use structures, recursion, or functions in the program, but there were workarounds. Then it generated the code. Loaded the code into an Apple ][+ emulator. Ran it. Oops.
?SYNTAX ERROR IN 8060
I guess you still have to know something about writing code. Or maybe not!! Maybe ChatGPT could debug it itself. Let’s try”
I got a syntax error in 8060 in the applesoft basic program you just wrote. Why was that?
It responded with a very verbose and authoritative-sounding explanation, ultimately concluding that the problem was caused by combining multiple statements on a single line. But I knew that wasn’t the problem. (In fact, a magazine at the time used to run a contest each month to see who could get the most functionality out of a one-line Applesoft BASIC program.)
It turns out that ChatGPT forgot that in Applesoft BASIC, if statements do not support an else clause. I reminded ChatGPT of that fact.
Applesoft Basic does not have an else clause in if statements. Please rewrite the program with that fix.
It seemed appreciative, replying “You’re absolutely right — and a great catch.” Then it very confidently explained that Applesoft BASIC doesn’t support else clauses in if statements and showed the workarounds. Generated the code again. Loaded it into the emulator again. Ran it. It worked.
I talked about something similar a couple of months ago with respect to logical data models. There’s no need to start from scratch, but you do have to know what you’re doing. It’s not always going to work the first time, and debugging is becoming an increasingly important skill.
I have always been of the “Hello world” school of application development, with the initial version of all of my programs printing “Hello world.” I don’t have to start there any longer. I can start by asking for what I want.
It’s like agile except that the application is actually being generated as the specification is articulated. Try it with website development. A fully functional, e-commerce website that used to take weeks to deliver can now be deployed in an hour. Less if you already have images and verbiage lined up.
Now, I guess I have to go out and mow the lawn.