| Bytes | Lang | Time | Link |
|---|---|---|---|
| 021 | Perl 5 ap | 210615T185117Z | Nahuel F |
| 027 | Wolfram Language Mathematica | 210615T194913Z | att |
| 037 | Arturo | 230214T114459Z | chunes |
| 010 | Nibbles | 230126T125550Z | xigoi |
| 044 | Risky | 210615T025934Z | rydwolf |
| 074 | Java OpenJDK 8 | 210707T171444Z | branboye |
| 018 | 8086 machine code | 210615T025220Z | EasyasPi |
| 030 | Julia | 210617T130039Z | MarcMush |
| 055 | Python 3 | 210617T083411Z | Olupo |
| 021 | Raku | 210617T072305Z | Jo King |
| 006 | Jelly | 210615T070234Z | xigoi |
| 011 | Pip p | 210616T211409Z | DLosc |
| 038 | Haskell | 210616T104807Z | lynn |
| 008 | Jelly | 210615T012459Z | caird co |
| 028 | PowerShell Core | 210615T033501Z | Julian |
| 052 | C clang | 210615T033532Z | a stone |
| 012 | Charcoal | 210615T094847Z | Neil |
| 043 | Retina 0.8.2 | 210615T094016Z | Neil |
| 006 | 05AB1E | 210615T063218Z | ovs |
| 029 | Ruby p | 210615T090533Z | ovs |
| 042 | R | 210615T075742Z | pajonk |
| 064 | Desmos | 210615T075310Z | Aiden Ch |
| 023 | Zsh | 210615T074525Z | pxeger |
| 015 | K ngn/k | 210615T072948Z | Galen Iv |
| 008 | Japt m | 210615T070906Z | Shaggy |
| 043 | Python 2 | 210615T071534Z | Wasif |
| 008 | Stax | 210615T033432Z | Razetime |
| 009 | Vyxal | 210615T015033Z | lyxal |
| 042 | AWK | 210615T033532Z | cnamejj |
| 009 | Jelly | 210615T023637Z | Bubbler |
| 012 | J | 210615T021637Z | Bubbler |
| nan | J | 210615T012228Z | Jonah |
| 023 | Dyalog APL | 210615T005627Z | AviFS |
| 015 | Jelly | 210615T011811Z | hyper-ne |
| 029 | JavaScript ES6 | 210615T010425Z | Arnauld |
Wolfram Language (Mathematica), 29 27 bytes
a[[#]]+=2&/@+(a=0?-1;-1)^#&
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).
Nibbles, 10 bytes
/$^0_:$.@+*-~%-$@~~
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
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;}
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
- 3 bytes: swap twice
Python 3, 57 55 bytes
v = [2,1]
for i in map(int,input()):print(v[i]);v[i]+=2
2 bytes less thanks to AviFS
Raku, 21 bytes
*>>.&{(%.{$_}+=2)-$_}
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
ċṪ$ƤḤ+
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:
- If it is 0, increment
iby 2 and return it (giving values 2, 4, 6, ...) - If it is 1, increment
vby 2 and return it (giving values 1, 3, 5, ...)
Haskell, 38 bytes
f(h:t)=h:f[x+mod(x-h-1)2*2|x<-t]
f e=e
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_
-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)}
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;}
C (gcc), 53 bytes
f(a,l)int*a;{for(int b[]={0,-1};l--;a++)*a=b[*a]+=2;}
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·α
η # 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·α
Ruby -p, 29 bytes
Takes input as space separated digits (or any other non-digit separator).
gsub(/\d/){($`*2+?1).count$&}
R, 42 bytes
function(x,y=seq(x)*2){x[x]=y-1;x[!x]=y;x}
-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)
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! - 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]
Starts even numbers at 0.
Explanation:
for x: for each$xin the input,$[]: arithmetic expansion++: increment and return original valuea$x: the variable nameda0ora1(which correspond to the number of 0s and 1s seen so far)x+*2: double and addxto get the correct valueecho: print (can't use<<<because the mutation wouldn't work in the subshell it creates)
K (ngn/k), 15 bytes
{x+2*(<<x)&<>x}
A K port of @Bubbler's J and APL solutions - don't forget to upvote it!
Python 2, 43 bytes
b=[2,1]
for e in input():print b[e];b[e]+=2
-5 bytes and fix thanks to @xnor
The program is now reusable
Stax, 9 8 bytes
╜♪N·{☼►◄
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+
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
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Ụ€⁺«/Ḥ_
My convoluted port of my own APL/J answer.
Jelly, 9 bytes
CỤỤ«ỤỤ$Ḥ_
Small modification of caird's 10-byter port.
J, 12 bytes
+2*/:<.&/:\:
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×⍋⌊⍥⍋⍒
J, 27 26 25 bytes
<@({.-~2*#\)/.~@/:~/:&;/:
/.~@/:~Sort and group by value({.-~2*#\)Create2 4 6 ...to the length of each group, and subtract the first element of each group from that (vectorized), so that the the 1 group becomes1 3 5 ...- The grouping screws up the order though, so we have to...
/:&;/:Resort it according to the grade up of the original input, which makes it correct again.
J, bonus: translation of AviFS's APL answer into J (28 bytes)
([:+/**-&1 0)@,:&(2*]*+/\)-.
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
Jelly, 15 bytes
ṢŒg2ḷ$\€ÄFị@ỤỤ$
ṢŒ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.