Write a binary counter in quine

10

Write two code fragments, which we will call s_zero and s_one.

Program (l, n) consists of l copies of s_zero and s_one corresponding with the digits of n in binary, padded with s_zero on the left.

For example, if s_zero = foo and s_one = bar then
Program (4, 0) = foofoofoofoo
Program (4, 1) = foofoofoobar
Program (4, 2) = foofoobarfoo
etc.

Program (l, n) must print the source of Program (l, (n + 1) mod (2^l)) to standard out. In the example above, foobarfoofoo must print foobarfoobar when executed.

Your score is the sum of the lengths of fragments s_zero and s_one

QuadmasterXLII

Posted 2015-01-26T03:51:09.033

Reputation: 881

Near duplicate to http://codegolf.stackexchange.com/questions/35974/code-that-runs-the-game-of-life-on-itself - computation done by quining program with 2 different code blocks.

– feersum – 2015-01-26T03:53:47.550

Can the program read its own source code? – Doorknob – 2015-01-26T03:53:50.210

2@feersum I disagree. This one is a lot easier, in particular, you don't need to be able to determine line breaks. Furthermore, I think the task does make a difference, otherwise every generalised quine challenge would be a dupe of the basic quine challenge. – Martin Ender – 2015-01-26T07:29:21.010

The advantage of the simpler task is that it encourages competition to create the shockingly short answers seen so far- I hope that justifies this challenge as different! – QuadmasterXLII – 2015-01-26T14:48:42.907

1Probably worth noting that s_zero and s_one have to be different. Otherwise I have many solutions with 2*n scores. – randomra – 2015-02-15T11:51:44.733

Answers

6

CJam, 29 + 29 = 58 bytes

The 0 code:

0{"_~"]]z(3\+2b(2b2>a\+z~[}_~

The 1 code:

1{"_~"]]z(3\+2b(2b2>a\+z~[}_~

Explanation

0                       " Push the current bit. ";
{"_~"                   " The basic quine structure. ";
    ]                   " Make an array of the current bit, the function and _~.
                          That is the code fragment itself. ";
    ]                   " Make an array of all code fragments in the stack. ";
    z(                  " Extract the array of the bits. ";
    3\+2b(2b2>          " Convert from base 2, decrease by one and convert back,
                          keeping the length. ";
    a\+z                " Put the bits back to the original position. ";
    ~                   " Dump the array of code fragments back into the stack. ";
    [                   " Mark the beginning position of the array of the next code fragment. ";
}_~

jimmy23013

Posted 2015-01-26T03:51:09.033

Reputation: 34 042

2every single time ... – Optimizer – 2015-01-26T10:47:55.950

3

CJam, 47 + 47 = 94 bytes

The 0 code:

{`"_~"+]:+T2*0+:T)\AsSerS/_,(@2Y$#+2bU@->]zs}_~

The 1 code:

{`"_~"+]:+T2*1+:T)\AsSerS/_,(@2Y$#+2bU@->]zs}_~

Excuse the expletive.

I'm sure I can still shave off a few bytes there. I'll add an explanation once I decide that I can't be bothered to golf it any further.

Test it here.

Martin Ender

Posted 2015-01-26T03:51:09.033

Reputation: 184 808

1

CJam, 45 + 45 = 90 bytes

The 0 code:

{`]W=L0+:L,Ua*L2b)2b+L,W*>\f{X$!s/\s*"_~"}}_~

The 1 code:

{`]W=L1+:L,Ua*L2b)2b+L,W*>\f{X$!s/\s*"_~"}}_~

Explanation soon.

Try it online here

Optimizer

Posted 2015-01-26T03:51:09.033

Reputation: 25 836

1

GolfScript, 37 + 37 = 74 bytes

0{`".~"+:q;]-2%1\{1$^.@&q@}%\;-1%~}.~
1{`".~"+:q;]-2%1\{1$^.@&q@}%\;-1%~}.~

Not quite as short as user23013's CJam solution, but I figured I'd post this anyway, if only to (marginally) increase the diversity of the languages used.

My solution not directly based on any of the existing solutions (and, indeed, I haven't examined them in detail, as I still don't read CJam very well), but they all feature variants of the same basic quine structure ({".~"}.~ in GolfScript, {"_~"}_~ in CJam). That's not really very surprising, since it seems to be one of the most efficient ways to write a quine with an arbitrary payload in these languages.

There are several parts of this code I don't really like, and I suspect it may be possible to golf this further, but I've spent too much time on this as it is.

Ilmari Karonen

Posted 2015-01-26T03:51:09.033

Reputation: 19 513