12
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.
3"If your string is 112112, this means that you have to print a 1, two 1s and a 2, like this: 1112. " I don't get it. – Fabinout – 2014-01-03T16:16:46.563
7Try to read it loudly. It's "one one", "two ones" and "one two". I mean, "1 time 1", "2 times 1" and "1 time 2". – Vereos – 2014-01-03T16:18:41.507
Okay it's clear, thanks. – Fabinout – 2014-01-03T16:19:06.370
5If regexes are "not even useful", why do you ban them? – J B – 2014-01-03T17:01:49.643
1Regex restriction removed. – Vereos – 2014-01-03T18:59:37.513
Uhm... I don't think it's fair to change the specification after 5 answers have been submitted (it has to be a function all of a sudden??). Besides, if it's supposed to be a function, shouldn't it return the answer, and not output it? – daniero – 2014-01-03T20:54:27.617
I didn't change nothing, I just highlighted it! Quincunx's answer made me think it was not that clear, so I set it bold. But yes, I guess that the function should return the answer. I feel like I'm messing it up, and I'm so sorry about that. :/ – Vereos – 2014-01-03T20:57:02.600
So... I guess it wouldn't be fair to make the users change the answers, so I'll edit it again, I'll just accept any function/program that gives the right output. Feel free to send any solution you'd like to. – Vereos – 2014-01-03T20:58:54.360
"Every input method will be fine". An input method can do an arbitrary amount of preprocessing on the input, up to and including generating the answer as one long string. The "Python - 126" answer by Quincunx hints at this ambiguity (mentioning his answer could be 118 if ...) Seems like the "input method" needs to be more rigorously specified. – user2460798 – 2014-01-04T06:40:21.060
I really don't get it. How do you transform
112112
to1112
? – ProgramFOX – 2014-01-04T08:43:58.287@user2460798 That was before I specified that. You can use whatever input method shortens your code. @ProgramFOX Like I said before, reading loudly really helps. If you divide the string in pairs, you get
11|21|12
, right? Then, read the pairs: first is "One one", that means "a one",1
. Then we have "two one", which is "one, two times", so11
. Finally, we have "one two", that means "a two",2
. We could say that the first number tells how many times the second number must be repeated. – Vereos – 2014-01-04T10:06:38.2571@ProgramFOX "one 1, two 1s, and a 2":
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. – apsillers – 2014-01-04T14:47:54.663