Code Golf: Code Golf: Difference between revisions
Content deleted Content added
Tag: Manual revert |
|||
(47 intermediate revisions by 24 users not shown) | |||
Line 13:
Without string literals, this is 60 bytes long.
<syntaxhighlight lang="11l">L(c)[37,9,2,3,70,33,9,10,0]{print(end' Char(code' c(+)102))}</syntaxhighlight>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* 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
</syntaxhighlight>
{{Out}}
<pre>
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 $
</pre>
=={{header|Ada}}==
With String literal:
<syntaxhighlight lang="ada">
with Ada.Text_IO;procedure C is begin Ada.Text_IO.Put("Code Golf");end;
</syntaxhighlight>
71 characters
Without Character or String literals (all one line):
<syntaxhighlight lang="ada">
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;
</syntaxhighlight>
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):
<syntaxhighlight lang="ada">
with Ada.Command_Line;with Ada.Text_IO;procedure C is begin Ada.Text_IO.Put(Ada.Command_Line.Command_Name(3..11));end;
</syntaxhighlight>
118 Characters
=={{header|6502 Assembly}}==
Line 74 ⟶ 132:
{{out}}
<pre>Code Golf</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* 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
</syntaxhighlight>
{{Out}}
<pre>
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 $
</pre>
=={{header|Arturo}}==
Line 102 ⟶ 199:
This should work with POSIX-compliant implementations (support for hex literals is not mandatory), in double-precision floating-point arithmetic.
=={{header|
{{works with|UNIX_Shell}}
The directly executable source code is 14 bytes by using its script name instead of character literals:
<syntaxhighlight lang="sh">echo -n ${0:2}</syntaxhighlight>
To run:
<syntaxhighlight lang="sh">./Code\ Golf</syntaxhighlight>
=={{header|BASIC}}==
==={{header|BASIC256}}===
With a quoted string, the following weighs in at 12 bytes.
<syntaxhighlight lang="freebasic">?"Code Golf"</syntaxhighlight>
Line 113 ⟶ 218:
Note: BASIC256 is an interpreter, it does not generate executables.
==={{header|SmallBASIC}}===
12 characters with a quoted string:
<syntaxhighlight lang="qbasic">?"Code Golf"</syntaxhighlight>
49 characters without quoted literals:
<syntaxhighlight lang="qbasic">FOR i in [44,0,11,10,79,40,0,3,9] DO ?CHR(111-i);</syntaxhighlight>
=={{header|Binary Lambda Calculus}}==
shortest: 10 bytes
<pre>*Code Golf</pre>
avoiding ASCII: 23 bytes
<pre>46 60 17 ac 23 40 b0 02 cf f7 97 f7 ee 80 bc 90 9b 9a df b8 90 93 99</pre>
=={{header|BQN}}==
Line 119 ⟶ 237:
Without quoted literals:
<syntaxhighlight lang="bqn">•Out@+111-44‿0‿11‿10‿79‿40‿0‿3‿9</syntaxhighlight>
=={{header|C}}==
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:
<syntaxhighlight lang="c">main(){printf("Code Golf");}</syntaxhighlight>
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:
<syntaxhighlight lang="c">a[]={0x65646f43,0x6c6f4720,102};main(){printf(a);}</syntaxhighlight>
The size of the executable needed is now 15,992 bytes.
Output in both cases:
<pre>
Code Golf
</pre>
=={{header|dc}}==
Line 125 ⟶ 263:
Without quoted literals (22 characters):
<syntaxhighlight lang="dc">16i436F646520476F6C66P</syntaxhighlight>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|}}
Delphi 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.
<syntaxhighlight lang="Delphi">
program Project1;
{$APPTYPE CONSOLE}
begin
WriteLn('code golf');
WriteLn(#67,#111,#100,#101,#32,#71,#111,#108,#102);
end.
</syntaxhighlight>
{{out}}
<pre>
Code Golf
Code Golf
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
# 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).
for i in [44 0 11 10 79 40 0 3 9]write strchar(111-i).
</syntaxhighlight>
=={{header|ed}}==
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]].
<syntaxhighlight lang="sed">
# by Artyom Bologov
a
Code Golf
.
p
Q
</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
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))
</syntaxhighlight>
{{out}}
<pre>
Code GolfCode Golf
</pre>
=={{header|Factor}}==
Line 155 ⟶ 336:
{{out}}<pre>Code Golf</pre>
=={{header|FutureBasic}}==
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.
<syntaxhighlight lang="futurebasic">
stop"Code Golf"
</syntaxhighlight>
Without literals, I poked two values into a string, then printed it with the '''stop''' function: 50 chars.
<syntaxhighlight lang="futurebasic">
str15 s:~@s,0x6F472065646F4309:%@s+8,0x666C:stop s
</syntaxhighlight>
Both produce this result:
{{out}}
[[File:Code Golf.png]]
=={{header|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.
Line 205 ⟶ 398:
(Note: if we were careful about the current directory we were in when we executed this program, we could eliminate the part that swaps <code>-</code> and <code>/</code> 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)...
=={{header|Joy}}==
By using a string literal (20 characters):
<syntaxhighlight lang="Joy">"Code Golf"putchars.</syntaxhighlight>
Without string literals (48 characters):
<syntaxhighlight lang="Joy">[35 79 68 69 0 39 79 76 70][32 + chr putch]step.</syntaxhighlight>
=={{header|jq}}==
Line 220 ⟶ 421:
'''Extra credit''':
The jq executable on my Mac is 461,864 bytes; gojq's is over 8 times larger.
=={{header|Julia}}==
<syntaxhighlight lang="julia">print("Code Golf")
print(String(Char.([67,111,100,101,32,71,111,108,102])))
</syntaxhighlight>
=={{header|Kotlin}}==
Line 228 ⟶ 435:
Without string literals, platform-independent:
<syntaxhighlight lang="kotlin">fun main()=print(byteArrayOf(67,111,100,101,32,71,111,108,102).decodeToString())</syntaxhighlight>
A shorter but hacky version:
<syntaxhighlight lang="kotlin">fun main(){listOf(35,79,68,69,0,39,79,76,70).map{print(' '+it)}}</syntaxhighlight>
=={{header|Ksh}}==
Line 237 ⟶ 446:
Or, in case format strings are allowed for conversion (36 bytes):
<syntaxhighlight lang="sh">printf %..39d%5..43d 2291147 3384588</syntaxhighlight>
=={{header|Lang}}==
With text literal (19 Bytes):
<syntaxhighlight lang="lang">fn.print(Code Golf)</syntaxhighlight>
Without text literal (88 Bytes):
<syntaxhighlight lang="lang">parser.op(print(join(\e, arrayMapToNew([67,111,100,101,32,71,111,108,102], fn.toChar))))</syntaxhighlight>
=={{header|Lua}}==
Line 256 ⟶ 471:
Without string literals (52 characters):
<syntaxhighlight lang="min">(35 79 68 69 0 39 79 76 70) (32+ chr putchr) foreach</syntaxhighlight>
=={{header|Nim}}==
Using a string literal (24 characters):
<syntaxhighlight lang="Nim">stdout.write "Code Golf"
</syntaxhighlight>
Compiling on Linux with Nim 1.6.12 using command <code>nim c -d:danger --opt:size -d:lto --gc:arc code_golf.nim</code>, the executable size is 23584 bytes.
Without string literals (61 characters):
<syntaxhighlight lang="Nim">for n in[67,111,100,101,32,71,111,108,102]:stdout.write n.chr
</syntaxhighlight>
Compiling on Linux with Nim 1.6.12 using command <code>nim c -d:danger --opt:size -d:lto --gc:arc code_golf.nim</code>, the executable size is 22528 bytes.
=={{header|OCaml}}==
Line 286 ⟶ 515:
# 1 2 3 4 5
#12345678901234567890123456789012345678901234567890
print
=={{header|Phix}}==
<!--
<syntaxhighlight lang="Phix">
puts(1,"Code Golf")
</syntaxhighlight>
Which is 19 bytes. Note that <code>?"Code Golf"</code>, while only 12 bytes, ''does'' print the quotation marks and therefore does not meet the task specifications.<br>
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:
<syntaxhighlight lang="Phix">
puts(1,atom_to_float64(1.276409856e-152)[4..$]&
atom_to_float64(1.458406353e-258)[4..$])
</syntaxhighlight>
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:
<syntaxhighlight lang="Phix">
puts(1,get_file_base(command_line()[2]))
</syntaxhighlight>
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),
Line 314 ⟶ 553:
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.
=={{header|Phixmonti}}==
Line 444 ⟶ 682:
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
=={{header|Ruby}}==
Line 488 ⟶ 726:
By using just a format specifier (56 characters):
<syntaxhighlight lang="sh">printf $(printf \\\\%o 67 111 100 101 32 71 111 108 102)</syntaxhighlight>
=={{header|Uxntal}}==
Using a "string literal" / raw ASCII rune (59 characters):
<syntaxhighlight lang="Uxntal">|0100 #0110 2194 06 #18 1720 fff7 #800f 1700 "Code 20 "Golf</syntaxhighlight>
Without raw ASCII runes (67 characters):
<syntaxhighlight lang="Uxntal">|0100 #0110 2194 06 #18 1720 fff7 #800f 1700 436f 6465 2047 6f6c 66</syntaxhighlight>
Both programs assemble to the same 26 byte ROM:
<pre>a001 1021 9406 8018 1720 fff7 a080 0f17
0043 6f64 6520 476f 6c66</pre>
The 9 byte string accounts for almost 35% of the final ROM.
The golfed code works very similarly to the following code:
<syntaxhighlight lang="Uxntal">|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
|0100
( 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 )
#80 .System/state DEO
BRK
@str
"Code 20 "Golf 00</syntaxhighlight>
The main trick used in golfing of this program was to encode the raw instruction bytes in hex in the source code.
=={{header|Verilog}}==
Line 521 ⟶ 792:
=={{header|Wren}}==
The shortest possible program (25 bytes) to print the required string is:
<syntaxhighlight 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 (68 bytes) is:
<syntaxhighlight lang="
{{out}}
In both cases:
Line 565 ⟶ 836:
With a quoted string, the following weighs in at 12 bytes.
<syntaxhighlight lang="yabasic">?"Code Golf"</syntaxhighlight>
Without quoted literals, this is
<syntaxhighlight lang="yabasic">data 44,0,11,10,79,40,0,3,9:for i=
Note: Yabasic is an interpreter, it does not generate executables.
|