31
\$\begingroup\$

Mr. Binary Counterman, son of Mr. Boolean Masker & Mrs. Even Oddify, follows in his parents’ footsteps and has a peculiar way of keeping track of the digits.

When given a list of booleans, he counts the 1s and 0s separately, numbering the 1s with the odds & the 0s with the evens.

For example, when he looks at 1 1 0 0 1 0 he counts: 1st odd, 2nd odd, 1st even, 2nd even, 3rd odd, 3rd even and copies it down as 1 3 2 4 5 6

Mr. Binary Counterman thinks it looks prettier to start counting odds at 1 and evens at 2. However the pattern is more symmetric if you start counting evens at 0. You may do either. So either 1 3 2 4 5 6 or 1 3 0 2 5 4 are good given the list above.

As input you may take any representation of a boolean list or binary number, the output should be the list of resulting numbers with any delimiter. (But the list elements should be separate & identifiable.)

This is , so least bytes wins.

Test Cases

1 0 1 0 1 0
1 2 3 4 5 6

1 1 1 1
1 3 5 7

0 0 0 0
2 4 6 8

0 1 1 0 0
2 1 3 4 6

0 1 1 0 0 1 0 1 1
2 1 3 4 6 5 8 7 9

0 0 1 0 0 1 1 1
2 4 1 6 8 3 5 7

0
2

1
1

1 1 1 0 0 0
1 3 5 2 4 6
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can we take the bits flipped? \$\endgroup\$
    – Razetime
    Jun 15, 2021 at 3:30
  • \$\begingroup\$ Absolutely! Look forward to seeing what you've come up with. \$\endgroup\$
    – AviFS
    Jun 15, 2021 at 3:54

35 Answers 35

1
2
1
\$\begingroup\$

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 i by 2 and return it (giving values 2, 4, 6, ...)
  • If it is 1, increment v by 2 and return it (giving values 1, 3, 5, ...)
\$\endgroup\$
1
  • \$\begingroup\$ I see my name :p \$\endgroup\$
    – AviFS
    Dec 28, 2022 at 23:39
1
\$\begingroup\$

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

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Oooh, I really like this. You can shave off one more byte by first printing and then incrementing. Which means you can start at 1 instead of -1! You also have a space between int, input you can take out. \$\endgroup\$
    – AviFS
    Jun 17, 2021 at 8:42
  • \$\begingroup\$ Thank you, for your advice. \$\endgroup\$
    – Olupo
    Jun 17, 2021 at 8:57
  • \$\begingroup\$ The map(int, input() is also taking up a fair bit of real estate. You can shave off another 3 bytes, by setting it up as a function that takes an already-made list of integers. That's allowable according to the I/O requirements and meta consensus. TIO here! \$\endgroup\$
    – AviFS
    Jun 17, 2021 at 9:24
  • \$\begingroup\$ Sorry, this is my first time golfing Python. You can shave off one more by replacing print() with yield. According to this consensus you can return a generator. TIO here. \$\endgroup\$
    – AviFS
    Jun 17, 2021 at 9:33
  • \$\begingroup\$ ... and yet another two bytes thanks to @rak1507. There are two spaces on either side of v = [2,1] you can get rid of. You're down to 50 bytes! \$\endgroup\$
    – AviFS
    Jun 17, 2021 at 9:46
1
\$\begingroup\$

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!

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

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
\$\endgroup\$
1
\$\begingroup\$

Arturo, 37 bytes

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

Try it

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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