Code Golf
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.
Using string literals, the following weighs in at 23 bytes.
print(end' ‘Code Golf’)Without string literals, this is 60 bytes long.
L(c)[37,9,2,3,70,33,9,10,0]{print(end' Char(code' c(+)102))}/* ARM assembly AARCH64 Raspberry PI 3B */
/* program codegolf64.s */
/*********************************/
/* Initialized data */
/*********************************/
.data
szString: .asciz "codeGolf"
.equ SIZESTRING, . - szString
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x2,SIZESTRING // string length
ldr x1,=szString
mov x0,1 // output Linux standard
mov x8,64 // code call system "write"
svc 0
mov x0,0 // return code
mov x8,93 // request to exit program
svc #0 // perform the system call- Output:
Compilation 64 bits Rosetta de codegolf64.s -rwx------ 1 u0_a344 u0_a344 1144 May 24 21:48 codegolf64 -rw------- 1 u0_a344 u0_a344 960 May 24 21:48 codegolf64.o -rw------- 1 u0_a344 u0_a344 813 May 24 21:45 codegolf64.s Fin de compilation. ~/.../rosetta/asm4 $ codegolf64 codeGolf~/.../rosetta/asm4 $
Not counting vector tables, disk/cartridge headers, and/or font graphics data, here is as small as I could get (example is for commodore 64)
With Quoted Literals
m
LDX #0
LDA G,x
BEQ d
jsr -46
jmp m+2
d
rts
G
db "Code Golf",0Without Quoted Literals
p equ -46
LDA #67
JSR p
LDA #111
JSR p
LDA #100
JSR p
LDA #101
JSR p
LDA #32
JSR p
LDA #71
JSR p
LDA #111
JSR p
LDA #108
JSR p
LDA #102
JMP pThis program runs under CP/M and will assemble with the CP/M assembler. It is 46 bytes, and the resulting executable is 18 bytes.
org 256!lxi d,264!mvi c,9!jmp 5!db'Code Golf$'
Without a string literal, the next best option is to define the bytes numerically. This takes 70 bytes. The resulting executable is the exact same, as this is merely a different way of writing the same program.
org 256!lxi d,264!mvi c,9!jmp 5!db 67,111,100,101,32,71,111,108,102,36
With String literal:
with Ada.Text_IO;procedure C is begin Ada.Text_IO.Put("Code Golf");end;
71 characters
Without Character or String literals (all one line):
with Ada.Text_IO;procedure C is L:array(1 .. 9)of Integer:=(67,111,100,101,32,71,111,108,102);begin for C of L loop Ada.Text_IO.Put(Character'Val(C));end loop;end;
163 characters; this should be portable to all Ada-12 compilers and all platforms
If we presume Linux, GNAT, and an executable name of "Code Golf", this can be shortened to (all one line):
with Ada.Command_Line;with Ada.Text_IO;procedure C is begin Ada.Text_IO.Put(Ada.Command_Line.Command_Name(3..11));end;
118 Characters
With Quoted Literals
PROC M()Print("Code Golf")- Output:
Code Golf
Without Quoted Literals
PROC M()Put(67)Put(111)Put(100)Put(101)Put(32)Put(71)Put(111)Put(108)Put(102)- Output:
Code Golf
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.
print("Code Golf")- Output:
Code Golf
Without Quoted Literals
Source file size is 65 bytes; 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.
OP!=(INTc)CHAR:REPR(111-c);print(!44+!0+!11+!10+!79+!40+!0+!3+!9)- Output:
Code Golf
/* ARM assembly Raspberry PI */
/* program codegolf.s */
/*********************************/
/* Initialized data */
/*********************************/
.data
szString: .asciz "codeGolf"
.equ SIZESTRING, . - szString
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
mov r2,#SIZESTRING @ string length
ldr r1,=szString
mov r0,#1 @ output Linux standard
mov r7,#4 @ code call system "write"
svc 0
mov r0, #0 @ return code
mov r7, #1 @ request to exit program
svc #0 @ perform the system call- Output:
Compilation 32 bits de codegolf.s -rwx------ 1 u0_a252 u0_a252 904 May 24 21:32 codegolf -rw------- 1 u0_a252 u0_a252 740 May 24 21:32 codegolf.o -rw------- 1 u0_a252 u0_a252 816 May 24 18:35 codegolf.s Fin de compilation. ~/.../rosetta/asm4 $ codegolf codeGolf~/.../rosetta/asm4 $
print"Code Golf"
print join to[:char][67 111 100 101 32 71 111 108 102]
- Output:
Code GolfCode GolfCode Golf
# syntax: GAWK -f CODE_GOLF.AWK
#
# Under MS-Windows 10 using Thompson Automation's TAWK 5.0c AWKW -xm
# the compiled length of each program is 34,936 bytes and all three is 35,140 bytes.
# Each requires the Awkr50w.EXE runtime of 231,936 bytes.
#
# Under MS-Windows 10 using Thompson Automation's TAWK 5.0c AWKW -xe
# the compiled length of each program is 244,856 bytes and all three is 245,060 bytes.
# This is a completely stand-alone executable.
#
# 24 bytes
BEGIN{printf"Code Golf"}
Using a string literal as conversion argument only:
# 64 bytes
BEGIN{for(n=15162543273030444;n;n=int(n/80))printf"%c",111-n%80}
This should work with POSIX-compliant implementations (support for hex literals is not mandatory), in double-precision floating-point arithmetic.
Ballerina is ill-suited to code golf as any executable normally needs a main() or init() function and, if you want to print something to the terminal, you need to import the ballerina/io module. However, whilst it's a horrible hack, I found that you can dispense with the former requirement by the simple expedient of assigning the print function to a module variable!
So, the shortest possible program (46 bytes) to print the required string is:
import ballerina/io;()a=io:print("Code Golf");
If the program itself cannot contain string or character literals, then the shortest program I've been able to come up with (87 bytes) is:
import ballerina/io;()a=io:print(string:fromBytes([67,111,100,101,32,71,111,108,102]));
Output in both cases
Code Golf
The size of the .jar files produced (Linux deb distro) is 9,097,902 and 9,097,775 bytes respectively. These are so huge because they need to include code for the ballerina/io module and all its dependencies in order to run as stand-alone executables. However, I'm at a loss to explain why the first program needs a 127 byte larger .jar file than the second!
The directly executable source code is 14 bytes by using its script name instead of character literals:
echo -n ${0:2}
To run:
./Code\ Golf
11 characters with a quoted string.
?"Code Golf
60 characters or 49 bytes in memory without quoted literals.
0FORI=1TO9:READC:?CHR$(239-C);:NEXT:DATA44,,11,10,79,40,,3,9
138 characters or 27 bytes in memory without quoted literals using the program name run from Apple DOS 3.3.
0CALL2061:END:????????????
FORI=1TO12:POKE2060+I,VAL(MID$("160247185012001032092219200208247096",I*3-2,3)):NEXT
SAVECode Golf
RUNCode Golf
With a quoted string, the following weighs in at 12 bytes.
?"Code Golf"
For the second task, this is 66 bytes long.
dim a={37,81,70,71,2,41,81,78,72}
for i=0 to 8
?chr(30+a[i]);
next
Note: BASIC256 is an interpreter, it does not generate executables.
12 characters with a quoted string:
?"Code Golf"
49 characters without quoted literals:
FOR i in [44,0,11,10,79,40,0,3,9] DO ?CHR(111-i);
Quoted string: 17 characters
Print "Code Golf"
ASCII codes: 70 characters
Push 9,3,0,40,79,10,11,0,44:Do While Used();Print Chr(111-Pop());:Loop
With a string literal, it can be done straightforwardly in 45 bytes:
get"libhdr";let start()be writes("Code Golf")Without a string literal, a portable version (assuming an ASCII system) can be written as such, which takes 79 bytes:
get"libhdr";let start()be for i=0to 8wrch(32+i!table 35,79,68,69,0,39,79,76,70)If, however, we assume a 16-bit, low-endian system (e.g. the CP/M BCPL compiler), it can be done in 68 bytes:
get"libhdr";let start()be writes(table 17161,25711,8293,28487,26220)Using a 32-bit, low-endian system (e.g. on Linux), we can shave of another byte, for 67 bytes:
get"libhdr";let start()be writes(table 1685013257,1866932325,26220)
shortest: 10 bytes
*Code Golf
avoiding ASCII: 23 bytes
46 60 17 ac 23 40 b0 02 cf f7 97 f7 ee 80 bc 90 9b 9a df b8 90 93 99
By using a string literal:
•Out"Code Golf"
Without quoted literals:
•Out@+111-44‿0‿11‿10‿79‿40‿0‿3‿9
The following answers assume compilation using gcc 11.3.0 on ubuntu 22.04, without using any special options and ignoring the warnings.
The shortest possible program (28 bytes) to print the required string is:
main(){printf("Code Golf");}
The size of the executable needed to run this is 15,960 bytes.
If the program itself cannot contain string or character literals, then the shortest program I've been able to come up with (50 bytes) is:
a[]={0x65646f43,0x6c6f4720,102};main(){printf(a);}
The size of the executable needed is now 15,992 bytes.
Output in both cases:
Code Golf
Using a string literal, this program is 62 bytes long.
Program-ID.C.Procedure division.Display"Code Golf"no advancing
Without a string literal, the program is 73 bytes long.
Program-ID.C.Procedure division.Display X"436F646520476F6C66"no advancing
For both programs, the executable produced by the GnuCOBOL compiler weighs in at 9,400 bytes.
By using a string literal (12 characters):
[Code Golf]PWithout quoted literals (22 characters):
16i436F646520476F6C66PDelphi says the code size is 5,180 bytes. Looking at the assembly language that is generated, the size is 132 bytes. The difference must be the Windows overhead for a console application.
program Project1;
{$APPTYPE CONSOLE}
begin
WriteLn('code golf');
WriteLn(#67,#111,#100,#101,#32,#71,#111,#108,#102);
end.
- Output:
Code Golf Code Golf
# with string literal (16 characters)
write"Code Golf"
# without quoted literals (54 characters)
for i in [44 0 11 10 79 40 0 3 9]write strchar(111-i).
The second part of the challenge is not accomplishable in ed, because there's no way to insert arbitrary characters with escape sequences. Also note that the trailing newline in the output is unavoidable, like in #sed.
# by Artyom Bologov
a
Code Golf
.
p
Q
write("Code Golf")
^|EMal supports blobs (byte arrays) that can be initialized with single bytes|^
write(blob().of(67,111,100,101,32,71,111,108,102))- Output:
Code GolfCode Golf
[I Code GolfI]
{ 67 111 100 101 32 71 111 108 102 } write
The executable is 2,265 KB.
CR ." Code Golf" CR \ 20 bytes with need to type return key
3313297 2532384 58 BASE ! CR . . CR \ 36 bytes
DECIMAL \ +8 to restore 'normal' behaviour
[0] [IF] \ Commented version
3313297 2532384 \ Two integers base 58 Golf Code
58 BASE ! \ Change to base 58.
CR . . CR \ Print the two integers in base 58
DECIMAL \ Restore the normal base, HEX would be shorter.
[THEN]
CR ." Code Golf" CR Code Golf ok 3313297 2532384 58 BASE ! CR . . CR DECIMAL Code Golf ok
The shortest Fortran program that writes "Code Golf" has two statements: one for the actual printing, and one end statement every Fortran program is required to have.
The total length of following little program is 26 characters.
write(*,*)'Code Golf'
end
We are free to print the "Gode Golf" as one single entity as shown above, or we can
print each single letter:
The total length of following little program is 49 characters.
write(*,*)'C','o','d','e',' ','G','o','l','f'
end
This code still contains the quotes as delimiter for the character constants. To avoid this, we can use the ASCII representation of each letter and convert the numeric constants to characters while printing:
The total length of following little program is 100 characters.
write(*,*)char(67),char(111),char(100),char(101),char(32),char(71),char(111),char(108),char(102)
end
Ancient Fortran versions required hollerith constants instead of strings in quotes. Both ifx and gfortran still accept this style, gfortran issues a warning "Legacy Extension".
The total length of following little program is 26 characters.
write(*,*)9hCode Golf
end
All four programs will be three characters shorter if we replace "Write (*,*)" by the equivalent "print *,"
- Output:
All four shown variants produce the identical output:
Code Golf
With a quoted string, the following weighs in at 13 bytes.
?"Code Golf";
Without quoted literals, this is 77 bytes long.
dim as byte i,a(8)={44,0,11,10,79,40,0,3,9}:for i=0 to 8:?chr(111-a(i));:next
Both compile to a file 27,016 bytes long.
"Code Golf" as Hex in little Endian ending in 0x00 86 byte. linux executable fpc 3.2.2 : 8x386 183400 Byte | x64 191104 byte
var a:QWord=$006F472065646F43;b:DWord=$0000666C;BEGIN write(pChar(@a),pChar(@b));END.
- Output:
Code Golf
The shortest way (15 chars) is to use the stop function. Using the print function prints to a window, but it vanishes instantly without adding the lengthy handleevents, which allows the code to be interactive.
stop"Code Golf"Without literals, I poked two values into a string, then printed it with the stop function: 50 chars.
str15 s:~@s,0x6F472065646F4309:%@s+8,0x666C:stop sBoth produce this result:
- Output:
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:
package main;func main(){print("Code Golf")}
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:
package main;func main(){print(string([]byte{67,111,100,101,32,71,111,108,102}))}
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.
With a quoted string, the following weighs in at 14 bytes.
"Code Golf":n;For the second task, this is 38 bytes long.
67 111 100 101 32 71 111 108 102]'':n+- Output:
In both cases:
Code Golf
For this bit of silliness, eliminating a trailing newline on stdout is probably the most difficult issue. So, we limit our implementation to linux and use /proc/self/fd/1
Sadly, we need to use a character literal to reference /proc/self/fd/1
But we do not need that reference to live in the implementation -- this task explicitly allows us to use the name of the running program.
So, our program looks like this:
#!/usr/bin/env jconsole
exit".(a.C.~<45 47){~a.i.;}.ARGV
And we name our program "exit'Code Golf'fwrite'-proc-self-fd-1'"
Here's an example bash session, illustrating this incredibly useful program:
$ "exit'Code Golf'fwrite'-proc-self-fd-1'" | wc
0 2 9
$ "exit'Code Golf'fwrite'-proc-self-fd-1'"
Code Golf$
Extra Credit: this program occupies 57 bytes (ignoring OS overhead, such as the name of the routine and the minimum size allocated to any file with any content).
(Note: if we were careful about the current directory we were in when we executed this program, we could eliminate the part that swaps - and / characters ((a.C.~<45 47){~a.i.). Removing those 19 characters and creating four directories to hold the program and invoking the program as "exit'Code Golf'fwrite'"/proc/self/fd/"1'" might even be within the spirit of this task. However... we'll leave that as an exercise for the reader...)
Alternatively, if we are interested in the size of the J executable, jconsole currently clocks in at 140k bytes. However, this is misleading, as it ignores the size of necessary shared libraries (not to mention the OS Kernel and necessary supporting files)...
With string literals (24 Bytes).
console.log("Code Golf")
Without string literals (71 bytes).
console.log(String.fromCharCode(...[67,111,100,101,32,71,111,108,102]))
By using a string literal (20 characters):
"Code Golf"putchars.Without string literals (48 characters):
[35 79 68 69 0 39 79 76 70][32 + chr putch]step.Works with gojq, the Go implementation of jq
Works with jaq, the Rust 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 38 bytes:
[44,0,11,10,79,40,0,3,9|111-.]|implode
Extra credit: The jq executable on my Mac is 461,864 bytes; gojq's is over 8 times larger, and jaq's is slightly larger still.
print("Code Golf")
print(String(Char.([67,111,100,101,32,71,111,108,102])))
Shortest program:
fun main()=print("Code Golf")
Without string literals, JVM only:
fun main()=print(String(byteArrayOf(67,111,100,101,32,71,111,108,102)))
Without string literals, platform-independent:
fun main()=print(byteArrayOf(67,111,100,101,32,71,111,108,102).decodeToString())
A shorter but hacky version:
fun main(){listOf(35,79,68,69,0,39,79,76,70).map{print(' '+it)}}
By just passing strings (17 bytes):
echo -n Code Golf
Let the shell generate every character (54 bytes):
typeset -i43 a=3066215 b=3384588;echo -n ${a:3} ${b:3}
Or, in case format strings are allowed for conversion (36 bytes):
printf %..39d%5..43d 2291147 3384588
With text literal (19 Bytes):
fn.print(Code Golf)Without text literal (88 Bytes):vb
parser.op(print(join(\e, arrayMapToNew([67,111,100,101,32,71,111,108,102], fn.toChar))))With string literal (21 characters):
io.write("Code Golf")
Without quoted literals (55 characters):
io.write(string.char(67,111,100,101,32,71,111,108,102))
Module CodeGolf {
prototype{?"Code Golf"}as a$
print len(a$)=12
inline a$
prototype {keyboard 67,111,100,101,32,71,111,108,102:Input"",a$} as a1$
print len(a1$)=52
after 200{keyboard 13} ' press enter after 200ms - not need to run
inline a1$
prototype {?union.data$(67,111,100,101,32,71,111,108,102)} as a$
print len(a$)=46
inline a$
' gsb files can be utf-16le, utf-8, ansi
' this is ansi export
open "exec1.gsb" for output as #f
?#f,a1$+":end"; ' plus 4 chars to close the program after pressing enter.
close #f
print filelen("exec1.gsb")=56 ' bytes
use exec1 ' execute exec1.gsb in a different M2000 environment
}
CodeGolf
Using a string literal, this program is 29 bytes long.
TextWindow.Write("Code Golf")Without a string literal, the program is 221 bytes long.
TextWindow.Write(Text.GetCharacter(67)+Text.GetCharacter(111)+Text.GetCharacter(100)+Text.GetCharacter(101)+Text.GetCharacter(32)+Text.GetCharacter(71)+Text.GetCharacter(111)+Text.GetCharacter(108)+Text.GetCharacter(102))By using a string literal (16 characters):
"Code Golf"printWithout string literals (52 characters):
(35 79 68 69 0 39 79 76 70) (32+ chr putchr) foreachThe shortest possible program (19 bytes) to print the required string is:
print"Code Golf",""
The size of the executable needed to run this (MiniScript Command-line v1.3 on Linux) is 690,984 bytes.
If the program itself cannot contain string or character literals without anything following, then the shortest program I've been able to come up with (63 bytes) is:
for c in[44,0,11,10,79,40,0,3,9];print char(111-c),null;end for
Also, although it's cheating a bit, if we call the script file itself 'Code Golf' (including the space and without any extension), then the following weighs in at 23 bytes:
print shellArgs[0],null
- Output:
In all 3 cases:
Code Golf
Part 1
This is trivially simple with NetRexx, the solution is a one line program that can be run with the nr command thus: nr programname
say 'Code Golf'
Program size: 16 characters.
Part 2
With a program name Code_Golf.nrx
the string can be extracted.
parse source . . name .
say name.substr(1, 4) name.substr(6, 4)
The size of Code_Golf.nrx is 64 characters.
- Output:
Code Golf
Using a string literal (24 characters):
stdout.write "Code Golf"
Compiling on Linux with Nim 1.6.12 using command nim c -d:danger --opt:size -d:lto --gc:arc code_golf.nim, the executable size is 23584 bytes.
Without string literals (61 characters):
for n in[67,111,100,101,32,71,111,108,102]:stdout.write n.chr
Compiling on Linux with Nim 1.6.12 using command nim c -d:danger --opt:size -d:lto --gc:arc code_golf.nim, the executable size is 22528 bytes.
With string literal (20 characters):
print -n 'Code Golf'Without quoted literals (31 characters):
0x[436f646520476f6c66]|print -rWith string literal (29 characters):
let()=print_string"Code Golf"
Without quoted literals (75 characters):
let()=List.iter(fun c->print_char(Char.chr(111-c)))[44;0;11;10;79;40;0;3;9]
With a quoted string, the following weighs in at 18 bytes.
text("Code Golf");
For the second task, this is 46 bytes long.
text(chr([67,111,100,101,32,71,111,108,102]));
The shortest ISO-compliant Pascal program is 46 characters.
program p(output);begin write('Code Golf')end.
Unless you make certain presumptions about the target system, you cannot achieve the second task in Pascal (as defined by the ISO standards). Therefore, see Free Pascal for one method.
###
Pr('Code Golf')
18 chars in a code.
###
Pr(#67#111#100#101#32#71#111#108#102)
Without quotes
Using a string literal:
# 1 2
#12345678901234567890
print'Code Golf' # 16 bytes
Without quoted literals:
# 1 2 3 4 5
#12345678901234567890123456789012345678901234567890
print chr($_^102)for 37,9,2,3,70,33,9,10,0 # 42 bytes
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})
Or quite long but deliciously cryptic:
puts(1,atom_to_float64(1.276409856e-152)[4..$]&
atom_to_float64(1.458406353e-258)[4..$])
Slightly shorter, at 30 bytes, though it could be considered string/char:
puts(1,x"436F646520476F6C66")
While not exactly shorter, if you name the source code as Code Golf[.exw] or the executable as Code Golf[.exe], perhaps needing a substitute(s,'_',' ') [or (..,95,32)], this approach will also work:
puts(1,get_file_base(command_line()[2]))
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.
/# Rosetta Code problem: http://rosettacode.org/wiki/Code_Golf
by Galileo, 10/2022 #/
include ..\Utilitys.pmt
"Code Golf" ?
( 67 111 100 101 32 71 111 108 102 ) len for get tochar print endfor nl
def >char tochar enddef
( 67 111 100 101 32 71 111 108 102 ) getid >char map lprint- Output:
Code Golf Code Golf Code Golf
As a string:
main => "Code Golf".print.- Output:
Code Golf
No quotes:
main => [67,111,100,101,32,71,111,108,102].map(chr).print.- Output:
Code Golf
This program runs under CP/M. No character literals are used, the 8080 PL/M compiler does not support lower case input, meaning this is the shortest you can get it either way. The line break is necessary because the PL/M compiler only reads the first 80 characters on a line.
The program is 117 bytes, assuming CP/M-style line breaks (which are
\r\n, same as Windows). The resulting executable is 45 bytes.
256:G:PROCEDURE(X,Y);DECLARE(X,Y)ADDRESS;GOTO 5;END G;CALL G(9,.(67,111,100,101
,32,71,111,108,102,36));GOTO 0;EOFUsing a string literal, this program is 89 bytes long.
To run:Start up.Write"Code Golf"to the console without advancing.Wait for the escape key.
Without a string literal, the program is 98 bytes long.
To run:Start up.Write$436F646520476F6C66 to the console without advancing.Wait for the escape key.
The executable compiled by the Plain English compiler weighs in at 143,360 bytes.
Unable to improve upon the Lua entry, so:
With string literal (21 characters):
io.write("Code Golf")
Without quoted literals (55 characters):
io.write(string.char(67,111,100,101,32,71,111,108,102))
The size of the executable needed to run these or indeed any other standalone program (Pluto 0.10.5 on Linux) is 2,663,952 bytes.
With a quoted string, the following weighs in at 18 bytes.
Print("Code Golf")
For the second task, this is 69 bytes long.
Dim a(8)
a(0)=37:a(1)=81:a(2)=70:a(3)=71
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72
For i=0 To 8
Print(Chr(30+a(i)))
Next
The size of the executables are 7680 and 10752 bytes respectively though this will obviously depend on PureBasic version, platform and build options being used.
- Python 3
Using string literals, the following weighs in at 22 bytes.
print(end="Code Golf")
Without string literals, this is 52 bytes long.
print(end=0x436f646520476f6c66.to_bytes(9).decode())
# or:
for c in 37,9,2,3,70,33,9,10,0:print(end=chr(c^102))
With a quoted string, the following weighs in at 17 bytes.
PRINT "Code Golf"
For the second task, this is 91 bytes long.
DIM a(8)
DATA 37,81,70,71,2,41,81,78,72
FOR i = 0 TO 8
READ a(i)
PRINT CHR$(30 + a(i));
Note: QBasic is an interpreter, it does not generate executables.
With Quoted Literals
say "Code Golf"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.
2549578149779768531 9 times [ 112 /mod emit ] dropI suspect there may be shorter methods, but these are my best attempt.
## easy way
cat("Code Golf")
## no quotes or string literals
cat(rlang::string(c(0x43, 0x6F, 0x64, 0x65, 0x20,
0x47, 0x6F, 0x6C, 0x66)))
- Output:
Code Golf
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.
print <Code Golf>
- Output:
Code Golf
Assuming we can't use the string literal in the source, the shortest I've come up with is:
print chrs 37,9,2,3,70,33,9,10,0 X+^102 # 39 chars, 39 bytes
print chrs -3,㊶,㉚,㉛,-㊳,1,㊶,㊳,㉜X+㉎ # 33 Chars, 49 bytes
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
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...)
Using a string literal, this program is 15 bytes long.
prin"Code Golf"
Without a string literal, the program is 67 bytes long.
prin rejoin map-each c[67 111 100 101 32 71 111 108 102][to-char c]
Or, in case binary literals are allowed, the program is 51 bytes long.
s: enbase #{1A875E1A895F}insert at s 5 space prin s
In Rebol3 `enbase` requires it's `base` argument, so it must be:
s: enbase #{1A875E1A895F}64 insert at s 5 SP prin s
Using binary is 36 bytes long.
prin to-string #{436F646520476F6C66}
Part 1
say 'Code Golf'
Part 2
Assumes the program file name is Code_Golf.rex
parse source . . name .
pathlen = length(name)
say substr(name, pathlen - 12, 4) substr(name, pathlen - 7, 4)
- Output:
Code Golf
≪ "Code Golf" ≫
The above code costs 48 nibbles (e.g. 4 bits) of memory (e.g. 24 bytes); the string itself requires 28 quartets.
≪ #102d #108d #111d #71d #32d #101d #100d #111d 67 CHR 1 8 START SWAP B→R CHR + NEXT ≫
This string-free code costs 233 nibbles (117 bytes). Unsigned integers - numbers starting with a # - have been used whenever possible since they use 9 nibbles less than floating-point numbers to store a value up to #FF
$><<"Code Golf" #15 chars
puts
# Taken from Perl:
$><<['436F646520476F6C66'].pack('H*') #37 charsWith a quoted string, the following weighs in at 17 bytes.
print "Code Golf"For the second task, this is 82 bytes long.
dim a(8):data 37,81,70,71,2,41,81,78,72:for i=0 to 8:read j:print chr$(30+j);:nextNote: Run BASIC is an interpreter, it does not generate executables.
With sed, we can hardly stick to the rules. It requires at least one byte of input (to run the script at all), and cannot suppress a trailing newline on output.
$ echo | sed 's/.*/Code Golf/'
Code GolfSed cannot translate/insert characters without having the target being specified in the code. So the best we can do, is to hide them behind escape-sequences (even with that, we can't avoid all letters from the target string). And, to limit ourselves a bit, we use each escape-sequence only once). That script is not POSIX-compliant, but works at least with GNU sed.
$ echo | sed 's/.*/\x6f/;s/./\x43&\x64\x65\c`\x47&\x6c\x66/'
Code GolfBy using a string literal, the following weighs in at 22 bytes.
PRINT "Code Golf";
ENDWithout quoted literals, this is 92 bytes long.
DIM a(9)
DATA 44,0,11,10,79,40,0,3,9
FOR i=1 to 9
READ a(i)
PRINT CHR$(111-a(i));
NEXT i
ENDBy using a string literal (18 characters):
echo 'Code Golf\c'By using just a format specifier (56 characters):
printf $(printf \\\\%o 67 111 100 101 32 71 111 108 102)Using a "string literal" / raw ASCII rune (53 characters):
#0110 2194 06 #18 1720 fff7 #800f 1700 "Code 20 "GolfWithout raw ASCII runes (61 characters):
#0110 2194 06 #18 1720 fff7 #800f 1700 436f 6465 2047 6f6c 66Both programs assemble to the same 26 byte ROM:
a001 1021 9406 8018 1720 fff7 a080 0f17 0043 6f64 6520 476f 6c66
The 9 byte string accounts for almost 35% of the final ROM.
The golfed code works very similarly to the following code:
%\0 { 00 } %\s { 20 }
|18 @Console/write
|100
( get pointer to byte before str )
;str #0001 SUB2
&loop
( load a byte, pre-increment, and write it )
INC2 LDAk DUP .Console/write DEO
( loop again if the byte was non-null )
?/loop
( exit )
BRK
@str "Code \s "Golf \0The main trick used in golfing of this program was to encode the raw instruction bytes in hex in the source code.
With a quoted string, the following weighs in at 64 bytes.
module main;
initial begin $write("Code Golf");
end
endmoduleFor the second task, this is 102 bytes long.
module main;
initial begin $write("%c%c%c%c %c%c%c%c",67,111,100,101,71,111,108,102);
end
endmodule- Output:
In both cases:
Code Golf
The shortest possible program to print the required string is:
print("Code Golf")If the program itself cannot contain string or character literals, then use byte list:
print([u8(67),111,100,101,32,71,111,108,102].bytestr())Output in both cases
Code Golf
The shortest possible program (25 bytes) to print the required string is:
System.write("Code Golf")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 (68 bytes) is:
for(c in[37,9,2,3,70,33,9,10,0])System.write(String.fromByte(c^102))- Output:
In both cases:
Code Golf
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.
.model tiny
.code
org 256
s:xchg ax,bp
mov dx,offset m
int 33
ret
m db "Code Golf$"
end sThis 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.
Text(0,"Code Golf")This version without a string or character literals is 33 characters long.
Text(0,[$65646f43,$6c6f4720,$e6])- Output:
Code Golf
With a quoted string, the following weighs in at 12 bytes.
?"Code Golf"Without quoted literals, this is 63 bytes long.
data 44,0,11,10,79,40,0,3,9:for i=0to 8read n:?chr$(111-n);nextNote: Yabasic is an interpreter, it does not generate executables.
!ys-0
defn main():
say: 'Code Golf'
nums =: +[67 111 100 101 32 71 111 108 102]
chars =: nums.map(C)
say: chars:join- Output:
$ ys code-golf.ys Code Golf Code Golf
Thanks to the Amstrad CPC's kernel, we can reduce our line count greatly by abstracting print routines to a single CALL statement. In addition, WinAPE lets us load our executable directly into memory without the need for a disk by simply using an ORG directive to define the starting address.
With Quoted Literals
Total: 11 lines.
org &200
ld hl,g
o:
ld a,(hl)
or a
ret z
call &bb5a
inc hl
jr o
g:
db "Code Golf",0Hexdump of the executable:
7E B7 C8 CD 5A BB 23 18 F7 43 6F 64 65 20 47 6F 6C 66 00
Total: 19 bytes.
Without Quoted Literals
Total: 20 lines
org &200
q equ &bb5a
LD A,67
call q
LD A,111
call q
LD a,100
call q
LD a,101
call q
LD a,32
call q
LD a,71
call q
LD a,111
call q
LD a, 108
call q
LD a,102
jp qHexdump of the executable:
3E 43 CD 5A BB 3E 6F CD 5A BB 3E 64 CD 5A BB 3E 65 CD 5A BB 3E 20 CD 5A BB 3E 47 CD 5A BB 3E 6F CD 5A BB 3E 6C CD 5A BB 3E 66 C3 5A BB
Total: 45 bytes.
With Literals
Zen C allows string literals to be evaluated as statements to print them to standard output. By appending the .. operator, the implicit trailing newline is suppressed, emitting exactly the 9 required characters.
fn main(){"Code Golf"..}Length: 24 bytes.
Without Literals
This version assumes the compiled executable is renamed to Code Golf and executed via ./Code Golf. It uses the program's own execution path in argv[0], offsets the pointer by 2 to skip the ./ prefix, and uses the POSIX write function to emit exactly 9 bytes to stdout (file descriptor 1). Omitting the explicit declaration for write triggers a compiler warning but saves bytes.
fn main(argc:int,argv:char**){write(1,argv[0]+2,9)}Length: 51 bytes.
Executable Size
A standard build produces an executable of around 17KB. However, when the binary is stripped and compiled using the Tiny C Compiler (TCC) backend, the footprint can be reduced to just 8.6KB.
zig build-exe code_golf.zig -femit-bin=code_golf -OReleaseSmall -fsingle-threaded
File Size: 64B. Exe Size: 2,856KB
pub fn main()void{@import("std").debug.print("Code Golf",.{});}File Size: 99B. Exe Size: 2,950KB.
pub fn main()void{for([_]u8{37,9,2,3,70,33,9,10,0})|c|@import("std").debug.print("{c}",.{c^102});}- Output:
Code Golf