243
\$\begingroup\$

Using your language of choice, golf a quine.

A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

No cheating -- that means that you can't just read the source file and print it. Also, in many languages, an empty file is also a quine: that isn't considered a legit quine either.

No error quines -- there is already a separate challenge for error quines.

Points for:

  • Smallest code (in bytes)
  • Most obfuscated/obscure solution
  • Using esoteric/obscure languages
  • Successfully using languages that are difficult to golf in

The following Stack Snippet can be used to get a quick view of the current score in each language, and thus to know which languages have existing answers and what sort of target you have to beat:

var QUESTION_ID=69;
var OVERRIDE_USER=98;

var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(index){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}
function commentUrl(index,answers){return"https://api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}
function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=!1;comment_page=1;getComments()}})}
function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}
getAnswers();var SCORE_REG=(function(){var headerTag=String.raw `h\d`
var score=String.raw `\-?\d+\.?\d*`
var normalText=String.raw `[^\n<>]*`
var strikethrough=String.raw `<s>${normalText}</s>|<strike>${normalText}</strike>|<del>${normalText}</del>`
var noDigitText=String.raw `[^\n\d<>]*`
var htmlTag=String.raw `<[^\n<>]+>`
return new RegExp(String.raw `<${headerTag}>`+String.raw `\s*([^\n,]*[^\s,]),.*?`+String.raw `(${score})`+String.raw `(?=`+String.raw `${noDigitText}`+String.raw `(?:(?:${strikethrough}|${htmlTag})${noDigitText})*`+String.raw `</${headerTag}>`+String.raw `)`)})();var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}
function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))
body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)
valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,})});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)
lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<i>'+a.language+'</i>').text().toLowerCase();languages[lang]=languages[lang]||{lang:a.language,user:a.user,size:a.size,link:a.link,uniq:lang}});var langs=[];for(var lang in languages)
if(languages.hasOwnProperty(lang))
langs.push(languages[lang]);langs.sort(function(a,b){if(a.uniq>b.uniq)return 1;if(a.uniq<b.uniq)return-1;return 0});for(var i=0;i<langs.length;++i)
{var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

\$\endgroup\$
3
  • 6
    \$\begingroup\$ Do you not mean, "Golf you a quine for greater good!"? \$\endgroup\$ May 3, 2011 at 2:49
  • 64
    \$\begingroup\$ @muntoo it's a play on "Learn you a Haskell for Great Good". \$\endgroup\$ May 3, 2011 at 2:52
  • 26
    \$\begingroup\$ Did anybody notice that this is question 69? \$\endgroup\$
    – aidan0626
    Oct 24, 2020 at 22:47

453 Answers 453

1
10 11
12
13 14
16
2
\$\begingroup\$

Assembly (NASM, 32-bit, Linux), 620 bytes

section .text
mov di,1
mov ebx,1
m:mov esi,a
l:mov ecx,esi
mov edx,1
mov al,[esi]
add ax,di
cmp al,10
jne p
mov ecx,b
mov dx,2
p:mov eax,4
int 128
mov al,[esi]
inc esi
cmp al,96
jne l
dec di
jz m
mov ecx,c
mov dx,3
mov ax,4
int 128
mov ax,1
int 128
section .data
c db 44,57,54
b db 92,110
a db`section .text\nmov di,1\nmov ebx,1\nm:mov esi,a\nl:mov ecx,esi\nmov edx,1\nmov al,[esi]\nadd ax,di\ncmp al,10\njne p\nmov ecx,b\nmov dx,2\np:mov eax,4\nint 128\nmov dl,[esi]\ninc esi\ncmp dl,34\njne l\ndec di\njz m\nmov ecx,c\nmov dx,3\nmov ax,4\nint 128\nmov ax,1\nint 128\nsection .data\nc db 44,57,54\nb db 92,110\na db`,96

-149 bytes by using backqoutes

-26 bytes by simplifying jumps

Try it online!

\$\endgroup\$
1
2
\$\begingroup\$

Swift 5, 202 bytes

While much longer than the shortest Swift submission, this has important aesthetic points for me since it avoids numerical ASCII references as well as any declarations at all, using an anonymous function, resulting in a single-statement solution.

import Foundation;({print($0+"("+String(data:try!JSONEncoder().encode($0),encoding:.utf8)!+")")})("import Foundation;({print($0+\"(\"+String(data:try!JSONEncoder().encode($0),encoding:.utf8)!+\")\")})")
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to the site! \$\endgroup\$ Nov 13, 2020 at 4:38
2
\$\begingroup\$

Setanta, 254 bytes

s:="s:=?q:='\"'n:=cuid@s(0,9)o:=cuid@s(9,fad@s)scriobh(athchuir@n(\"?\",q+athchuir@(athchuir@s(\"\\\\\",\"\\\\\\\\\"))('\"','\\\\\"')+q)+o)"q:='"'n:=cuid@s(0,9)o:=cuid@s(9,fad@s)scriobh(athchuir@n("?",q+athchuir@(athchuir@s("\\","\\\\"))('"','\\"')+q)+o)

Try it here!

This was pretty long, mainly because Setanta doesn't have any string formatting builtins. Probably could still be improved.

Edit 2021/2/4: It seems that you now need to spell the function to print a value as scríobh (previously, you could use scriobh without the accent on the i). Making that fix adds 2 bytes to the solution.

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 24 bytes

print<<""x2
print<<""x2

24 bytes without any options.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 38 bytes

$_=q{$_=q{0};s/0/$_/;say};s/0/$_/;say

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Adjust, 21 bytes

`@96c$+.+O`@96c$+.+O

Try it online!

Basically just a port of the Adapt quine, but updated with Adjust's swap command ($ vs Adapt's s) and no longer implicit output

\$\endgroup\$
2
\$\begingroup\$

AWK, 56 bytes

BEGIN{printf a="BEGIN{printf a=%c%s%c,34,a,34}",34,a,34}

Try it online!

Outgolfed these two.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 48 bytes

s='print(f"s={s!r};{s}")';print(f"s={s!r};{s}")

Try it online!

Python 3, 74 bytes

s=r"s=;print(f'{s[:2]}r\"{s}\"{s[2:]}')";print(f'{s[:2]}r\"{s}\"{s[2:]}')

Try it online!

First two attempts... I think this is the basic approach, just used Python's f-strings.

\$\endgroup\$
2
\$\begingroup\$

Pxem Pxem (esolang-box notation), 162 113 109 bytes.

Eventually as in meta discussion I had to change the language. The following program equals to filename with same name and empty content in original notation.

Be!?BzBc!!B+BzXXFBaBcFBzBsXXFBBaBsBtBvBmBvBcAcB-BaBsBp".e!?.z.c!!.+.zXXF.a.cF.z.sXXF..a.s.t.v.m.v.cAc.-.a.s.p

Try it online!

With comments

XX.z
# represents programs from first .m
# every dot has to be replaced with B, for example,
# because dots are syntaxically special
# append " to it
.aXX(long)".z
# essentially double entire stack
.a.eXX.z
# loop begin
.a!?.zXX.z
  # move every character to bottom
  # HOWEVER B is replaced with DOT
  # when exiting from loop, the stack will be:
  # "(original data)"(modified data)
  #
  # push dummy F if B is not top
  .a.c!!.+.zXXF.aXX.z
  # replace B with dot, and push F if F is not top
  .a.cF.z.sXXF..aXX.z
  # remove dummy
  .a.sXX.z
  # move to bottom
  .a.t.v.m.vXX.z
# get out if "
.a.cAc.-.aXX.z
# finally; discard top
.a.s.p

Previous version

  • 113 bytes: Bm!?BzBcBoBc!!B+BzXXFBaBcFBzBsXXFBBaBsBtBvBmBvBcAcB-BaBp".m!?.z.c.o.c!!.+.zXXF.a.cF.z.sXXF..a.s.t.v.m.v.cAc.-.a.p
  • 162 bytes: XXx\è\ô\Ú\î\Ú\Þ\Ú\Þ\Ú@\V\Þ``\Z\Â\Æ\Æ\Þ\è\ì\Ú\ì``\Z\è¢\Â\ì\æ\ì°\î\Æ\Æ\ô\È\Â\H\Þ°\ÂQ.t.z.m.w.m.o.m.o.m .+.o00.-.a.c.c.o.t.v.m.v00.-.tQ.a.v.s.vX.w.c.c.z.d.a.$.oX.a
\$\endgroup\$
7
  • \$\begingroup\$ This isn't a quine. It's a program that outputs its own file name. A quine would require the filename to not be important; which I'm not sure is possible in Pxem. This is like submitting a 35 byte Python solution with code exec(__file__)\n with filename print'exec(__file__)': TIO. Even though this is not in direct violation of any rules here, it clearly breaks their spirit. But what you're doing here is against the rules. \$\endgroup\$
    – Makonede
    May 29, 2021 at 21:35
  • \$\begingroup\$ @Makonede but Pxem matters filename first and content is matter if and only if .f and .e are in filename; would I just undo frilom 109-byte version to 113-byte version to remove .e? Or should have I just clarify that a Pxem program consists of filename and optionally its content? In this case I would claim that its filename is the only source code; then I would satisfy the definition of quine: produces a copy of its own source code. \$\endgroup\$
    – user100411
    May 29, 2021 at 23:32
  • \$\begingroup\$ A Pxem program may run based on filename, but that does not automatically make it need to output its code instead of its content. A quine is still a quine, no matter how a language is implemented. Remember that you can't just make it output its content, by the way. A quine has to have part of its code that encodes the entire program. And even though I originally thought this isn't breaking rules, I'm 99.9% sure this is a rule somewhere, I just can't describe it in search-friendly keywords. I'll ask TNB about this answer for their opinion, I'm highly doubtful that this is a valid answer. \$\endgroup\$
    – Makonede
    May 30, 2021 at 3:18
  • \$\begingroup\$ @Makonede; the programs output their filenames; not content. \$\endgroup\$
    – user100411
    May 30, 2021 at 3:26
  • \$\begingroup\$ Or should the 109-bytes version be represented in plain-text notation, such as in esolang-box or nk.'s text2pxem.pl? \$\endgroup\$
    – user100411
    May 30, 2021 at 22:18
2
\$\begingroup\$

Agda, 160 bytes

open import IO;open import Data.String;main = run(putStr(r ++ show r))where r ="open import IO;open import Data.String;main = run(putStr(r ++ show r))where r ="

Try it online!

Agda is an interesting Haskell-like language with some interesting ideas and a lot of imports. Luckily I've managed to avoid most of those (though my early attempts weren't so successful). The version on TIO is a bit out of date, which led to some frustration with the docs, but I think this code should still work with the latest version.

Explanation:

open import IO           -- Import the IO library for `run` and `putStr`
open import Data.String  -- Import Data.String for `++` and `show`
main = run(putStr        -- with main, print the string
       (r ++ show r))    -- r appended with the string representation of r
       where r =         -- where r is
                 "..."   -- the string of the rest of the program
\$\endgroup\$
2
\$\begingroup\$

OCaml, 66 bytes

(fun x->Printf.printf"%s%S"x x)"(fun x->Printf.printf\"%s%S\"x x)"

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JsonLogic, 631

{"reduce":[[1],{"cat":[{"reduce":[{"var":"accumulator"},{"cat":[{"var":"accumulator"},{"substr":["\\\",:[]1abcdelmnorstuv{}",{"var":"current"},1]}]}]},{"var":"accumulator"},"]]}"]},[21,1,16,11,10,19,9,11,1,3,4,4,6,5,2,21,1,9,7,18,1,3,4,21,1,16,11,10,19,9,11,1,3,4,21,1,20,7,16,1,3,1,7,9,9,19,13,19,12,7,18,15,16,1,22,2,21,1,9,7,18,1,3,4,21,1,20,7,16,1,3,1,7,9,9,19,13,19,12,7,18,15,16,1,22,2,21,1,17,19,8,17,18,16,1,3,4,1,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,1,2,21,1,20,7,16,1,3,1,9,19,16,16,11,14,18,1,22,2,6,5,22,5,22,5,22,2,21,1,20,7,16,1,3,1,7,9,9,19,13,19,12,7,18,15,16,1,22,2,1,5,5,22,1,5,22,2,4]]}

Try it online by pasting it under "Rule" here. (Note: the "Output" section is JSON-encoded. The raw unquoted string is logged in the console which can be viewed with F12)

\$\endgroup\$
2
\$\begingroup\$

ErrLess, 34 24 bytes

Thanks to Jo King for saving 10 bytes

S'R1+,$:@:?.S'R1+,$:@:?.

No trailing newline.

Explanation

S'R1+,$:@:?.S { Put the rest of the program in a string: ("'R1+,$:@:?.") }
'R1+,         { Add a one-element string containing `S`: ("'R1+,$:@:?." "S") }
$:            { Swap & concatenate: ("S'R1+,$:@:?.") }
@:            { Duplicate & concatenate: ("S'R1+,$:@:?.S'R1+,$:@:?.") }
?.            { Print & halt }

ErrLess is a stack-based language I made for fun over the last few months. You can read the docs here, and I also started a tutorial.

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

OIL, 77 bytes

Quines are one of the things that aren't that hard in OIL, because of the whole "unified memory" thing. The quine exists since a long time, but I hadn't posted it here yet.

The following code needs to be stripped of the comments to work:

0  # do nothing, these two fields are just placeholders
0
1  # copy the line above to itself
1
1
4  # output line 1
1
11 # and a newline
4  # and output line 1 again
1
11 # and a newline (now we printed 0 and 0)
1  # copy line 2 to line 2
2  # *
2
1  # copy
12 # line 12 (the one marked with the star)
18 # to line 18 (marked with $)
10 # ^, if the next line (currently 2) is equal to the line afterwards (1)
18 # $
1
32 # jump to line 32 (essentially quitting, because it's out of bounds)
22 # otherwise to the next line:
1  # copy from 18 ($) to 26 (&)
18
26
4  # output from the cell with the number in the next line
26 # &
8  # increment line 18 ($)
18
11 # newline
6  # jump to line 17 (^, beginning of the loop)
17
\$\endgroup\$
2
\$\begingroup\$

Scratch, 542 bytes

when gf clicked
set[s v]to[when gf clicked/&set[s v]to[/&set[i v]to(0/&set[q v]to[/&repeat(length of(s/&change[i v]by(1/&if<<(letter(i)of(s))=[/]>and<(letter((i)+(1))of(s))=[&]>>then/&say(q/&set[q v]to[/&change[i v]by(1/&else/&set[q v]to(join(q)(letter(i)of(s/&end/&if<(i)=[28]>then/&set[q v]to(join(q)(s/&
set[i v]to(0
set[q v]to[
repeat(length of(s
change[i v]by(1
if<<(letter(i)of(s))=[/]>and<(letter((i)+(1))of(s))=[&]>>then
say(q
set[q v]to[
change[i v]by(1
else
set[q v]to(join(q)(letter(i)of(s
end
if<(i)=[28]>then
set[q v]to(join(q)(s

Try it on Scratch! (link is to a project that uses lists as output for better readability.)

This uses a classic method of storing the data in a container and uses it to make a quine. Because Scratch doesn't allow to initialize a list in just a single block, I have to use a variable of string and encode /& for a newline.

Explanation:

when gf clicked // when green flag is clicked (the start of the program)
set[s v]to[when gf clicked/&set[s v]to[/&set[i v]to(0/&set[q v]to[/&repeat(length of(s/&change[i v]by(1/&if<<(letter(i)of(s))=[/]>and<(letter((i)+(1))of(s))=[&]>>then/&say(q/&set[q v]to[/&change[i v]by(1/&else/&set[q v]to(join(q)(letter(i)of(s/&end/&if<(i)=[28]>then/&set[q v]to(join(q)(s/&
// the above variable `s` is used to store the content of the whole code, apart from the content of itself, which we'll try to tackle using the below code.
set[i v]to(0 // set the counter to 0
set[q v]to[ // this is like a buffer for what we'll say on a line
repeat(length of(s //repeat for the length of s
change[i v]by(1 // increment i by one
if<<(letter(i)of(s))=[/]>and<(letter((i)+(1))of(s))=[&]>>then // if we hit the newline
say(q // then say what is in the buffer
set[q v]to[ // clear the buffer
change[i v]by(1 // and increment i, to avoid parsing &
else /// else
set[q v]to(join(q)(letter(i)of(s //add the letter to the buffer
end // end the if-else
if<(i)=[28]>then //if we hit the 28th character, which is right after the initializion part of the variable s
set[q v]to(join(q)(s // then we add the whole content of s onto the buffer
\$\endgroup\$
6
  • \$\begingroup\$ Instead of using /&, why not use // and do <(letter(i)of(s))=(letter((i)+(1))of(s))>? \$\endgroup\$ Jun 29, 2023 at 11:08
  • \$\begingroup\$ @UndoneStudios Unfortunately letter have double t, so when the code runs it treats the double t as a newline. \$\endgroup\$
    – badatgolf
    Jun 30, 2023 at 2:05
  • \$\begingroup\$ What about using a single character that the code doesn't have (like $?) \$\endgroup\$ Jun 30, 2023 at 10:52
  • \$\begingroup\$ @UndoneStudios you also can't, because it will mistake the $ in <(letter(i)of(s))=[$]> in s as a newline. \$\endgroup\$
    – badatgolf
    Jul 1, 2023 at 2:49
  • \$\begingroup\$ We could add another condition and<not<(i)=(whatever position this $ is)>>. Probably that would work? \$\endgroup\$ Jul 3, 2023 at 10:34
2
\$\begingroup\$

RASEL, 11 bytes

<@,Yj5#?:,"
\$\endgroup\$
2
\$\begingroup\$

Thunno, \$26\log_{256}(95)\$ ≈ 21.35203072707557

bytes

"D34CXx+xs+s+"D34CXx+xs+s+

Attempt This Online!

It's one of those ones which has one of itself.

Explained

"..."D34CXx+xs+s+
"..."              # Push a literal string 
     D             # Duplicate that string
      34CX         # X = chr(34)
          x+       # append " to the string
            xs+    # prepend " to the string
               s+  # prepend that to the original literal string
\$\endgroup\$
1
2
\$\begingroup\$

Commodore C64 machine code (6502) using the Kernal (non-competing/just for fun), 42 bytes

From memory location 0x033c, enter the following machine code in your monitor:

>C:033c  a2 00 bd 3c  03 48 4a 4a  4a 4a 20 59  03 68 48 29
>C:034c  0f 20 59 03  68 cd 65 03  f0 0e e8 d0  e5 c9 0a 90
>C:035c  02 69 06 69  30 20 d2 ff  60 01

Now call with g 033c or SYS 828 from the BASIC prompt.

Each byte of the machine code is written to the screen with the CHROUT routine in the Commodore Kernal, so this will likely work with all Commodore 8-bit machines, though it is untested on all but the Commodore C64.

How it works:

The routine starts from the base address at 0x033c, and reads each byte by loading its value into the accumulator; the accumulator is pushed to the stack, and then the value is logically shifted right so to get the high nybble (so d2 becomes 0d), and the high nybble is converted to its PETSCII equivalent (0 - F inclusive) and outputted to the screen.

We pull the value back from the accumulator and then mask out the high nybble, leaving the lower nybble (so a9 will become 09) and again we convert this to its PETSCII equivalent and output it to the screen.

Finally, we check the value of the current memory location of 0x033c offset by the X register (our counter); if it is equal to our terminating byte (in this case is 01, the last byte of the quine) we're done. Otherwise, we increment our X register by 1 and go again.

For some hints, you may disassemble this by using d 033c in your monitor; this will show you the 6502 assembly equivalent to the machine code bytes.

For further guidance and reference, consult C64 Wiki or CodeBase 64.

Commodore C64 Quine

\$\endgroup\$
2
+100
\$\begingroup\$

(,), 8167 7594 7402 7242 Chars

((),()()())(((),(())()),)(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),()()()())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),()()()())(((),(())()),)(((),(())()),)(((),(())()),)(((),(())()),())(((),(())()),())(((),(())()),())(((),(())()),())((()),((()())))((),()()())(()(),(()())(())()(),,,(),,(())(())()())(,,,(()()),(),,()())(,,,(()(),,,(()())())(())())(,,,(()(),,,(()()))(),(),,(()))(,,,(()())())((,,,(()()),(),,()()())(,,,(()(),,,(()())())()()()())(,,,(()()),(),,()())(,,,(()())(),(),,()())(,,,(()()))(,,,(()())(),(),,()())(,,,(()())()()()())(,,,(()(),,,(()()))(),(),,((())))((),(())(),,(()())()),,,,((())))((),()()())((),(())(),,((()))(()()),((())))

Thanks to some golfs (and help from Dadsdy himself) now fits in a link!

I wanted to golf Dadsdy's submission but I couldn't really understand the implementation of the decoder, so I've ended up writing a new submission from scratch.

Edit: reverted one golf because the interpreter changed and broke it; I've managed to nest some print statements however, which ended up in a lower bytecount anyway!

Edit: new golf suggested by Dadsdy cuts 160 more bytes!

Explanation

A typical quine strategy, the first part encodes the second part of the code, and the second part prints the encoded version followed by the unencoded one.

I'm using the variable at address 1 (var1) as a pointer, going through the characters of the quine one by one. var2 will contain the ascii code for an open bracket ((, 40), while closed bracket (), 41) and comma (,, 44) will be printed by adding 1 and 4 respectively to that number. Variables from 3 onward will contain the encoding of the quine.

((),()()())
Set var1 to 3

((),(())())....(((),(())()),())
Encode all the code after this part: 
  (((),(())()),) is an open bracket
  (((),(())()),()) is a closed bracket
  (((),(())()),()()()()) is a comma
this sets each memory address in order to 0, 1, or 4 to match the following code.

((()),((()())))
Set the memory address after the end of the data to -1

((),()()())
Reset var1 to 3

(()(),(()())(())()(),,,(),,(())(())()())
Set var2 to 40 (5*8)

(,,,(()()),(),,()())(,,,(()(),,,(()())())(())())(,,,(()(),,,(()()))(),(),,(()))(,,,(()())())
Print ((),()()()), the first instruction of the quine which was not included in the encoded part

((,,,(()()),(),,()()())(,,,(()(),,,(()())())()()()())(,,,(()()),(),,()())(,,,(()())(),(),,()())(,,,(()()))(,,,(()())(),(),,()())(,,,(()())()()()())(,,,(()(),,,(()()))(),(),,((())))((),(())(),,(()())()),,,,((())))
For each variable in memory print the corresponding encoding
(this will print the encoded part of the quine)

((),()()())
Reset var1 to 3

((),(())(),,((()))(()()),((())))
For each variable in memory, print the corresponding character    
(this will print the unencoded part of the quine)

text = `((()),((()())))((),()()())(()(),(()())(())()(),,,(),,(())(())()())(,,,(()()),(),,()())(,,,(()(),,,(()())())(())())(,,,(()(),,,(()()))(),(),,(()))(,,,(()())())((,,,(()()),(),,()()())(,,,(()(),,,(()())())()()()())(,,,(()()),(),,()())(,,,(()())(),(),,()())(,,,(()()))(,,,(()())(),(),,()())(,,,(()())()()()())(,,,(()(),,,(()()))(),(),,((())))((),(())(),,(()())()),,,,((())))((),()()())((),(())(),,((()))(()()),((())))`;
text = text.replace("\n","").replace("\r","")
output = "((),()()())";
inc = "((),(())())"
for (let c of text){
  output += c=="(" ? `(${inc},)` : c==")" ? `(${inc},())` : `(${inc},()()()())`;
}

output += text
out1.innerHTML = output.length + " chars"
out2.innerHTML = output
<h1 id=out1></h1>
<p id=out2></p>

\$\endgroup\$
7
  • \$\begingroup\$ I cannot belive I didn't try that encoder. Edit: I did, I just didn't realize I didn't need to set it to anything \$\endgroup\$
    – Dadsdy
    Jun 26, 2023 at 23:32
  • \$\begingroup\$ For -7 Chars each, the ()(),() can be removed. This is because ungiven condition arguments default to -1. \$\endgroup\$
    – Dadsdy
    Jun 26, 2023 at 23:50
  • \$\begingroup\$ Also it turns out outputting the 40 + variable**2 and replacing (((),(())()),()()()()) with (((),(())()),()()) is worth it, as is replacing ((),(())()) with (((),(())()),), getting it down to 7645 chars. codepen.io/Onilily/pen/zYMvjMO \$\endgroup\$
    – Dadsdy
    Jun 27, 2023 at 0:04
  • \$\begingroup\$ I was sure I had tried that first golf, but apparently it got lost somewhere ^^" I'll need some time to understand your second idea though... Thanks! \$\endgroup\$
    – Leo
    Jun 27, 2023 at 3:54
  • \$\begingroup\$ Turns out that after another golf using a new flag, the first golf is no longer more efficient \$\endgroup\$
    – Dadsdy
    Jul 2, 2023 at 16:07
2
\$\begingroup\$

Thue++ 39 Bytes

.+::=~$0\n::=\n$0
::=
.+::=~$0\n::=\n$0
\$\endgroup\$
2
\$\begingroup\$

FALSE, 34 39 bytes

["$91,34,!34,93,!"]$91,34,!34,93,!

Try it online!

This is a "standard" quine program as outlined here: https://esolangs.org/wiki/Quine. What makes this program interesting is its use of FALSE's lambda ([]) command. FALSE doesn't have string literals, so I had to make an anonymous lambda function that prints a string, rather than returning it's value. This can then be executed multiple times by duping it ($).

\$\endgroup\$
2
\$\begingroup\$

ForWhile, 24 bytes

"34#.(,#)~:?"34#.(,#)~:?

prints the string "34#.(,#)~:? twice

online interpreter

Explanation

"34#.(,#)~:?"             \ push the string 34#.(,#)~:?
             34#.         \ print a "
                 (,#)     \ print the string
                     ~:?  \ 1st iteration: go back to the start of the program now with an additional -1 on the stack
                     ~:?  \ 2nd iteration: jump to address 0, terminating the program     
\$\endgroup\$
2
\$\begingroup\$

Thunno 2 j, 6 bytes

"DṘ"DṘ

Try it online!

Explanation

"DṘ"DṘ  # Full program
"DṘ"    # Push the string "DṘ"
    D   # Duplicate it
     Ṙ  # Get its representation
        # (Surround by quotes)
        # j flag joins the stack
\$\endgroup\$
2
  • \$\begingroup\$ You have “explanation” written twice, and maybe that screenshot should be a Try It link now \$\endgroup\$
    – noodle man
    Aug 9, 2023 at 22:51
  • \$\begingroup\$ @noodleman good spot lol \$\endgroup\$
    – The Thonnu
    Aug 10, 2023 at 7:24
2
\$\begingroup\$

Nekomata, 9 bytes

"ᵉĝ,"ᵉĝ,

Attempt This Online!

"ᵉĝ,"ᵉĝ,
"ᵉĝ,"       Push the string "ᵉĝ,"
      ĝ     Wrap the string in quotes
     ᵉ ,    Join it with the original string

Since Nekomata always prints the result with a trailing newline, I have to add a newline to the code to make it a quine.

\$\endgroup\$
2
\$\begingroup\$

Lua, 45 bytes

Simple format approach. We can use %q for conciseness.

s="s=%qprint(s:format(s))"print(s:format(s))
\$\endgroup\$
2
  • \$\begingroup\$ Same as codegolf.stackexchange.com/a/47143 \$\endgroup\$
    – noodle man
    Sep 14, 2023 at 17:34
  • \$\begingroup\$ @noodleman Heh, I had assumed that our quines had to be different given that it ranked higher. Turns out they are probably off by one byte: print prints a newline, so their solution needs a trailing newline, which puts it at 45 bytes. \$\endgroup\$
    – Luatic
    Sep 14, 2023 at 17:56
2
\$\begingroup\$

Python 3, 93 bytes

Note the trailing newline.

f=lambda x:chr(34).join([x,x,'))']);print(f("f=lambda x:chr(34).join([x,x,'))']);print(f("))

Inspired by Vyxal’s I builtin.

This quine isn’t winning any challenges, but it’s still a quine!

Try it online!

\$\endgroup\$
2
\$\begingroup\$

HTML+JS (data url), 142 bytes

data:text/html,<pre id="a"></pre><script>!function f(){a.innerText=`data:text/html,<pre id="a"></pre><script>!`+f+"()</scr"+"ipt>"}()</script>

This requires no "extra environment", just paste it into the browser address bar.

common version, 112 bytes

<pre id="a"></pre><script>!function f(){a.innerText=`<pre id="a"></pre><script>!`+f+"()</scr"+"ipt>"}()</script>

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf! You don't have to include the data:text/html, if you don't want to, a plain HTML answer is fine \$\endgroup\$ Oct 19, 2023 at 16:33
1
\$\begingroup\$

C, 125 84 chars

main(){char*p="main(){char*p=%c%s%c,c='%c',s[256];sprintf(s,p,c,p,c,c);puts(s);}",c='"',s[256];sprintf(s,p,c,p,c,c);puts(s);}

It turns out that my idea was implemented much better:

main(){char*p="main(){char*p=%c%s%c;printf(p,34,p,34,10);}%c";printf(p,34,p,34,10);}
\$\endgroup\$
1
  • 3
    \$\begingroup\$ You could shave 9 chars off the shorter version by leaving out the trailing newline. \$\endgroup\$ Feb 3, 2012 at 18:55
1
\$\begingroup\$

F♯# - 349 Characters

let s="\\\"\nnlet s=let z a b=s.Substring(a,b)System.Console.WriteLine()z 4 6+z 1 1+z 0 1+z 0 1+z 0 1+z 1 1+z 0 1+z 3 1+z 3 1+z 4 169+z 1 1+z 2 1+z 10 26+z 2 1+z 36 25+z 62 111+z 61 1" 
let z a b=s.Substring(a,b)
System.Console.WriteLine(z 4 6+z 1 1+z 0 1+z 0 1+z 0 1+z 1 1+z 0 1+z 3 1+z 3 1+z 4 169+z 1 1+z 2 1+z 10 26+z 2 1+z 36 25+z 62 111+z 61 1)

My first attempt at a quine - probably an easier (or shorter) way to do it, but not a bad first attempt I don't think

\$\endgroup\$
1
\$\begingroup\$

Tcl, 61 chars

set c {set c {$c};puts [subst -noc \$c]};puts [subst -noc $c]
\$\endgroup\$
1
10 11
12
13 14
16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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