Input:
A non-empty list / vector where each element holds a value/character, indicating if you'll count up or down. I'll use 1
and -1
, but you may choose whatever you like. You can only use two values, you can't use 1,2,3...
and -1,-2,-3...
, for up and down respectively.
Challenge:
You'll use the numbers in the geometric series 1, 2, 4, 8, 16, 32.... Every time you start counting up or down, you'll count in increments of 1, then 2, then 4 and so on. If you change and start counting the other way then you'll subtract 1, then 2, then 4 and so on. The output shall be the number you get to in the end.
Examples:
In the example below, the first line is the input, the second line is the numbers you're counting up/down, the third line is the cumulative sum, and the last line is the output.
Example 1:
1 1 1 1 1 1 1 1 1 1
1 2 4 8 16 32 64 128 256 512
1 3 7 15 31 63 127 255 511 1023
1023
Example 2:
1 1 1 1 1 1 -1 -1 -1 -1 1 1 1
1 2 4 8 16 32 -1 -2 -4 -8 1 2 4
1 3 7 15 31 63 62 60 56 48 49 51 55
55
As you can see, the first 1
or -1
"resets" the value we're counting, and consecutive sequences of 1
or -1
means doubling the value.
Example 3:
-1 -1 1 1 -1 -1 -1
-1 -2 1 2 -1 -2 -4
-1 -3 -2 0 -1 -3 -7
-7
Some additional test cases to account for some potential corner cases.
The input is on the first line. The output is on the second.
1
1
-------
-1
-1
-------
-1 1 -1 1 -1 1 -1 1 -1 1 -1 1
0
This is code-golf so the shortest submission in each language wins.