Given a string of 1
and 2
of any length, write some code (doesn't have to be a function anymore, everything will be just fine) that calculates how many steps does it need to shrink the string to a final form, following this criterion:
If your string is 112112
, this means that you have to print a 1, two 1s and a 2, like this:
1112
. When you'll perform the operation again you'll have to print a 1 and a 2. You get 12
. Then you print one 2, obtaining 2
. This is a final form, since this string is not going to change anymore. Your code will output 3
, since you needed 3 steps to get to the final form.
Other Rules
If the string has uneven length, the last number remains untouched.
Every string that cannot change anymore (like
222222
) is considered a final form.You cannot use any external source.
Your code must work with every string of
1
and2
.Shortest code wins, as it is code-golf.
Your code should print every step.
Every input method will be fine.
Examples
Input >> 122122122121212212
Your code has to print:
211222111111222
11222111222
122111222
2111222
111222
1222
222
Steps:7 (you can omit the "Steps")
---- ---- ---- ----
Input >> 22222221
Your code has to print:
22222211
2222221
2
---- ---- ---- ----
Input >> 2222
Your code has to print:
0
EDIT: Heavily edited. So sorry about that.
1 11 2
. Every two numbers is a pair: the first number of the pair says how many times to render the second number in the pair. Any final odd digit without a pair partner is rendered as-is. \$\endgroup\$