Previously I discussed several approaches to printing out the Christmas tree pattern for this challenge.
The method that produced the smallest Color BASIC program looked like this:
That uses 66 byte of BASIC program space (though will use more RAM for strings as it runs).
It appears there are still optimizations to make! In the comments. Stewart Orchard pointed out this one:
Not a big gain but two bytes can be saved by removing the third argument from MID$().
The two argument version of MID$() returns the remainder of the string starting from the specified position, and ASC() returns its result based on the first character of its argument.– Stewart Orchard
In Color BASIC, MID$ can accept three parameters:
If you had as string of 10 characters, and wanted to print the third character, you could do it like this:
A$="ABCDEFGHIJ" OK PRINT MID$(A$,3,1) C
Stewart pointed out that if you left off the final parameter, it returns the rest of the string:
A$="ABCDEFGHIJ" OK PRINT MID$(A$,3,1) CDEFGHIJ
Now that I look at it, it appears MID$ with two parameters is sort of like an inverted RIGHT$. MID$ would give you all the characters starting at the one you specify, and RIGHT$ gives you the number of ending characters you specify.
PRINT MID$(A$,3) CDEFGHIJ PRINT RIGHT$(A$,3) HIJ
I don’t recall using MID$ like this, but I have simulated the same behavior using RIGHT$ like:
PRINT RIGHT$(A$,LEN(A$)-3+1) CDEFGHIJ PRINT MID$(A$,3) CDEFGHIJ
I am a bit embarrassed to admit I think I even did something like this:
PRINT MID$(A$,3,LEN(A$)-3) CDEFGHIJ
…but let’s not speak of that.
And if MID$ can work like RIGHT$, it can also work like LEFT$:
PRINT LEFT$(A$,3) ABC PRINT MID$(A$,1,3) ABC
But I digress…
If MID$(A$,3,1) gives you one character starting as location 3, and MID$(A$,3) gives you all the characters starting at position 3, how does that work? Stewart explained that ASC will still work if you pass it a string since it only works on the first character of the string. It is even documented that way:
Thus, ASC(“HELLO”) produces the same result as ASC(“H”) — the ASCII value of letter H.
With that in mind, we can remove “,1” from the program and reduce it by two bytes:
Thank you, Stewart, for leaving that comment.
Until next time…