The 100 Doors Problem, on a very small computer

The 100 Doors problem running on an (emulated) unexpanded ZX81

I learned about this simple computer problem from Michael Doornbos: Just for fun, the 100 door problem on several different systems

Yeah, it’s pretty neat to be able to do that on a Commodore VIC-20 with 5K of RAM. But how about a ZX81 with only 1K? With screen memory that moves around depending on how much stuff you have on the screen? No problem:

ZX81 screendump showing program listing (program is listed at text elsewhere)
that’s it: that’s the whole program

The tricky part is printing just enough to the screen that you have enough memory to store the array and still have enough memory for you program. I did that by printing four lines of “🮐” characters (CHR$ 136 on the ZX81, U+1FB90) and moving the cursor down just far enough that later output wouldn’t zap my data. The screen address (given by the D_FILE pointer at 16396) is used as an array of 100 characters.

The ZX81’s (non-ASCII) character set has a nice quirk that Space is CHR$ 0, and inverse video Space (“█”) is at CHR$ 128. So you can use NOT to toggle the value.

Here’s the program listing, with Unicode characters:

   10 REM 100DOORS1K SCRUSS 2025
  20 FOR I=1 TO 128
  30 PRINT "🮐";
  40 NEXT I
  50 PRINT AT 3,0;"🮐"
  60 LET D=PEEK 16396+PEEK 16397*256
  70 FOR J=1 TO 100
  80 POKE D+J,0
  90 NEXT J
 100 FOR I=1 TO 100
 110 FOR J=I TO 100 STEP I
 120 POKE D+J,128*NOT PEEK (D+J)
 130 NEXT J
 140 NEXT I
 150 FOR I=1 TO 100
 160 IF PEEK (D+I) THEN PRINT I,
 170 NEXT I

The ZX81 program image plus the listing in zmakebas format is included here:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *