49
\$\begingroup\$

Challenge:

The concept is simple enough: write a full program to output its own code golf score!

Output should only be the byte count of your program and a trailing bytes.

BUT WAIT..... there is one restriction:

  • Your source code can not include any of the digits from your byte count
  • So if your score is 186 bytes, your program can not contain the characters 1 , 6 , or 8

Example Output:

315 bytes
27 Bytes
49 BYTES

Additional Rules:

  • Unnecessary characters, spaces, and newlines are forbidden in the source code, however trailing spaces and newlines are perfectly acceptable in output
  • There should be a single space between the number and bytes in the output
  • Letters are case insensitive
  • No self inspection or reading the source code
  • standard loopholes are disallowed

  • this is , so

Shortest code in bytes wins!

\$\endgroup\$
  • 6
    \$\begingroup\$ Does this need the quine tag, or may the code self-inspect? \$\endgroup\$ – Adám Mar 29 '18 at 15:59
  • 3
    \$\begingroup\$ @Dat, given the formatting of the word bytes I suspect the intention is that the count should include the bytes it takes to print the text: bytes \$\endgroup\$ – Jeff Schaller Mar 29 '18 at 16:16
  • 2
    \$\begingroup\$ Are leading spaces in output acceptable? \$\endgroup\$ – rafa11111 Mar 29 '18 at 17:43
  • 3
    \$\begingroup\$ If my code is 1 byte long, should I output 1 bytes or 1 byte? (keep in mind there are already 41 answers, although I don't think any are affected) \$\endgroup\$ – Erik the Outgolfer Mar 29 '18 at 18:07
  • 1
    \$\begingroup\$ @JoKing well I didn’t address casing in the rules, but if somehow using capital letters makes your score lower, then by all means I’d love to see what you came up with. I say go for it! \$\endgroup\$ – X1M4L Mar 29 '18 at 22:50

133 Answers 133

36
\$\begingroup\$

JavaScript, 19 bytes

Octal was invented to serve two purposes:

  • setting file permissions in Linux
  • answering this challenge

alert(023+' bytes')

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Does this count as a full program? \$\endgroup\$ – ericw31415 Mar 29 '18 at 21:06
  • 1
    \$\begingroup\$ @ericw31415 This is a browser program, as defined in this meta answer. \$\endgroup\$ – Arnauld Mar 29 '18 at 21:13
  • 1
    \$\begingroup\$ +1 because finally clarifying me why octal is not completely useless! \$\endgroup\$ – sergiol Apr 15 '18 at 11:31
32
\$\begingroup\$

Perl 5, 16 bytes

There are many other ways to get 16 but this one is mine

say afpbytes^PPP

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ How does this work? \$\endgroup\$ – caird coinheringaahing Mar 30 '18 at 13:00
  • 9
    \$\begingroup\$ @cairdcoinheringaahing In perl you can leave out the quotes on strings if they looks like a literal and don't conflict with a keyword (and is very much deprecated but we don't care in golf), Binary operators & | ^ and ~ can also be applied to strings and work on them like on a list of ASCII values (if and only if both sides are strings). For | and ^ it works as if the shorte string is extended with \0. After that is a simple exercise to find combinations of letters that xor to the desired output, \$\endgroup\$ – Ton Hospel Mar 30 '18 at 14:12
21
\$\begingroup\$

7, 23 characters, 9 bytes

54340045141332401057403

Try it online!

This is a fairly hard challenge in a language that consists entirely of digits, but I managed it…

This is just 9 bytes in 7's encoding. (Strictly speaking, it's 8⅜ bytes (23 × ⅜ − ¼ because the final two trailing 1 bits can be omitted), but for the first time, PPCG's requirement to round up to a whole number of bytes is actually an advantage because it means that the extra trailing 1 bits are necessary and thus not banned by the question.) A reversible hex dump:

00000000: b1c0 2530 b6a0 22f8 1f                   ..%0.."..

The main challenge of writing this program in 7 was golfing it to under 10 bytes (as writing 7 without using 0 or 1 is pretty hard.) This uses the same structure as the standard "Hello world" program:

54340045141332401057403
5434004514133240105      commands 0-5 append literals to data space
                   7     start a new section of data space
                    403  another literal appended to data space
                         {implicit: eval the last section as commands}
                    4    swap 1st and 2nd sections with an empty section between
                     6   reconstruct the commands that would create the 1st section
                      3  output (+ some other effects we don't care about)

In other words, we start by creating two sections of the data space; we have two literals, each of which pushes a sequence of commands there. The second section (they're pushed stack-style so first push = last pop) is a fairly arbitrary sequence of commands but is pushed using the command sequence 5434004514133240105 (thus producing the data sequence 5434664574733246765; when discussing 7 in text, I normally use normal font for a command that pushes a literal, and bold for the corresponding resulting literal). The first section is pushed using the command sequence 403, producing 463. Then the first section is copied back to the program (an implicit behaviour of 7).

The 463 is now composed of (bold) commands that do something immediately, rather than (non-bold) commands that just push literals. 4 rearranges the sections to get our "string literal" into the first section. Then 0 does the operation that 7 is most known for: taking a section of data space, and reconstructing the command sequence that's most likely to have created it. In the case where the original command sequence was all 0-5, this is 100% accurate (unsurprisingly, as those commands purely push data and thus leave obvious evidence of what they did), and so we get our original sequence 5434004514133240105 back. Finally, 3 prints it.

So the remaining thing to look at here is the encoding of the string. This has its own domain-specific language:

5434004514133240105
5                    change encoding: 6 bits per character
 43                  select character set: digits and common symbols
   40                '9'
     04              space
       51            select character set: uppercase letters
         4133240105  'B' 'Y' 'T' 'E' 'S'

(There's no "select character set: lowercase letters" in the "digits and common symbols" character set – you have to go via a different character set first – so I needed to use uppercase to golf this short enough to fit underneath the effective 10-byte limit.)

\$\endgroup\$
13
\$\begingroup\$

Canvas, 8 bytes

8 bytes+

Try it here!

A more interesting 20 byte solution:

bytecount.innerText#

Try it here!

\$\endgroup\$
  • 3
    \$\begingroup\$ OK, that is pretty clever. \$\endgroup\$ – Adám Mar 29 '18 at 15:58
11
\$\begingroup\$

brainfuck, 53 BYTES

+[[<+>->->++>----<<<]>-]>-.--.<<<+.<.<.-----.>+++.<-.

Try it online!

Another submission with capitalised bytes. Found with the help of the excellent BF-crunch helper by primo.

\$\endgroup\$
10
\$\begingroup\$

PHP, 14 bytes

<?=7*2?> bytes
\$\endgroup\$
  • 7
    \$\begingroup\$ How about putting the bytes part outside the ?>? Try it online! \$\endgroup\$ – Jo King Mar 30 '18 at 0:02
  • \$\begingroup\$ @JoKing great 3 less bytes :) \$\endgroup\$ – Cyclonecode Mar 30 '18 at 1:37
  • 1
    \$\begingroup\$ Just fyi, it's probably best that you split up your answer into multiple answers if you cover more than one language. \$\endgroup\$ – Conor O'Brien Mar 30 '18 at 16:56
  • \$\begingroup\$ @ConorO'Brien - Oki will do \$\endgroup\$ – Cyclonecode Mar 30 '18 at 17:10
7
\$\begingroup\$

PHP older than 7.2, 13 bytes

Assumes default settings (E_NOTICE warnings disabled) and PHP version older than 7.2.

Needs to be decoded with xxd -r

00000000: 3c3f 3d7e cecc df9d 868b 9a8c 3b         <?=~........;

Or if you prefer Base64

PD89fs7M352Gi5qMOw==

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Stax, 5 bytes

╗↑╞ô╝

Run and debug it

Unpacked, it looks like this. Packed source mode makes this kind of trivial and uninteresting.

5p     print 5 with no line break
`5l@   print the compressed string " bytes"

If you don't want to use packed source mode, 6^p`5l@ works for 7 bytes.

\$\endgroup\$
  • 1
    \$\begingroup\$ I've downvoted this answer, because I think that using packed mode, while a clever way of saving bytes, feels like cheating (which it isn't) at worst, and uncreative at best. Rather than go on a downvote spree of all Stax restricted-source answers, I'll leave this downvote to cover all such similar answers. \$\endgroup\$ – caird coinheringaahing Apr 11 '18 at 19:34
  • 1
    \$\begingroup\$ I certainly agree that it's uncreative. But I think the fault lies with the concept of restricted-source, not the language. This is actually the most direct way of producing "5 bytes" using Stax. \$\endgroup\$ – recursive Apr 11 '18 at 19:36
  • \$\begingroup\$ I do agree that there is a problem with the challenge (hence why I've downvoted it), but I still dislike this answer. Please don't delete it though (just preemptive caution), there's nothing wrong with it. \$\endgroup\$ – caird coinheringaahing Apr 11 '18 at 19:39
5
\$\begingroup\$

Python 3, 25 24 18 bytes

print(6*3,'bytes')

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ But "4" is in "24". Maybe "33-9"? \$\endgroup\$ – recursive Mar 29 '18 at 15:44
  • \$\begingroup\$ @recursive 3*8 should be fine, though \$\endgroup\$ – Kevin Cruijssen Mar 29 '18 at 15:45
  • \$\begingroup\$ @recursive whoops, fixed it :P \$\endgroup\$ – NieDzejkob Mar 29 '18 at 15:47
5
\$\begingroup\$

R, 16 bytes

cat(4*4,"bytes")

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Isn't it 17 because of your missing space? \$\endgroup\$ – PmanAce Mar 30 '18 at 3:05
  • \$\begingroup\$ @PmanAce "Output should only be the byte count of your program and a trailing bytes". The program is 16 bytes. \$\endgroup\$ – Giuseppe Mar 30 '18 at 3:13
  • \$\begingroup\$ cat by default adds a space between its inputs. \$\endgroup\$ – Giuseppe Mar 30 '18 at 3:13
5
\$\begingroup\$

05AB1E, 6 bytes

₂¤“ÿ¡Ï

Try it online!


₂      # [26]        | Pushes 26 (many other alternatives).
 ¤     # [2, 6]      | Split out tail.
  “ÿ¡Ï # ['6 bytes'] | ÿ = "Interpolate top of stack.", ¡Ï = "bytes" (compressed)

Alternatives for ₂¤ include: 5>, ₁¤,

\$\endgroup\$
4
\$\begingroup\$

dc, 14 11 bytes

[ bytes]Bnn

Try it online!

-3 bytes by @Cows quack

\$\endgroup\$
  • \$\begingroup\$ [ bytes]Bnn shaves some bytes \$\endgroup\$ – Cows quack Mar 29 '18 at 15:52
  • \$\begingroup\$ @Cowsquack thanks! \$\endgroup\$ – NieDzejkob Mar 29 '18 at 15:55
  • \$\begingroup\$ @cowsquack duckies go MOO, I'd assume :P? \$\endgroup\$ – Magic Octopus Urn Mar 30 '18 at 22:05
  • \$\begingroup\$ @MagicOctopusUrn Ducks, what are ducks? :P \$\endgroup\$ – Cows quack Mar 31 '18 at 9:03
4
\$\begingroup\$

MATL, 13 bytes

'57$f}xiw'4-c

Try it online!

This pushes the string '57$f}xiw' and subtracts 4, to get 13 bytes. This is converted to characters using c.

\$\endgroup\$
  • \$\begingroup\$ 6EV' bytes'h is 12 (I was just about to post it) \$\endgroup\$ – Giuseppe Mar 29 '18 at 16:11
  • 1
    \$\begingroup\$ although it's the same boring approach :P \$\endgroup\$ – Giuseppe Mar 29 '18 at 16:11
4
\$\begingroup\$

Octave, 22 bytes

disp([50 50 ' bytes'])

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Bash, 21 bytes

"Read your own source code" is clearly not the winner here, but no one else submitted one, so what the heck.

echo `wc -c<$0` bytes

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Petit Computer BASIC, 11 bytes

?9+2,"bytes

Using a comma in a print statement will move the cursor over to the next multiple of the current tab size. In SmileBASIC, that is 4 by default, so this would output 11 bytes (2 spaces) but PTC BASIC used 3 instead, so the output has the correct spacing.

\$\endgroup\$
4
\$\begingroup\$

Retina, 15 bytes


 bytes
*>w`..+

This program counts the number of substrings with at least two characters in the string " bytes", and then prints this count and the string itself.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog Unicode), 12 bytes

Full program which implicitly prints to STDOUT.

'bytes',⍨3×4

Try it online!

,⍨ is appends the string to the result of the multiplication. APL's default display of mixed-type data puts a space between a number and subsequent text.

\$\endgroup\$
  • \$\begingroup\$ I may be wrong, but don't you have a 2 in your byte count and program? 3*4 should be fine though \$\endgroup\$ – fyrepenguin Mar 29 '18 at 20:04
  • \$\begingroup\$ @fyrepenguin Heh, right you are, of course. How silly of me. \$\endgroup\$ – Adám Mar 29 '18 at 20:39
4
\$\begingroup\$

Brain-Flak, 100 BYTES

((((((<>))))))(((()[[]]((((()()((((([][]){}){}())[()]))[[][]])()){})()[][]))[()()[]])[]){({}<>)<>}<>

Try it online!

Outputs the bytes part in all caps. Someone familiar with Brain-Flak might be really confused with all the ((<>)) at the beginning, but rest assured, they're only there to abuse the stack height nilad ([]) and none of the 0s they produce are printed.

\$\endgroup\$
4
\$\begingroup\$

PHP + HTML, 16 bytes

&#49;&#54; bytes

Yes, this is getting pretty close to loophole abuse, but I felt this needed to be posted. And the use of PHP means that this answer technically is written in a Turing-complete programming language. Of course, any other similar HTML templating language would work just as well.

Live demo (without PHP):

&#49;&#54; bytes

\$\endgroup\$
  • \$\begingroup\$ Beat me to it! And the ; can usually be omitted! \$\endgroup\$ – wedstrom Apr 2 '18 at 21:53
  • \$\begingroup\$ Ahh but then it would be 14/15 bytes \$\endgroup\$ – wedstrom Apr 2 '18 at 21:57
4
\$\begingroup\$

Haskell, 26 bytes

main=putStr$"\50\54 bytes"

Try it online!


29 bytes

main=putStr$pred<$>"3:!czuft"

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Unnecessary whitespace is now forbidden, but you can use a $ instead. \$\endgroup\$ – Ørjan Johansen Mar 30 '18 at 0:57
  • \$\begingroup\$ @ØrjanJohansen Thanks for pointing out. \$\endgroup\$ – Laikoni Mar 30 '18 at 10:42
4
\$\begingroup\$

Whitespace, 112 bytes

Just because nobody's done it yet:

   			    
	
 	   
   	 	  		
   	   	 	
   	 	 	  
   	 		  	
   	    	 
   	     

  
	
   
 
	  

 


   




Try it online!

Prints " BYTES" in caps, since uppercase letters have shorter binary ASCII codes. The commands in the program, in my own made up visible notation, are:

Push 112
PrintNum
Push 0
Push 0x53
Push 0x45
Push 0x54
Push 0x59
Push 0x42
Push 0x20
Label _
PrintChr
Dup
JumpZ S
Jump _
Label S
End
\$\endgroup\$
4
\$\begingroup\$

Java 5 or 6, 44 bytes (full program)

enum A{A;{System.out.print(0x2C+" bytes");}}

No TIO-link, because Java 5/6 is old.. In Java 5/6 it was possible to have an enum with code instead of the mandatory main-method, making it shorter for full program challenges.

Also errors with java.lang.NoSuchMethodError: main\nException in thread "main" in STDERR after printing to STDOUT, which is fine according to the meta (but if the challenge would have disallowed additional errors to STDERR, System.exit(0); can be added (and the octal numbers has to be updated) to prevent the error.

Suggested by @OlivierGrégoire (and his relevant Java tip).

Java 8+, 74 bytes (full program)

interface M{static void main(String[]a){System.out.print(0112+" bytes");}}

Try it online.

Java 8+, 16 15 bytes (lambda function)

v->0xF+" bytes"

-1 byte thanks to @OliverGégoire.

Try it online.

\$\endgroup\$
  • 2
    \$\begingroup\$ Technically not a full program :) \$\endgroup\$ – O.O.Balance Mar 29 '18 at 15:54
  • \$\begingroup\$ @O.O.Balance Oops.. somehow read past that, thanks for the correction. There goes my score.. XD \$\endgroup\$ – Kevin Cruijssen Mar 29 '18 at 16:48
  • 1
    \$\begingroup\$ 37*2 contains a 7, so it is not valid. Your alternative solution is fine though. \$\endgroup\$ – O.O.Balance Mar 29 '18 at 23:01
  • 1
    \$\begingroup\$ v->0xE+"bytes" or v->0xF+" bytes". Can't test but should work anyways. Nothing says the score and "bytes" must be separated. \$\endgroup\$ – Olivier Grégoire Mar 30 '18 at 10:56
  • 1
    \$\begingroup\$ @OlivierGrégoire I know, I know. ;) When I posted the full program I was doubting to answer it as enum thinking about your tip-answer. Do you know any online compiler that still supports it? I prefer to have a link with test code for my answers.. \$\endgroup\$ – Kevin Cruijssen Mar 30 '18 at 14:36
4
\$\begingroup\$

FerNANDo, 79 bytes

3 3
1
2 2
0 1 3 3 1 2 2 3
0 1 3 3 2 1 0 2
0 1 3 0 0 1 0 1
0 3 3 1 0 0 3 1
1 1
1

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Applescript, 15 bytes

Yes, I’m going there.

9+6&”bytes”
\$\endgroup\$
  • 1
    \$\begingroup\$ But... Your source code can not include any of the digits from your byte count \$\endgroup\$ – nicael Mar 29 '18 at 19:10
  • \$\begingroup\$ @nicael Dang, you’re right. One second then... \$\endgroup\$ – DonielF Mar 29 '18 at 19:12
  • \$\begingroup\$ Correct me if I'm wrong, but isn't a multibyte character (3 bytes in Unicode if I remember correctly), so this is more than 11 bytes? \$\endgroup\$ – caird coinheringaahing Apr 3 '18 at 0:35
  • \$\begingroup\$ @cairdcoinheringaahing ...Shoot. You’re right - “ is 2 bytes, upping this to 15. \$\endgroup\$ – DonielF Apr 3 '18 at 1:05
  • \$\begingroup\$ does this not still print 11? \$\endgroup\$ – Destructible Lemon Apr 3 '18 at 3:11
3
\$\begingroup\$

Forth (gforth), 14 bytes

$e . ." bytes"

Try it online!

\$\endgroup\$
3
\$\begingroup\$

><>, 13 bytes

#o<" bytes"nd

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Octave, 23 bytes

printf('%i bytes',9+14)

Try it online!

And this:

disp(['','67$f}xiw'-4])

Try it online!

\$\endgroup\$
  • \$\begingroup\$ The first answers seems fine, but... how does the second one works? o.O \$\endgroup\$ – rafa11111 Mar 29 '18 at 18:14
  • 2
    \$\begingroup\$ @rafa11111 Subtracting 4 from (the ASCII codes of) '67$f}xiw' gives the ASCII codes of '23 bytes'. Then, concatenating with the empty string '' converts ASCII codes to chars \$\endgroup\$ – Luis Mendo Mar 29 '18 at 19:07
  • 2
    \$\begingroup\$ @Luis Mendo Pretty clever! Thanks for clarifying! \$\endgroup\$ – rafa11111 Mar 29 '18 at 19:13
3
\$\begingroup\$

Batch, 32 bytes

@@echo %COMSPEC:~-10,-010% bytes

COMSPEC is usually %WINDIR%\system32\cmd.exe so I extract the substring from 10 to 8 characters from the end. Old 16-byte self-referencing version:

@echo %~z0 bytes

Fortunately the byte count wasn't a multiple of 10.

\$\endgroup\$
  • \$\begingroup\$ @manatwork OK, I hadn't seen that, here's a new version. \$\endgroup\$ – Neil Mar 29 '18 at 21:16
3
\$\begingroup\$

JavaScript, 16 15 bytes

x=>0xf+' bytes'
\$\endgroup\$
  • \$\begingroup\$ _=>5*3+' bytes' \$\endgroup\$ – Neil Mar 29 '18 at 19:36
  • \$\begingroup\$ @Neil Both the code and the byte-count contains a 5 in that case.. \$\endgroup\$ – Kevin Cruijssen Mar 29 '18 at 20:04
  • \$\begingroup\$ @KevinCruijssen Bah, I can't believe I overlooked that. \$\endgroup\$ – Neil Mar 29 '18 at 21:17
  • \$\begingroup\$ Submissions are required to be full programs "write a full program to output its own code golf score!" \$\endgroup\$ – Cows quack Apr 15 '18 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.