LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Cont​est idea

Root / Site Discussion / [.]

12Me21Created:
(saves 2 bytes)
Actually it saves 4 bytes since SB files are in the UTF-16 format.

(saves 2 bytes)
Actually it saves 4 bytes since SB files are in the UTF-16 format.
That's true. It really should use UTF-8, since 99.999999999% of all code is ASCII anyway. But unicode just sucks in general (regardless of the encoding) because for each character, there are like 20 others that are identical. And there are only a few fonts with 100% unicode support.

SmileBASIC tells you the size (in bytes) of a file. Why not just use that, since it would be the absolute easiest?

SmileBASIC tells you the size (in bytes) of a file. Why not just use that, since it would be the absolute easiest?
That requires saving the file, though. We also could use PRGSIZE(0,1) But I want comments to not be included in the size, to encourage people to use them so their code makes more sense.

I want ... to encourage people ... so their code makes more sense.
And, you're in favour of code golf?

Isn't the point of code golf to make the code as small and obscure as possible? Allowing comments is the antithesis of this. If you want to encourage good development, instead challenge users to come up with the most clever or efficient solution to a problem instead of just the shortest.

Can't participants go over 2048 bytes, then use any compression methods they know to get the file size under 2048 in a seperate copy? This eliminates the need to manually count every byte. Additionally, the programmer can distribute the documented and easy to read version along with the programming golf version if they chose to.

Here's my attempt. Untested since I don't have SmileBASIC, and there might be ways to improve it. This is a pretty naive approach, I'm sure someone can do better, but I can't think of any way. Also, I can't experiment with different small tweaks until SB releases in Europe, so there's probably a few bytes to save in this code.
C=PRGSIZE(0,1)' Set C to number of characters (including comments)
FOR L=0TO PRGSIZE(0)-1
PRGEDIT 0,L
A$=PRGGET$()
FOR I=0TO LEN(A$)-1
IF ASC(A$[I])==34THEN B=!B' Keep track of whether we're in a string or not. This assumes that there are no syntax errors, and all opened strings are also closed on the same line.
IF!B&&A$[I]=="'"THEN C=C+I-LEN(A$)BREAK' Might need to add : before BREAK. This line finds comments. If one is found, subtract the rest of the line and move on to the next line.
NEXT
NEXT
?C
And here it is again, with some assumption-based corner cuts. Tell me if it works:
C=PRGSIZE(0,1)FOR L=0TO PRGSIZE(0)-1PRGEDIT 0,L
A$=PRGGET$()
FOR I=0TO LEN(A$)-1IF ASC(A$[I])==34THEN B=!B' Keep track of whether we're in a string or not. This assumes that there are no syntax errors, and all opened strings are also closed on the same line.
IF!B&&A$[I]=="'"THEN C=C+I-LEN(A$)BREAK' Might need to add : before BREAK. This line finds comments. If one is found, subtract the rest of the line and move on to the next line.
NEXT
NEXT?C
Edit:
For example: BEEP 69'MEOW counts as 6 characters
Doesn't this count as 7 characters? BEEP 69'MEOW 1234567-----

Here's my attempt. Untested since I don't have SmileBASIC, and there might be ways to improve it. This is a pretty naive approach, I'm sure someone can do better, but I can't think of any way. Also, I can't experiment with different small tweaks until SB releases in Europe, so there's probably a few bytes to save in this code.
C=PRGSIZE(0,1)' Set C to number of characters (including comments)
FOR L=0TO PRGSIZE(0)-1
PRGEDIT 0,L
A$=PRGGET$()
FOR I=0TO LEN(A$)-1
IF ASC(A$[I])==34THEN B=!B' Keep track of whether we're in a string or not. This assumes that there are no syntax errors, and all opened strings are also closed on the same line.
IF!B&&A$[I]=="'"THEN C=C+I-LEN(A$)BREAK' Might need to add : before BREAK. This line finds comments. If one is found, subtract the rest of the line and move on to the next line.
NEXT
NEXT
?C
And here it is again, with some assumption-based corner cuts. Tell me if it works:
C=PRGSIZE(0,1)FOR L=0TO PRGSIZE(0)-1PRGEDIT 0,L
A$=PRGGET$()
FOR I=0TO LEN(A$)-1IF ASC(A$[I])==34THEN B=!B' Keep track of whether we're in a string or not. This assumes that there are no syntax errors, and all opened strings are also closed on the same line.
IF!B&&A$[I]=="'"THEN C=C+I-LEN(A$)BREAK' Might need to add : before BREAK. This line finds comments. If one is found, subtract the rest of the line and move on to the next line.
NEXT
NEXT?C
Edit:
For example: BEEP 69'MEOW counts as 6 characters
Doesn't this count as 7 characters? BEEP 69'MEOW 1234567-----
Yeah... I guess I can't count lol Anyway, your program works... Except there are 3 bugs: 0: program lines start at 1 not 0 (which is weird, because everything else in SB starts at 0...) 1: string flag is not reset after a CR 2: the CR at the end of a comment is not counted Anyway, your program is 157 characters, which is just a TINY bit bigger than mine (149 characters)
FOR j=0TO PRGSIZE(0)-1PRGEDIT0,j
L$=PRGGET$()S=0FOR i=0TO-1+LEN(L$)S=L$[i]==CHR$(34)XOR S
IF S THEN@C
IF"'"==L$[i]THEN i=i+1BREAK@C
NEXT
C=C+1
NEXT?C
But you win, since it wouldn't be fair for me to be in the contest because I started early.

your program works... Except there are 3 bugs: 0: program lines start at 1 not 0 (which is weird, because everything else in SB starts at 0...) 1: string flag is not reset after a CR 2: the CR at the end of a comment is not counted
0: Huh, who would have thought. Simple fix, and saves me 2 characters (the -1) 1: it doesn't need to be, as my comment explains, I assume that there are no syntax errors. A string must be closed on the same line that it's opened. Could you give me an example where this doesn't happen? 2: I thought the PRGSIZE(0,1) (character count of a program) included this character. Are you sure about this?
Anyway, your program is 157 characters, which is just a TINY bit bigger than mine (149 characters)
FOR j=0TO PRGSIZE(0)-1PRGEDIT0,j
L$=PRGGET$()S=0FOR i=0TO-1+LEN(L$)S=L$[i]==CHR$(34)XOR S
IF S THEN@C
IF"'"==L$[i]THEN i=i+1BREAK@C
NEXT
C=C+1
NEXT?C
But you win, since it wouldn't be fair for me to be in the contest because I started early.
First of all, I think you should count your own entry since the fact that you're "hosting" the contest doesn't actually give you any advantage. There's plenty of time to work on this challenge regardless of having started early. Second, I don't really understand how your program works. Can you explain it? Nevermind, got it :) I see some possible syntax errors also, which is probably my mistake because I thought you tested it already, still I find it really strange that these work:
FOR j=0TO PRGSIZE(0)-1PRGEDIT0,j
Firstly, here you also start counting from line 0 instead of line 1. See bug #0 you told me my code has ;) Second, this part bothers me: PRGEDIT0,j If I'm not mistaken, PRGEDIT0 would be interpreted as a variable name (rather than a command PRGEDIT and a parameter 0). This would be equivalent to having a line like this:
X,Y
This is probably a syntax error. Besides this, good job! Edit: adopting your method for flipping a boolean, plus another small tweak, I've brought mine down to 150 or 151, depending on whether the last line has an end-of-line character.
C=PRGSIZE(0,1)FOR L=1TO PRGSIZE(0)PRGEDIT 0,L' 45
A$=PRGGET$()FOR I=0TO LEN(A$)-1B=ASC(A$[I])==34XOR B' 52 Keep track of whether we're in a string or not. This assumes that there are no syntax errors, and all opened strings are also closed on the same line.
IF!B&&A$[I]=="'"THEN C=C+I-LEN(A$)BREAK' 39 Might need to add : before BREAK. This line finds comments. If one is found, subtract the rest of the line and move on to the next line.
NEXT' 4
NEXT?C' 6
Edit 2: there's tons of room for reducing the size or your code. I could tell you, but I think you can find it yourself ;)

Oops, I made a few mistakes when typing. The correct code is:
FOR j=1 to PRGSIZE(0)PRGEDIT 0,j
L$=PRGGET$()S=0FOR i=0TO-1+LEN(L$)S=L$[i]=CHR$(34)XOR S
IF S THEN@C
IF"'"==L$[i]THEN i=i+1BREAK@C
NEXT
C=C+i
NEXT?C
You need to count the CR after a comment:
BEEP 69'MEOW
Is actually 8 characters if it is a line in a program, since not counting the CR would allow things like:
BEEP'
instead of
BEEP
(saves 1 character)
Also, resetting the string flag is nessesary because of things like
?"LOL
'comment
which your program counts as around 15 characters instead of 7 (ish)

Oh, good point about unclosed strings, but your program doesn't reset it either! And you also still start from 0 instead of 1.

Oh, good point about unclosed strings, but your program doesn't reset it either! And you also still start from 0 instead of 1.
Wow, I made the same mistake again! fixed. My program does reset the string flag: L$=PRGGET$()S=0FOR i=0TO-1+LEN(L$)S=L$[i]=CHR$(34)XOR S

My program does reset the string flag: L$=PRGGET$()S=0FOR i=0TO-1+LEN(L$)S=L$[i]=CHR$(34)XOR S
I see, you're right!