The Snowing Christmas Sweater

December 25 was the deadline for the submissions to the Vintage Computing Christmas Challenge 2023 (VC³ 2023), a nice programming competition organized by Logiker for the Christmas period (here’s the announcement video). The objective of the main challenge is the creation of a program for any computer and language (preferably vintage systems), capable of faithfully representing the figure shown in the picture below, optimizing the code as much as possible.

The expected program output
The expected program output

Having already taken part in last year’s edition, at the beginning of December I was invited by Logiker to join the new challenge, so I decided to try to code an entry in BASIC language for the Sinclair ZX Spectrum in my spare time.

I wanted the shape to be drawn as if it were created by snow flakes, (pseudo-)randomly falling on the computer screen, but to make things easy I simply started with printing the whole image. Then I arrived step by step at the final version of the program, with each step being a slight improvement of the previous.

In the first step, I just printed an oblique line of asterisks. At first, I didn’t know (or remember) that the PRINT AT statements allows negative values for line and column, with absolute values being used as the printing position, but then I realized that with this trick, I could draw 2 segments with a single FOR loop:

100 FOR x=0 TO 18
200 PRINT AT x,x-3;"*"
300 NEXT x
2 segments of asterisks printed with a single FOR loop
First step: 2 segments of asterisks

By iterating the process 3 times, I was actually able to print half of the desired picture:

90 FOR a=-15 TO -3 STEP 6
100 FOR x=0 TO 18
200 PRINT AT x,a+x;"*"
300 NEXT x
310 NEXT a
Printing half of the shape
Half of the shape

The full shape can be simply obtained by adding the same shape, but horizontally flipped, i.e. by printing at each loop another “*” specularly, at column 18 – ABS(current column) (program size: 116 bytes):

90 FOR a=-15 TO -3 STEP 6
100 FOR x=0 TO 18
200 PRINT AT x,a+x;"*"
210 PRINT AT x,18-ABS (a+x);"*"
300 NEXT x
310 NEXT a

Printing the full shape using FOR loops
Full shape

The effect is nice, but I wanted the “snow flakes” (“*”) to randomly appear on the screen.
So, I changed the program to a 1-liner endless loop in which the “points” belonging to each of the half shapes are chosen randomly at each iteration (program size: 88 bytes):

3 LET a=6*INT (RND*INT PI)-15: LET x=INT (RND*19): PRINT AT x,a+x;"*": PRINT AT x,18-ABS (a+x);"*": GO TO PI

The last GO TO statement uses a trick to reduce the code size, in fact the PI function requires just one token, whilst the “3” number literal would require 6 more bytes (See the Memory chapter on the Sinclair ZX SPectrum BASIC manual).

In the next step, I merged the two print statements, in order to randomly chose at each iteration which half of the shape the “*” being printed belongs to (program size: 83 bytes):

3 LET a=6*INT (RND*INT PI)-15: LET x=INT (RND*19): PRINT AT x,INT (RND+RND)*18-ABS (a+x);"*": GO TO PI

In the next step, I refactored the program to remove the a variable and save some more bytes (program size: 78 bytes):

3 LET x=INT (RND*19): PRINT AT x,INT (RND+RND)*18-ABS (x+6*INT (RND*INT PI)-15);"*": GO TO PI

Then, I saved some more bytes by replacing number literals with strings and using the VAL function (program size: 66 bytes):

3 LET x=INT (RND*VAL "19"): PRINT AT x,INT (RND+RND)*VAL "18"-ABS (x+VAL "6"*INT (RND*INT PI)-VAL "15");"*": GO TO PI

With a last little change I saved 3 more bytes and ended up with the following program, whose size is 63 bytes:

3 LET x=RND*VAL "18": PRINT AT x,INT (RND+RND)*VAL "18"-ABS (x+VAL "6"*INT (RND*INT PI)-VAL "15");"*": GO TO PI
The final version of the program
Final version (program size: 63 bytes)

I’m sure that with a little more time I could save some more bytes; maybe I’ll try to improve the program further as a personal challenge!

A note on code length measurement: the program size has been determined with:

PRINT PEEK 23627+256*PEEK 23628-23755

(see System Variables in the ZX Spectrum manual).

I think that with the proper colours combination (green border, red paper and white ink), the shape could be a Christmas sweater pattern, so I named my entry The Snowing Christmas Sweater.

My entry, with colours
With colours

Don’t you think so? Have a look at the following picture (powered by the wildemasche.com designer) and prove me wrong 😄

The Christmas sweater
My Christmas Sweater (powered by the wildemasche.com designer)

I also made a small video for presenting my entry:

A presentation of The Snowing Christmas Sweater

The results of the challenge will be published soon, so stay tuned for updates!

Thanks Logiker for the challenge (it’s a great fun!) and the BASIC on the ZX Spectrum Facebook group members for inspiration!

Leggi in Italiano

Leave a comment

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

4 thoughts on “The Snowing Christmas Sweater”