12
\$\begingroup\$

Quining has a long history - if you haven't heard of it, click the tag and read up a little on it.

Your Task

Output an infinite series of 1s (with no other spacing [newlines, spaces, etc.]) UNTIL SIGINT (typically CTRL-C) is called. When it is called, output the program source.

Rules

  • It must be a valid quine:
  • You may use any language.
  • As this is a code golf, the shortest answer wins!
\$\endgroup\$
  • 5
    \$\begingroup\$ "Language": Truth Machine Source: 1 \$\endgroup\$ – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Nov 6 '15 at 9:44
  • \$\begingroup\$ @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ That's pretty great, actually. I'm not sure if I'd accept it, because it doesn't output the source at the end, it just stops putting out the source. \$\endgroup\$ – Addison Crump Nov 6 '15 at 9:57
  • 1
    \$\begingroup\$ How do you know the last character is not the program outputting its source? :P (It's a joke answer, anyway) \$\endgroup\$ – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Nov 6 '15 at 10:10
  • 1
    \$\begingroup\$ ¯\_(ツ)_/¯ API, maybe? \$\endgroup\$ – Addison Crump Nov 6 '15 at 10:16
4
\$\begingroup\$

Python 3, 76 bytes

s="while 1:\n try:print(end='1')\n except:-print('s=%r;exec(s)'%s)";exec(s)

Note that the byte count includes a trailing newline. This also uses -print to error out after quining.

\$\endgroup\$
4
\$\begingroup\$

Pyth, 25 bytes

.xf!p1)jN*2]".xf!p1)jN*2]

A modification of the standard Pyth quine to add a try-except function.

\$\endgroup\$
  • \$\begingroup\$ Would using # work for this? It would save 2 bytes. \$\endgroup\$ – Mike Bufardeci Nov 6 '15 at 14:35
  • \$\begingroup\$ @MikeBufardeci I tried that, but couldn't get to work. Give it a shot yourself. \$\endgroup\$ – isaacg Nov 6 '15 at 15:33
3
\$\begingroup\$

AutoIt, 488 429 362 bytes

My brain hurts, this is too meta.

$1=BinaryToString
$2=Chr(34)
$s="FileWrite('a','#include<Misc.au3>'&@LF&'Do'&@LF&'ToolTip(1)'&@LF&'Until _IsPressed(Chr(49)&Chr(66))')+RunWait(@AutoItExe&' a')"
Execute($s)
$x="$1=BinaryToString\n$2=Chr(34)\n%sExecute($s)\n$x=%s\nClipPut(StringFormat($x,$1(0x223D7324)&$s&$1(0x0A0D22),$2&$x&$2))"
ClipPut(StringFormat($x,$1(0x223D7324)&$s&$1(0x0A0D22),$2&$x&$2))

This is quite interesting in the way that it compiles a child-process which in return keeps outputting 1 to the ToolTip API until ESC is pressed. If ESC is pressed, the child process kills itself and the parent (this quine) resumes execution and dumps it's source to the clipboard. You have to run this from the editor.

BTW: This creates an auxiliary file 'a' on your disk.

\$\endgroup\$
  • 2
    \$\begingroup\$ Auxiliary files! \o/ \$\endgroup\$ – Addison Crump Nov 6 '15 at 10:46
3
\$\begingroup\$

C, 239 221 206 172 Bytes

Definitely could be a lot shorter, but I had to post something on PPCG eventually.

*s="*s=%c%s%c,r;h(s){r=1;}main(){signal(2,h);while(!r)printf(%c1%c);printf(s,34,s,34,34,34);}",r;h(s){r=1;}main(){signal(2,h);while(!r)printf("1");printf(s,34,s,34,34,34);}

Compiles with gcc 5.2.1 (with various warnings).

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

Haskell, 206 bytes

import Control.Exception;main=catch(putStr o)e;o='1':o;e::SomeException->IO();e _=putStr(s++show s);s="import Control.Exception;main=catch(putStr o)e;o='1':o;e::SomeException->IO();e _=putStr(s++show s);s="
\$\endgroup\$
1
\$\begingroup\$

C#, 339 Bytes

using d=System.Console;class c{static bool k=1>0;static void Main(){d.CancelKeyPress+=delegate{k=1<0;var s="using d=System.Console;class c{{static bool k=1>0;static void Main(){{d.CancelKeyPress+=delegate{{k=1<0;var s={0}{1}{0};d.WriteLine(s,(char)34,s);}};while(k){{d.Write(1);}}}}}}";d.WriteLine(s,(char)34,s);};while(k){d.Write(1);}}}
\$\endgroup\$
  • \$\begingroup\$ Nice Trick - thanks @LegionMammal978 \$\endgroup\$ – Stephan Schinkel Nov 6 '15 at 11:47
  • \$\begingroup\$ Also, delegate can be replaced with ()=>. Why isn't k a local variable? \$\endgroup\$ – LegionMammal978 Nov 6 '15 at 11:53
1
\$\begingroup\$

Perl 5.10+, 64 bytes

perl -E '$_=q{$SIG{INT}=sub{say"\$_=q{$_};eval";die};{print 1;redo}};eval'

Requires Perl 5.10+ for say, which can be enabled with either -M5.010 or -E.

How it works

This is yet another variation of the following quine, which I seem to use on every quine challenge:

$_=q{say"\$_=q{$_};eval"};eval

Broken down:

perl -E '
    $_=q{                        # store contents of quine in $_
        $SIG{INT}=sub{           # install handler for SIGINT
            say"\$_=q{$_};eval"; # print quine
            die                  # break out of eval
        };
        {
            print 1;             # print "1"
            redo                 # restart block
        }
    };
    eval                         # eval $_, executing its contents as code
'
\$\endgroup\$
  • \$\begingroup\$ Oops, initially was printing the 1s separated by newlines. Fixed at a cost of 2 bytes. \$\endgroup\$ – ThisSuitIsBlackNot Nov 6 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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