This challenge is inspired by the AoC 2020, Day 14 - Part II, created by Eric Wastl and his team, which asks to output the possible binary values from a bitmask.
Let's say we've a bitmask like "10X0X0"
, then we've to find the possible binary values that can be generated by replacing an X
with either 0
or 1
. Here I've taken X
, but you could take anything except 0
and 1
, of course.
So, the possible binaries are ["100000", "100010", "101000", "101010"]
.
When each of them are converted in decimal, then these are [32, 34, 40, 42]
respectively.
And, finally the sum is 148
.
Test cases
INPUT: "X"
["0", "1"]
[0, 1]
OUTPUT: 1
INPUT: "0"
OUTPUT: 0
INPUT: "1"
OUTPUT: 1
INPUT: "1X0"
["100", "110"]
[4, 6]
OUTPUT: 10
INPUT: "1X0X1"
["10001", "10011", "11001", "11011"]
[17, 19, 25, 27]
OUTPUT: 88
INPUT: "X0X0X0X"
["0000000", "0000001", "0000100", "0000101", "0010000", "0010001", "0010100", "0010101",
"1000000", "1000001", "1000100", "1000101", "1010000", "1010001", "1010100", "1010101"]
[0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85]
OUTPUT: 680
INPUT: "1X1X1X1X1X1X1X1X1X1X1"
OUTPUT: 1789569024
INPUT: "1X01X01X01X01X01X01X01X01X01X01X01X0"
OUTPUT: 201053554790400
INPUT: "X000X000X000X000X000X000X000X000X000X000X000X000"
OUTPUT: 307445734561824768
INPUT: "101"
["101"]
[5]
OUTPUT: 5
INPUT: "XXX"
["000", "001", "010", "011", "100", "101", "110", "111"]
[0, 1, 2, 3, 4, 5, 6, 7]
OUTPUT: 28
Rules
- It is guaranteed that \$ 0 \leq \text{count}(X) \leq 12 \$ and \$ 1 \leq \text{length(mask)} \leq 48 \$.
- Input can be either a string, or an array of chars (non-empty).
- I have chosen
X
in bitmask, however you can replace it with any other character of your choice. - Output will be the sum of the binaries in decimal.
- This is a code-golf, so fewest bytes will win!
X
? \$\endgroup\$.5
forX
?! \$\endgroup\$