g | x | w | all
Bytes Lang Time Link
021Perl 5 ap210615T185117ZNahuel F
027Wolfram Language Mathematica210615T194913Zatt
037Arturo230214T114459Zchunes
010Nibbles230126T125550Zxigoi
044Risky210615T025934Zrydwolf
074Java OpenJDK 8210707T171444Zbranboye
0188086 machine code210615T025220ZEasyasPi
030Julia210617T130039ZMarcMush
055Python 3210617T083411ZOlupo
021Raku210617T072305ZJo King
006Jelly210615T070234Zxigoi
011Pip p210616T211409ZDLosc
038Haskell210616T104807Zlynn
008Jelly210615T012459Zcaird co
028PowerShell Core210615T033501ZJulian
052C clang210615T033532Za stone
012Charcoal210615T094847ZNeil
043Retina 0.8.2210615T094016ZNeil
00605AB1E210615T063218Zovs
029Ruby p210615T090533Zovs
042R210615T075742Zpajonk
064Desmos210615T075310ZAiden Ch
023Zsh210615T074525Zpxeger
015K ngn/k210615T072948ZGalen Iv
008Japt m210615T070906ZShaggy
043Python 2210615T071534ZWasif
008Stax210615T033432ZRazetime
009Vyxal210615T015033Zlyxal
042AWK210615T033532Zcnamejj
009Jelly210615T023637ZBubbler
012J210615T021637ZBubbler
nanJ210615T012228ZJonah
023Dyalog APL210615T005627ZAviFS
015Jelly210615T011811Zhyper-ne
029JavaScript ES6210615T010425ZArnauld

Perl 5 (-ap), 27 21 bytes

6 bytes saved thanks to @Xcali

s/\d/$&+$c[$&]++*2/ge

Try it online!

Wolfram Language (Mathematica), 29 27 bytes

a[[#]]+=2&/@+(a=0?-1;-1)^#&

Try it online!

Boring answer. Mathematica's modified assignment operators (e.g. +=) have higher precedence than its assignment operators (e.g. =). It so happens that Function (&)'s precedence falls in between, so modified assignment doesn't need parentheses inside &. (//= is slightly different from both groups).

Arturo, 37 bytes

$=>[p:[2,1]map&'x[n:p\[x]p\[x]:n+2n]]

Try it

Nibbles, 10 bytes

/$^0_:$.@+*-~%-$@~~

Attempt This Online!

Disgusting.

General idea: Process the list right to left. Prepend each bit to the result, then increase all numbers in the result that have the same parity by 2.

/  Right fold
$    the input
    with initial value
^0   repeat 0 times
_     the input
:   join
$    the bit
.    map
@     the accumulator
+     add
*      multiply
-       subtract
~        1
%        modulo
-         subtract
$          the number in the accumulator
@          the bit being prepended
~         2
~       2
       the number in the accumulator

Risky, 44 bytes

__0+0+_0+0+__0+0+_0+0+__0+0+_0+0+__0+0+_0+?1__0+0+_0+0+__0+0+_0!-_0!_{1+_0+0_[2_{0+__{1

Try it online!

How it works:

This is a really low level explanation:

... + __0+0+_0+?                                                  ;  the input array
                 1                                                ;  map with the following pairs:
                   ... + __0+0+_0!-                               ;  [0, -1]
                                    _                             ;  map to
                                      0!_{1+_0+0                  ;  range with same length
                                                 _                ;  map to
                                                   [              ;  absolute value
                                                          +       ;    of the sum of
                                                     2_{0         ;      twice the index in the range and
                                                            __{1  ;      the offset (0 or -1)

That's useless, though. Here's a better description of how this works:

Risky has an operator called "map pairs" which takes an array, and maps the items according to a set of rules. The rules are arrays, starting with the item to be replaced, and with (typically) one item to map to. However, if multiple are specified, they'll be used in order.

This answer generates those mappings, which look like [[0, 2, 4, 6, ...], [1, 1, 3, 5, 7, ...]]. It does this by mapping [0, -1] to [2_{0+__{1 over a range [0, x), which is essentially (x, n) => abs(2 * x + n), where x is the number in the range and n is either 0 or -1.

Java (OpenJDK 8), 74 bytes

a->{for(int c=0,i=-1,j=0;c<a.length;)a[c]=a[c++]==1?i+=2:(j+=2);return a;}

Try it online!

8086 machine code, 21 18 bytes

00000000: b0 01 b2 02 d1 eb 72 01 92 aa 40 40 72 01 92 e2  ......r...@@r...
00000010: f3 c3                                            ..

Function.

        [bits 16]
        [cpu 8086]
        section .text
        ; nasm syntax
        ; INPUT:
        ;    DI: destination byte array
        ;    BX: bit pattern (little endian)
        ;    CX: count of bits
        ; OUTPUT:
        ;    stored to DI
        global  mrbitctr
mrbitctr:
        ; Count odd bits in AL
        mov     al, 1
        ; Count evens in DL
        mov     dl, 2
.loop:
        ; Shift right BX one bit. This will put
        ; the lowest bit in CF.
        shr     bx, 1
        ; Was the bit set? If so, jump.
        jc      .no_swap
        ; Even: swap
.swap:
        ; Pull the old switcheroo to select evens
        xchg    ax, dx
        ; Odd: don't swap
.no_swap:
        ; Store to DI and increment
        stosb
        ; Add 2 to AL by incrementing AX twice
        ; Note: INC does not affect the carry flag
        inc     ax
        inc     ax
        ; swap back if even
        jc      .no_swap_back
.swap_back:
        xchg    ax, dx
.no_swap_back:
        ; Loop CX times.
        loop    .loop
.end:
        ret

Julia, 33 30 bytes

f(x,y=[0,-1])=x.|>i->y[i+1]+=2

Try it online!

Python 3, 57 55 bytes

v = [2,1]
for i in map(int,input()):print(v[i]);v[i]+=2

Try it online!

2 bytes less thanks to AviFS

Raku, 21 bytes

*>>.&{(%.{$_}+=2)-$_}

Try it online!

Maps each element to the index into an anonymous hash, incrementing that value by two (initially zero), and finally subtracting the element itself to distinguish between odd and even.This could also be extended to values beyond 0 and 1 simply by changing the 2 to another number.

Jelly, 7 6 bytes

ċṪ$ƤḤ+

Try it online!

Basically a translation of an old version of ovs' 05AB1E answer.

Explanation

ċṪ$ƤḤ+ Main monadic link
   Ƥ   Map over prefixes
  $    (
ċ        Count the occurences of
 Ṫ         the last item after removing it
  $    )
    Ḥ  Unhalve
     + Add the original list

Pip -p, 11 bytes

{a?vi+:2}Ma

Takes input as a single string of 1's and 0's on the command-line. Try it online!

Explanation

             i is preinitialized to 0, v to -1 (implicit)
{       }Ma  Map the following function to the characters of the input:
     +:2       Add 2 to...
 a?vi          ... v, if the character is 1 (truthy), or i, if it is 0 (falsey)
               ... and return the resulting value
             Autoprint in list format (implicit, -p flag)

In other words, for each character in the input:

Haskell, 38 bytes

f(h:t)=h:f[x+mod(x-h-1)2*2|x<-t]
f e=e

Try it online!

The recursion happens on “the tail of the list, but with all elements x that have the same parity as the head incremented by 2.” Like so:

  f [1,1,0,0,1,0]
= 1 : f [3,0,0,3,0]
= 1 : 3 : f [0,0,5,0]
= 1 : 3 : 0 : f [2,5,2]
= 1 : 3 : 0 : 2 : f [5,4]
= 1 : 3 : 0 : 2 : 5 : f [4]
= 1 : 3 : 0 : 2 : 5 : 4 : f []
= [1,3,0,2,5,4]

Jelly, 8 bytes

,CÄḤ×ƊS_

Try it online!

-2 bytes thanks to Bubbler

How it works

,CÄḤ×ƊS_ - Main link. Takes a binary list B on the left
 C       - Complement. Flip the bits of B
,        - Pair with B: [B, B']
     Ɗ   - Last 3 links as a monad f([B, B']):
  Ä      -   Cumulative sum of each
   Ḥ     -   Unhalve
    ×    -   Multiply modified B by B and modified B' by B'
      S  - Columnwise sum
       _ - Subtract B, elementwise

PowerShell Core, 28 bytes

$b=0,-1;$args|%{($b[$_]+=2)}

Try it online!

Port of Arnauld's solution, thanks !

Initial implementation, 35 bytes

switch($args){0{++$e*2}1{$o++*2+1}}

Try it online!
Or starting with 0 (35 bytes)

Takes the input as a list of 0/1's
Returns a list of integers

Explanation

switch($args){ # For each argument passed as an integer
0{++$e*2}      # if it is 0, output an even number, starting from 2
1{$o++*2+1}}   # if it is 1, output an odd number, starting from 1

C (clang), 52 bytes

-1 thanks to @AZTECCO, by using clang instead of gcc.

f(a,l)int*a;{for(int b[]={0,-1};l--;)*a++=b[*a]+=2;}

Try it online!


C (gcc), 53 bytes

f(a,l)int*a;{for(int b[]={0,-1};l--;a++)*a=b[*a]+=2;}

Try it online!

Charcoal, 12 bytes

IEθ⁺Iι⊗№…θκι

Try it online! Link is to verbose version of code. Takes input as a string of bits. 0-indexed. Explanation:

  θ             Input string
 E              Map over characters
     ι          Current character
    I           Cast to integer
   ⁺            Plus
      ⊗         Doubled
       №        Count of
           ι    Current character in
         θ      Input string
        …       Truncated to length
          κ     Current index
I               Cast to string
                Implicitly print on separate lines

Retina 0.8.2, 43 bytes

(.)(?<=((\1)|.)*(1)?)
$#3$*2$4¶
2
11
1+
$.&

Try it online! Link includes test cases. Takes input as a string of bits. 1-indexed. Explanation:

(.)(?<=((\1)|.)*(1)?)

For each bit, count the number of preceding identical bits. If the current bit is 1 then count it separately otherwise include the current bit in the count.

$#3$*2$4¶

Record a 2 for each duplicate plus one extra for a 1 bit.

2
11

Convert to unary.

1+
$.&

Convert the sum back to decimal.

05AB1E, 8 6 bytes

η^_O·α

Try it online!

η       # prefixes of the input
 ^      # XOR the first value of the input with the first prefix, second value of input with second prefix, ...
  _     # boolean negate
   O    # sum each modified prefix
    ·   # double all integers
     α  # absolute difference to the input

η^_ can be replaced with δQÅl (equality table; lower-triangular matrix), which is a byte longer but might be shorter in some other language.

δQÅlO·α

Try it online!

Ruby -p, 29 bytes

Takes input as space separated digits (or any other non-digit separator).

gsub(/\d/){($`*2+?1).count$&}

Try it online!

R, 42 bytes

function(x,y=seq(x)*2){x[x]=y-1;x[!x]=y;x}

Try it online!

-8 bytes thanks to @Dominic

Takes input as booleans: TRUE(1) and FALSE(0).

Straightforward approach, but takes advantage of truncating the replacement to the length of items being replaced.


Different approach:

R, 47 bytes

function(x,n=0:-1)for(i in x)show(n[i]<-n[i]+2)

Try it online!

Takes input incremented by 1: 2 (for 1) and 1 (for 0).

Desmos, 64 bytes

a(l)=\sum_{n=1}^{[1...length(l)]}l[n]
b=1-l
f(l)=2ba(b)+2la(l)-l

Just implements the strategy shown in AviFS's Dyalog APL answer.

Try It On Desmos!

Try It On Desmos! - Prettified

Explanation:

a(l)=\sum_{n=1}^{[1...length(l)]}l[n]: Function that takes in a list \$l\$ and returns the running total of \$l\$.

b=1-l: Variable that stores the inputted list, but with each bit flipped.

f(l)=2ba(b)+2la(l)-l: Function that takes in a list of bits \$l\$ and outputs the correct answer, based on the strategy mentioned above.

Zsh, 23 bytes

for x
echo $[x+a$x++*2]

Attempt This Online!

Starts even numbers at 0.

Explanation:

K (ngn/k), 15 bytes

{x+2*(<<x)&<>x}

Try it online!

A K port of @Bubbler's J and APL solutions - don't forget to upvote it!

Japt -m, 8 bytes

?J±2:T±2

Try it

Python 2, 43 bytes

b=[2,1]
for e in input():print b[e];b[e]+=2

Try it online!

-5 bytes and fix thanks to @xnor

The program is now reusable

Stax, 9 8 bytes

╜♪N·{☼►◄

Run and debug it

Inspired by Arnauld's idea.

0-indexed, takes the bits flipped.

Explanation

AEsF{Q2+}&
AEs        swap the input with  [1,0]
   F       foreach i:
    {   }&  modify the element at i in 2,1
     Q      print without popping
      2+    add 2

Vyxal, 15 9 bytes

₌⇧⇩⇧$⇧∵d+

Try it Online!

Me when APL port

Explained

₌⇧⇩⇧$⇧∵d+
₌⇧⇩       # grade up input, grade down input
   ⇧$⇧    # grade each of those up
      ∵d  # 2 * the minimum of those two lists
        + # added to the input   

AWK, 42 bytes

d[1]=1{for(;a++<NF;d[b]+=2)$a=+d[b=$a%2]}1

Try it online!

So this one is one test with a codeblock, and a naked 1 to print all the commandline arguments. It replaces each commandline argument with the appropriate even/odd counter in the codeblock.

The "test" for the codeblock is always truthy and it just used to initialize the "odd" counter to 1.

d[1]=1{                                  }

The code block runs through each commandline argument,

       for(;a++<NF;       )

Then sets that argument to the current value of the even/odd counter with:

                           $a=+d[b=$a%2]

And at the end of the loop, increments the current counter by 2 in preparation for the next match.

                   d[b]+=2

Once that's done, it just need to print out all the arguments.

                                         1

Jelly, 9 bytes

,CỤ€⁺«/Ḥ_

Try it online!

My convoluted port of my own APL/J answer.

Jelly, 9 bytes

CỤỤ«ỤỤ$Ḥ_

Try it online!

Small modification of caird's 10-byter port.

J, 12 bytes

+2*/:<.&/:\:

Try it online!

Explanation: Self plus twice the minimum of ascending and descending ranks.

Given a boolean array 1 1 0 0 1 1 1, ascending rank /:@/: and descending rank /:@\: are computed as follows:

array:       1 1 0 0 1 1 1
asc. rank:   2 3 0 1 4 5 6
desc. rank:  0 1 5 6 2 3 4
minimum:     0 1 0 1 2 3 4

APL(Dyalog Unicode), 9 bytes SBCS

⊢+2×⍋⌊⍥⍋⍒

Try it on APLgolf!

J, 27 26 25 bytes

<@({.-~2*#\)/.~@/:~/:&;/:

Try it online!

J, bonus: translation of AviFS's APL answer into J (28 bytes)

([:+/**-&1 0)@,:&(2*]*+/\)-.

Try it online!

Just because I liked it and wanted to see how they'd compare.

Dyalog APL, 24 23 bytes

(⍵ׯ1+2×+\⍵)+N×2×+\N←~⍵

The list of evens and the list of odds are generated separately and added elementwise with the + in the middle. Here's what it looks like with the problem's example input:

Evens:
    ⍵            The input                      → 1 1 0 0 1 0
    ~            Negate it                      → 0 0 1 1 0 1
    N←           Let N be the negated list      → 0 0 1 1 0 1
    +\           Take the running sum           → 0 0 1 2 2 3
    2×           Multiply by two                → 0 0 2 4 4 6
    N×           Multiply by the negated list   → 0 0 2 4 0 6

Odds:
    ⍵            The input                      → 1 1 0 0 1 0
    +\           Take the running sum           → 1 2 2 2 3 3
    2×           Multiply by two                → 2 4 4 4 6 6
    ¯1+          Subtract 1                     → 1 3 3 3 5 5
    ⍵×           Multiply by the list           → 1 3 0 0 5 0

Together:
  (~⍵)×2×+\~⍵    Evens                          → 0 0 2 4 0 6
  (⍵ׯ1+2×+\⍵)   Odds                           → 1 3 0 0 5 0
  +              Add elementwise                → 1 3 2 4 5 6

Try it online!

Jelly, 15 bytes

ṢŒg2ḷ$\€ÄFị@ỤỤ$

Try it online!

ṢŒg2ḷ$\€ÄFị@ỤỤ$  Main Link; take a list of 0s and 1s
Ṣ                Sort the list
 Œg              Group runs of equal elements
       €         For each group
     $\          Cumulatively reduce by
   2ḷ            x => 2 (that is, all but the first element become 2)
        Ä        Cumulative sum, vectorizing to depth 1
         F       Flatten
          ị@     Index into (reverse order)
            ỤỤ$  The input graded up twice

Grading up twice returns the permutation to index into another list to get the same ordering or something like that. I think that's how it works.

JavaScript (ES6), 29 bytes

a=>a.map(v=>b[v]+=2,b=[0,-1])

Try it online!