# Write a binary counter in quine

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

• Near duplicate to codegolf.stackexchange.com/questions/35974/… - computation done by quining program with 2 different code blocks. Jan 26, 2015 at 3:53
• Can the program read its own source code? Jan 26, 2015 at 3:53
• @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. Jan 26, 2015 at 7:29
• 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! Jan 26, 2015 at 14:48
• Probably worth noting that s_zero and s_one have to be different. Otherwise I have many solutions with 2*n scores. Feb 15, 2015 at 11:51

# 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. ";
}_~

• every single time ... Jan 26, 2015 at 10:47

# 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.

# Python 3.8 (pre-release), 264 250 120 + 120 = 240 bytes

## Code 0: (120 bytes)

for i in(c:=[]),0:
c+=0,;exec(s:="i or print(end=f'for i in(c:=[]),0:\\n c+={c^all(c:=c[1:-1])},;exec(s:=%r)#'%s)")#


Try it online!

## Code 1: (120 bytes)

for i in(c:=[]),0:
c+=1,;exec(s:="i or print(end=f'for i in(c:=[]),0:\\n c+={c^all(c:=c[1:-1])},;exec(s:=%r)#'%s)")#


Try it online!

Try it online!

## How it works:

• c store the binary number of the program on the form of a list

The for loop execute twice :

• the first time, it assigns c to the correct value
• the second time, it prints the code replacing a char by 1 or 0 depending on c. The nth time c^all(c:=c[1:-1]) is called, it returns the nth leftmost bit of c+1

the next for loop are ignored thanks to the comment #

The printing is a derivate of this quine : exec(s:="print('exec(s:=%r)'%s)")

# 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

# 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.