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.(saves 2 bytes)Actually it saves 4 bytes since SB files are in the UTF-16 format.
Contest idea
Root / Site Discussion / [.]
12Me21Created:
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?
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 ?CAnd 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?CEdit:
For example: BEEP 69'MEOW counts as 6 charactersDoesn'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.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)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 ?CAnd 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?CEdit:For example: BEEP 69'MEOW counts as 6 charactersDoesn't this count as 7 characters? BEEP 69'MEOW 1234567-----
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?CBut 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 counted0: 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)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.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?CBut you win, since it wouldn't be fair for me to be in the contest because I started early.
FOR j=0TO PRGSIZE(0)-1PRGEDIT0,jFirstly, 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,YThis 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' 6Edit 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?CYou need to count the CR after a comment:
BEEP 69'MEOWIs 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 'commentwhich your program counts as around 15 characters instead of 7 (ish)