Code Golf: Code Golf: Difference between revisions

From Rosetta Code
Content added Content deleted
m (ALGOL 68 →‎Without Quotes: change section name)
Line 13: Line 13:
<pre>Code Golf</pre>
<pre>Code Golf</pre>


===Without Quotes===
===Without Quoted Literals===
Source size is 74 bytes; as noted above, as ALGOL 68G is an interpreter, there isn't a compiled object.<br>
Source size is 74 bytes; as noted above, as ALGOL 68G is an interpreter, there isn't a compiled object.<br>
Declares and uses a unary operator ! which is effectively an abbreviation for REPR (which converts an INT to a CHAR) and then uses this with the builtin + operator which appends CHARs or STRINGS to form another STRING.
Declares and uses a unary operator ! which is effectively an abbreviation for REPR (which converts an INT to a CHAR) and then uses this with the builtin + operator which appends CHARs or STRINGS to form another STRING.

Revision as of 22:22, 9 December 2021

Code Golf: Code Golf is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

First, show the shortest possible program that will emit the nine-character string string “Code Golf”, without the quotation marks and without anything after the final “f”. Then show the shortest possible program that does the same thing but without itself containing any string or character literals, and without requiring any input or any environment variables or command-line arguments, though the name of the running program can be used.

Extra credit: how big is the executable required to perform the first task? Skip details about any prior compilation steps that might be involved.

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.3.win32

With Quoted Literals

Source size is 18 bytes; as ALGOL 68G is an interpreter, there isn't a compiled object. <lang algol68>print("Code Golf")</lang>

Output:
Code Golf

Without Quoted Literals

Source size is 74 bytes; as noted above, as ALGOL 68G is an interpreter, there isn't a compiled object.
Declares and uses a unary operator ! which is effectively an abbreviation for REPR (which converts an INT to a CHAR) and then uses this with the builtin + operator which appends CHARs or STRINGS to form another STRING. <lang algol68>OP!=(INTc)CHAR:REPRc;INTo=111;print(!67+!o+!100+!101+!32+!71+!o+!108+!102)</lang>

Output:
Code Golf

Raku

Not very interesting, as it's pretty much just standard, non-obscure Raku. The output string is so short, there isn't any easy way to golf it shorter than just printing it directly. 17 bytes. <lang perl6>print </lang>

Output:
Code Golf

Wren

The shortest possible program (25 bytes) to print the required string is: <lang ecmascript>System.write("Code Golf")</lang> The size of the executable needed to run this or indeed any other standalone program (Wren-cli on Linux) is 414,760 bytes. However, if Wren were being embedded in a minimal C program, then the size of the executable would be 17,320 bytes.

If the program itself cannot contain literal strings and must end in 'f', then the shortest program I've been able to come up with (76 bytes or 79 counting the implicit \n at the end of the first three lines) is: <lang ecmascript>class Code{} class Golf{} System.writeAll([Code,String.fromByte(32),Golf]) Golf</lang>

Output:

In both cases:

Code Golf

Note that classes (like everything else in Wren) are objects and all objects have a default string representation which in the case of a class is its name.

Note also that Wren's single pass compiler allows the last line though it does nothing.

X86 Assembly

This is 100 bytes long (with CR+LF line endings). More useful than small, obfuscated source is small executable. This makes a 17-byte .COM file under MS-DOS. Assemble with: tasm and tlink /t. The xchg instruction is a single byte (as opposed to a straightforward 2-byte mov ah,9), and it takes advantage of the high byte of register bp being set to 09h when the program is started by MS-DOS. 09h selects the "display string" function. <lang asm>.model tiny .code org 256 s:xchg ax,bp mov dx,offset m int 33 ret m db "Code Golf$" end s</lang>

XPL0

This is 19 bytes long. I hate to say how big the executable is, but it's 54,400 bytes on the Raspberry Pi. Under MS-DOS a version of the compiler produces an executable as small as 6674 bytes. <lang XPL0> Text(0,"Code Golf")</lang>