Intro
Find the result of number cannibalism.
Why was 6 scared of 7? Becase 7 8
9.
Your program will have to find the result of applying the 8
operation(defined below) on a number, repeatedly.
Challenge
Your input will be a single positive integer, n.
You need to find each 8
in the integer, and substitute it for +
or -
based on the following conditions.
- remove all
8
s at the beginning or end of the string. - If there are multiple
8
s, then collapse them into a single8
. - \$a\$ and \$b\$ are the chunks to the left and right of the
8
respectively. Since8
is an operator, the numbers are delimited by8
. - If \$ a ≥ b,\$
8
becomes+
. - else, it becomes
-
.
Then evaluate the expression from left to right, and take the absolute value.
If the result has any 8
s in it, the repeat the above steps again till there are none.
For example, 12384568789
becomes:
123 456 7 9
123 < 456: 123 - 456 7 9
456 ≥ 7 : 123 - 456 + 7 9
7 < 9 : 123 - 456 + 7 - 9 = -335
= abs(-335)
= 335
Hence 335 is the final answer.
Example Input and Output
789 → 2
180 → 1
42312348 → 4231234
2389687 → 66
12384568789 → 335
13749547874394873104972349 → 7309154
808 → 0
7800482 → 13
14458883442 → 1997
88888 → indeterminate
388182 → 2
08182 → 3
Here are the testcases as an array:
[789,180,42312348,2389687,12384568789,13749547874394873104972349,808,7800482,14458883442,88888]
Scoring Criteria
This is code-golf. Shortest answer in each language wins.