Solve the BattleBlock Theater Puzzle The game BattleBlock Theater occasionally contains a puzzle which is a generalised version of Lights Out. You've got three adjacent blocks, each of which indicates a level between 1 and 4 inclusive with bars, e.g.:

|
||||
||


If you touch a block, then that block as well as any adjacent block will increment its level (wrapping back from 4 to 1). The puzzle is solved when all three blocks show the same level (it doesn't matter which level). Since, the order you touch the blocks in doesn't matter, we denote a solution by how often each block is touched. The optimal solution for the above input would be 201:

|    --&gt; || --&gt; |||     |||
||||     |      ||      |||
||       ||     ||  --&gt; |||


The game very easily generalises any number of blocks, although for some numbers, not all configurations are solvable.

The Challenge

Given a sequence of block levels, return how often each block needs to be touched to solve the puzzle. E.g. the above example would be given as 142 and could yield 201 as a result. If there is no solution, return some consistent output of your choice, which is distinguishable from all potential solutions, e.g. -1 or an empty string.

You may write a function or program, take input via STDIN, command-line argument or function argument, in any convenient list or string format, and similarly output via a return value or by printing to STDOUT.

Your code should return correct results for all test cases within a minute on a reasonable machine. (This is not a completely strict limit, so if your solution takes a minute and ten seconds, that's fine, but if it takes 3 minutes, it isn't. A good algorithm will easily solve them in seconds.)

This is code golf, so the shortest answer (in bytes) wins.

Examples

Solutions are not unique, so you may get different results.

Input                          Output

1                              0
11                             00
12                             No solution
142                            201
434                            101
222                            000
4113                           0230
32444                          No solution
23432                          10301
421232                         212301
3442223221221422412334         0330130000130202221111
22231244334432131322442        No solution
111111111111111111111222       000000000000000000000030
111111111111111111111234       100100100100100100100133
412224131444114441432434       113013201011001101012133


As far as I know, there are exactly 4 solutions for each input where the number of blocks is 0 mod 3, or 1 mod 3, and there are 0 or 16 solutions where it is 2 mod 3.
Python 2, 115 bytes
n=input()
for F in range(4):
 t=[F];b=0;exec&quot;x=(-n[b]-sum(t[-2:]))%4;t+=x,;b+=1;&quot;*len(n)
 if x&lt;1:print t[:-1];break

This is the golfed version of the program I wrote while discussing the problem with Martin.
Input is a list via STDIN. Output is a list representing the last solution found if there is a solution, or zero if there isn't. For example:
&gt;&gt;&gt;
[1, 4, 2]
[2, 1, 1]
&gt;&gt;&gt;
[1, 2]
0
&gt;&gt;&gt;
map(int,&quot;3442223221221422412334&quot;)
[2, 3, 3, 2, 1, 3, 2, 0, 0, 2, 1, 3, 2, 2, 0, 0, 2, 2, 3, 1, 1, 3]


Pyth, 32 29 bytes
V4J]NVQaJ%_+s&gt;J_2@QN4)I!eJPJB

The obligatory port. Thanks to @Jakube for the 3 byte saving.
Input method is the same as above, try it online.

Explanation (long and full of logic!)
First, two basic observations:

Observation 1: It doesn't matter which order you touch the blocks

Observation 2: If you touch a block 4 times, it's equivalent to touching it once


In other words, if there is a solution then there is a solution where the number of touches is between 0 and 3 inclusive.
Since modulo 4 is so nice, let's do that with the blocks too. For the rest of this explanation, block level 0 is equivalent to block level 4.
Now let's denote a[k] to be the current level of block k and x[k] to be the number of times we touch block k in a solution. Also let n be the total number of blocks. As @Jakube has noted, a solution must satisfy:
  a[0]   + x[0] + x[1]
= a[1]   + x[0] + x[1] + x[2]
= a[2]          + x[1] + x[2] + x[3]
= a[3]                 + x[2] + x[3] + x[4]
...
= a[n-1]                                     ...  + x[n-2] + x[n-1] + x[n]
= a[n]                                       ...           + x[n-1] + x[n]
= C

where C is the final level all blocks end up on, between 0 and 3 inclusive (remember we're treating level 4 as level 0) and all equations above are really congruences modulo 4.
Now here's the fun part:

Observation 3: If a solution exists, a solution exists for any final block level 0 &lt;= C &lt;= 3.

There are three cases based on the number of blocks modulo 3. The explanation for each of them is the same — for any number of blocks, there exists a subset of blocks which, if you touch each of them once, increases all block levels by exactly 1.
0 mod 3 (touch every third block starting from the second):
    .X. / .X. / .X.

1 mod 3 (touch every third block starting from the first):
    X. / .X. / .X. / .X

2 mod 3 (touch every third block starting from either the first or second):
    X. / .X. / .X. / .X.
    .X. / .X. / .X. / .X

This explains why there are 4 solutions for 0 mod 3 and 1 mod 3, and usually 16 solutions for 2 mod 3. If you have a solution already, touching the blocks as above gives another solution which ends up at a higher block level (wrapping around).
So what does this mean? We can pick any final block level C we want! Let's pick C = 0, because this saves on bytes.
Now our equations become:
0 = a[0] + x[0] + x[1]
0 = a[1] + x[0] + x[1] + x[2]
0 = a[2] + x[1] + x[2] + x[3]
0 = a[3] + x[2] + x[3] + x[4]
...
0 = a[n-1] + x[n-2] + x[n-1] + x[n]
0 = a[n] + x[n-1] + x[n]

And rearrange:
x[1] = -a[0] - x[0]
x[2] = -a[1] - x[0] - x[1]
x[3] = -a[2] - x[1] - x[2]
x[4] = -a[3] - x[2] - x[3]
...
x[n] = a[n-1] - x[n-2] - x[n-1]
x[n] = a[n] - x[n-1]

So what we can see is, if we have x[0], then we can use all the equations except the last to find out every other x[k]. The last equation is an additional condition we must check.
This gives us an algorithm:

Try all values for x[0]
Use the above equations to work out all of the other x[k]
Check if the last condition is satisfied. If so, save the solution.

That gives the solution above.
So why do we sometimes get no solution for 2 mod 3? Let's take a look at these two patterns again:
X. / .X. / .X. / .X.
.X. / .X. / .X. / .X

Now consider the equations at those positions, i.e. for the first one:
0 = a[0] + x[0] + x[1]
0 = a[3] + x[2] + x[3] + x[4]
0 = a[6] + x[5] + x[6] + x[7]
0 = a[9] + x[8] + x[9] + x[10]

Add them up:
0 = (a[0] + a[3] + a[6] + a[9]) + (x[0] + x[1] + ... + x[9] + x[10])

For the second one:
0 = a[1] + x[0] + x[1] + x[2]
0 = a[4] + x[3] + x[4] + x[5]
0 = a[7] + x[6] + x[7] + x[8]
0 = a[10] + x[9] + x[10]

Add them up again:
0 = (a[1] + a[4] + a[7] + a[10]) + (x[0] + x[1] + ... + x[9] + x[10])

So if (a[1] + a[4] + a[7] + a[10]) and (a[0] + a[3] + a[6] + a[9]) are not equal, then we have no solution. But if they are equal, then we get 16 solutions. This was for the n = 11 case, but of course this generalises to any number that is 2 mod 3 — take the sum of every third element starting from the second, and compare to the sum of every third element starting from the first.
Now finally, is it possible to figure out what x[0] has to be instead of trying all of the possibilities? After all, since we restricted our target level C to be 0, there is only one x[0] which gives a solution in the 0 mod 3 or 1 mod 3 case (as 4 solutions / 4 final levels = 1 solution for a specific final level).
The answer is... yes! We can do this for 0 mod 3:
 .X..X
.X..X.

Which translates to:
0 = a[2] + x[1] + x[2] + x[3]   -&gt; 0 = (a[2] + a[5]) + (x[1] + ... + x[5])
0 = a[5] + x[4] + x[5]          /


0 = a[1] + x[0] + x[1] + x[2]   -&gt; 0 = (a[1] + a[4]) + (x[0] + x[1] + ... + x[5])
0 = a[4] + x[3] + x[4] + x[5]   /

Subtracting gives:
x[1] = (a[2] + a[5]) - (a[1] + a[4])

Similarly for 1 mod 3 we can do this pattern:
 .X..X.
X..X..X

Which gives:
x[0] = (a[2] + a[5]) - (a[0] + a[3] + a[6])

These of course generalise by extending the indices by increments of 3.
For 2 mod 3, since we have two subsets which cover every block, we can actually pick any x[0]. In fact, this is true for x[0], x[1], x[3], x[4], x[6], x[7], ... (basically any index not congruent to 2 mod 3, as they are not covered by either subset).
So we have a way of picking an x[0] instead of trying all possibilities...
... but the bad news is that this doesn't save on bytes (124 bytes):
def f(n):s=[];L=len(n);B=sum(n[~-L%3::3])-sum(n[-~L%3::3]);x=A=0;exec&quot;s+=B%4,;A,B=B,-n[x]-A-B;x+=1;&quot;*L*(L%3&lt;2or B&lt;1);print s