Code Golf: Code Golf: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Without Quoted Literals: added commentary)
(→‎Without Quoted Literals: more commentary)
Line 176: Line 176:
===Without Quoted Literals===
===Without Quoted Literals===


<code>' [ 67 111 100 101 32 71 111 108 102 ] echo$</code> is marginally shorter but less interesting. For longer strings, encoding the text as a bignum rapidly becomes the more space efficient option.
<code>' [ 67 111 100 101 32 71 111 108 102 ] echo$</code> is marginally shorter but less interesting. For longer strings, encoding the text as a bignum rapidly becomes the more space efficient option. The text "Code Golf" is not quite long enough for the obvious improvement of using hexadecimal rather than decimal, as the digit reduction is less than the four character overhead of putting <code>hex</code> and a space before the number.


<lang Quackery>2549578149779768531 9 times [ 112 /mod emit ] drop</lang>
<lang Quackery>2549578149779768531 9 times [ 112 /mod emit ] drop</lang>

Revision as of 21:41, 10 February 2022

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 “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.

Action!

With Quoted Literals

<lang action!>PROC M()Print("Code Golf")</lang>

Output:
Code Golf

Without Quoted Literals

<lang action!>PROC M()Put(67)Put(111)Put(100)Put(101)Put(32)Put(71)Put(111)Put(108)Put(102)</lang>

Output:
Code Golf

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. The interpreter itself is 2780 K. <lang algol68>print("Code Golf")</lang>

Output:
Code Golf

Without Quoted Literals

Source file size is 67 bytes (says Windows DIR); as noted above, ALGOL 68G is an interpreter so there isn't a compiled object. The interpreter itself is 2780 K.
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:REPR(c+32);print(!35+!79+!68+!69+!0+!39+!79+!76+!70)</lang>

Output:
Code Golf

AWK

<lang AWK>

  1. syntax: GAWK -f CODE_GOLF.AWK
  2. Under MS-Windows 10 using Thompson Automation's TAWK 5.0c AWKW -xm
  3. the compiled length of each program is 34,936 bytes and all three is 35,140 bytes.
  4. Each requires the Awkr50w.EXE runtime of 231,936 bytes.
  5. Under MS-Windows 10 using Thompson Automation's TAWK 5.0c AWKW -xe
  6. the compiled length of each program is 244,856 bytes and all three is 245,060 bytes.
  7. This is a completely stand-alone executable.
  8. 25 bytes

BEGIN{print("Code Golf")}

  1. 52 bytes

BEGIN{print("\x43\x6F\x64\x65\x20\x47\x6F\x6C\x66")} BEGIN{print("\103\157\144\145\040\107\157\154\146")} </lang>

Output:
Code Golf
Code Golf
Code Golf

FreeBASIC

With a quoted string, the following weighs in at 12 bytes. <lang freebasic>?"Code Golf"</lang>

For the second task, this is 80 bytes long. <lang freebasic>dim as byte i,a(8)={37,81,70,71,2,41,81,78,72} for i=0 to 8 ?chr(30+a(i)); next</lang>

Both compile to a file 27,016 bytes long.

Go

Go isn't well equipped for Code Golf as a certain amount of ceremony (package main and func main()) are needed for any executable.

The shortest possible program (44 bytes) to print the required string is: <lang go>package main;func main(){print("Code Golf")}</lang>


If the program itself cannot contain string or character literals, then the shortest program I've been able to come up with (81 bytes) is: <lang go>package main;func main(){print(string([]byte{67,111,100,101,32,71,111,108,102}))}</lang> Output in both cases

Code Golf

The size of the executables are 1,158,158 and 1,158,174 bytes respectively though this will obviously depend on Go version, platform and build options being used.

jq

Works with: jq

Works with gojq, the Go implementation of jq

To skip the newline, the interpreter must be invoked with the -j option:

$ jq -nj '"Code Golf"' | wc -c
       9

For the second task, the following program clocks in at 41 bytes: <lang>[-33,11,0,1,-68,-29,11,8,2|.+100]|implode</lang>

Extra credit: The jq executable on my Mac is 461,864 bytes; gojq's is over 8 times larger.

Pascal

Free Pascal

"Code Golf" as Hex in little Endian ending in 0x00 86 byte. linux executable fpc 3.2.2 : 8x386 183400 Byte | x64 191104 byte <lang pascal> var a:QWord=$006F472065646F43;b:DWord=$0000666C;BEGIN write(pChar(@a),pChar(@b));END. </lang>

Output:
Code Golf

Perl

<lang perl># 20211216 Perl programming solution

  1. 1 2
  2. 12345678901234567890
print 'Code Golf'    # 17 bytes
print "\n";
  1. 1 2 3 4
  2. 1234567890123456789012345678901234567890
print pack'H*','436F646520476F6C66'      # 35 bytes
print "\n";</lang>

Actually just for storage purpose it is possible (just not always) to store the bytes string as an UTF string. <lang perl>binmode STDOUT, ":encoding(UTF-8)";

my $string = "\x{436F}\x{6465} \x{476F}\x{6C66}"; print "\n"; print "\n'$string' is an UTF string with length = ".length($string)."\n"; print "\n";

my ( $bytes, $offset ) = , 0 ;

for ( map { ord $_ } split //, $string ) {

  my @ar = ();
  while ( $_ > 0 ) { unshift @ar, $_ & 0xff and $_ >>= 8 }
  for ( @ar ) { vec( $bytes, $offset++, 8 ) = $_ }

}

print "Extract to bytes '$bytes' with length = ".length($bytes)."\n";</lang>

Output:
'䍯摥 䝯汦' is an UTF string with length = 5

Extract to bytes 'Code Golf' with length = 9

Phix

puts(1,"Code Golf")

Which is 19 bytes. Note that ?"Code Golf", while only 12 bytes, does print the quotation marks and therefore does not meet the task specifications.
Without using string literals, at 42 bytes we can have

puts(1,{67,111,100,101,32,71,111,108,102})

Slightly shorter, at 30 bytes, though it could be considered string/char:

puts(1,x"436F646520476F6C66")

The compiled size of the first is 276,992 bytes. You can actually make a smaller executable as follows:

include puts1h.e
puts1("Code Golf")

Then compile it with p -c -nodiag test.exw (or whatever) to yield an executable of 36,532 bytes - no diagnostics, which is itself non-trivial and otherwise pulls in file handling (for the ex.err it writes), printf, ffi, and they in turn pull in almost every builtin in existence between them. However even without all that lot it still needs stack, unassigned, and heap handlers, and unfortunately the latter also drags in delete() and therefore callfunc and therefore a whole bunch of subscript stuff we don't rightly need... still I suppose 36K ain't really all that bad. Oh, I should also say the compiler/interpreter/linker/debugger is itself (currently) 2,789,376 bytes, plus you'll still need most of builtins/ which is around the 9MB mark.

Quackery

With Quoted Literals

<lang Quackery>say "Code Golf"</lang>

Without Quoted Literals

' [ 67 111 100 101 32 71 111 108 102 ] echo$ is marginally shorter but less interesting. For longer strings, encoding the text as a bignum rapidly becomes the more space efficient option. The text "Code Golf" is not quite long enough for the obvious improvement of using hexadecimal rather than decimal, as the digit reduction is less than the four character overhead of putting hex and a space before the number.

<lang Quackery>2549578149779768531 9 times [ 112 /mod emit ] drop</lang>

R

I suspect there may be shorter methods, but these are my best attempt. <lang R>## easy way cat("Code Golf")

    1. no quotes or string literals

cat(rlang::string(c(0x43, 0x6F, 0x64, 0x65, 0x20,

                   0x47, 0x6F, 0x6C, 0x66)))

</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

Assuming we can't use the string literal in the source, the shortest I've come up with is:

<lang perl6>print chrs (-32,12,1,2,-67,-28,12,9,3) »+»99 # 45 Chars, 47 bytes</lang> <lang perl6>print chrs (-3,㊶,㉚,㉛,-㊳,1,㊶,㊳,㉜) »+»㉎ # 37 Chars, 56 bytes</lang> <lang perl6>print <Dpef!Hpmg>.ords».pred.chrs # 33 Chars, 34 bytes. Somewhat cheaty as it _does_ contain a string literal, but not the same literal as the output</lang> Same output for each. Of course, to actually run any of that code you need the Raku compiler at 18.0Kb, the nqp vm interpreter at 17.9 Kb and the moar virtual machine at 17.9Kb. (Or the Java virtual machine, which is remarkably difficult to come up with a size for...)

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 string or character literals, then the shortest program we've been able to come up with (71 bytes) - thanks to Thundergnat! - is: <lang ecmascript>for(c in[-32,12,1,2,-67,-28,12,9,3])System.write(String.fromByte(c+99))</lang>

Output:

In both cases:

Code Golf

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 characters 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> This version without a string or character literals is 33 characters long. <lang XPL0>Text(0,[$65646f43,$6c6f4720,$e6])</lang>

Output:
Code Golf