11
\$\begingroup\$

Write a quine, that when run, creates itself in the current directory, in a source file named itself. We will be using Windows in this, so the file name (and therefore quine) must have the following limitations:

  • None of these characters \ / : ? * < > |
  • Fewer than 211 255 characters

Limitations and assumptions

  • Your code must be a full program (It's going to be run after all).

  • No copying from the source file.

  • You can assume that there isn't another file with your Quine as its name (as it would produce Quine (1)).

  • Errors are allowed (as long as they don't get into the source code

This is , shortest code in bytes wins!

Edit

Maybe I wasn't clear, the file with the name of the quine has to actually contain the quine. My bad.

\$\endgroup\$
8
  • \$\begingroup\$ What are the limitations on non-ASCII characters in the program? (On Windows, this might quite possibly depend on what language version of Windows is installed; I forget the exact rules and whether they've changed over time.) \$\endgroup\$
    – user62131
    Feb 7, 2017 at 15:11
  • \$\begingroup\$ Why fewer than 211 bytes? That's kind of an arbitrary restriction... \$\endgroup\$
    – mbomb007
    Feb 7, 2017 at 19:19
  • \$\begingroup\$ @mbomb007 Max filename size, I believe. \$\endgroup\$ Feb 7, 2017 at 19:20
  • 1
    \$\begingroup\$ @VoteToClose It's 255. stackoverflow.com/a/265782/2415524 \$\endgroup\$
    – mbomb007
    Feb 7, 2017 at 19:22
  • 1
    \$\begingroup\$ I don't know if that have changed but Windows 10 don't accept double quote " in file name \$\endgroup\$
    – Jakque
    Jun 10, 2021 at 10:41

5 Answers 5

6
\$\begingroup\$

Node.js, 56 52 bytes

function f(){require('fs').writeFile(f+='f()',f)}f()

This prints the warning

DeprecationWarning: Calling an asynchronous function without callback is deprecated.

If you want the all-green, it costs 4 bytes to change writeFile to writeFileSync.

\$\endgroup\$
6
\$\begingroup\$

Vitsy, 10 27 bytes

'rddd++&"rdd8++a[v}v1-D);].

If : was allowed, I could shorten this to 11 bytes. :(

Explanation:

'rddd++&"rdd8++a[v}v1-D);].
'                           Capture all instructions as a string until encountering
                              ' again, looping if necessary.
 r                          Reverse the current stack (output is top-down).
  ddd++                     Push char literal ' to the stack.
       &                    Push a new stack to the stack stack.
        "                   Same as ', but " specific.
         r                  Reverse the current stack.
          dd8++             Push char literal " to the stack.
               a            Push 10 to the stack.
                [        ]  Loop forever.
                 v          Capture the top value as a variable.
                  }         Take the bottom item of the stack and put it on the top.
                   v        Dump the variable to the stack.
                    1-      Subtract 1.
                      D     Duplicate the top item.
                       );   Pop n; if n is 0, exit the loop.
               a[v v1-D);]  This is a makeshift for loop with 10 iterations.
                          . Pop the top stack as n, and the second stack as o. 
                              Write a file called "n" with the contents of "o".

The two stacks will be identical at the end. On some Java versions, this may throw a FileNotFoundException due to different implementations of the FileInputStream class.

Previous answer before edit:

&'rddd++}.

Explanation:

&           Push a new stack to the stack stack.
 'rddd++    Modified standard quine.
        }   Move the ' to the right place.
         .  Pop the top stack as n, and the second stack as o. Write a file called
                 "n" with the contents of "o". (Quine name, no content)

Because I might as well, here's a diagram of the steps that occur within the stacks (each stack is represented by []):

Initial state: [[]]

&              [[], []]
                  Push a new stack to the stack stack.

 '             [[], ["r", "d", "d", "d", "+", "+", "}", ".", "&"]]
                  This stack state occurs because ' will loop around the line
                    until finding the next '.

  r            [[], ["&", ".", "}", "+", "+", "d", "d", "d", "r"]]
                  Reverses the top (last) stack.

   ddd         [[], ["&", ".", "}", "+", "+", "d", "d", "d", "r", 13, 13, 13]
                  Push thirteen thrice.

      ++       [[], ["&", ".", "}", "+", "+", "d", "d", "d", "r", "'"]
                  Because char ' is 39 = 13 + 13 + 13.

        }      [[], [".", "}", "+", "+", "d", "d", "d", "r", "'", "&"]]
                  Takes the bottom item, then puts it on the top.

         .     []
                  Writes a file with content from the second-to-top stack and the
                    name as the concatenation of all elements in the top stack,
                    with top member priority (backwards from my representation).

I'm actually not sure how this works right; minute changes cause this to break. For example, placing the & in various other locations should work just as well, but it causes ClassCastExceptions, IOExceptions, and ArrayOutOfBoundsExceptions depending on where you put it. I might need to do some bugfixing.

\$\endgroup\$
10
  • 2
    \$\begingroup\$ +1 for Push a new stack to the stack stack. \$\endgroup\$
    – Pavel
    Feb 7, 2017 at 20:46
  • 2
    \$\begingroup\$ +1 from me if you can push a new stack stack to a stack stack stack, otherwise forget it! ... Kidding +1, I laughed too. \$\endgroup\$ Feb 7, 2017 at 22:04
  • 2
    \$\begingroup\$ @carusocomputing What if I told you I can with o? ;) \$\endgroup\$ Feb 7, 2017 at 22:04
  • \$\begingroup\$ Sigh unzip... \$\endgroup\$ Feb 7, 2017 at 22:07
  • 1
    \$\begingroup\$ Uhm... I think that the generated file is supposed to contain the code (i.e. its content should be equal to its filename) \$\endgroup\$
    – Leo
    Feb 7, 2017 at 22:41
2
\$\begingroup\$

Lua, 96 bytes.

s="s=%qs=string.format(s,s)f=io.open(s)f.write(f,s)"s=string.format(s,s)f=io.open(s)f.write(f,s)

Typed this on my phone, should work, but I'll test it when I get to a computer.

\$\endgroup\$
3
  • 12
    \$\begingroup\$ I think at the very least you'll need to change sting.format to string.format twice. \$\endgroup\$ Feb 7, 2017 at 8:28
  • \$\begingroup\$ @PeterTaylor you don't like Sting, do you? \$\endgroup\$
    – KeyWeeUsr
    Feb 7, 2017 at 14:08
  • 5
    \$\begingroup\$ @KeyWeeUsr, I have nothing against him personally, and in fact he helped pay for my upbringing, but I think that the Lua standard library was probably designed for general usability by people familiar with similar APIs rather than specifically for Police fans. \$\endgroup\$ Feb 7, 2017 at 14:54
2
\$\begingroup\$

C, 134 bytes

s[255];fd;char p[255]="s[255];fd;char p[255]=%c%s%c;main(){sprintf(s,p,34,p,34);creat(s,0);}";main(){sprintf(s,p,34,p,34);creat(s,0);}
\$\endgroup\$
2
  • \$\begingroup\$ The source here contains * so it doesn't look like it's allowed according to the rules. \$\endgroup\$
    – Value Ink
    May 23, 2017 at 22:57
  • \$\begingroup\$ @ValueInk Fixed. Thanks for pointing that out! \$\endgroup\$
    – MD XF
    May 23, 2017 at 23:10
2
\$\begingroup\$

Python 3, 51 bytes

a="s='a=%r;exec(a)'%a;open(s,'w').write(s)";exec(a)

Try it online!

Python 3, 87 79 71 62 bytes

Since windows is not allowing double quote " anymore (more infos), I made an other solution verifying the new convention:

w='w=%r;open(w%%w,w[0]).write(w%%w)';open(w%w,w[0]).write(w%w)

Try it online!

\$\endgroup\$

Your Answer

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

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