I am not giving a new solution. But I'd like to propose a nicer expression of the solution given for the general case $n=2^k$.
In fact my solution is the same as noedne's.
And the explanation why it works is the same as OHO's.
In the solutions already given the method to explore all states uses the pattern of A, B, A, C, A, B, A, D, ... . The same move A repeats every second move. Between these a second move B repeats every 4 moves, and so on.
You can write it shorter by just writing [D, C, B, A], this notation meaning that you do [C, B, A] then D and [C, B, A] again. [C, B, A] means [B, A] then C then [B, A], and so on. Each list is executed by doing the tail of the list, then the head move, then the tail of the list again.
Using this notation the solution for $n=2^0$ is [x]
, for $n=2^1$ is [x, xx]
and for $n=2^2$ is [x, xx, x.x, xxxx]
. The x
's and .
's denote resp. a button you switch and a button you don't switch. xx
means to switch any two adjacent buttons, x.x
means to switch any two buttons separated by a single button you don't touch.
You can continue for $n=2^3$: [x, xx, x.x, xxxx, x...x, xx..xx, x.x.x.x, xxxxxxxx]
and $n=2^4$: [x, xx, x.x, xxxx, x...x, xx..xx, x.x.x.x, xxxxxxxx, x.......x, xx......xx, x.x.....x.x, xxxx....xxxx, x...x...x...x, xx..xx..xx..xx, x.x.x.x.x.x.x.x, xxxxxxxxxxxxxxxx]
.
As you can see the expression is already much simpler. The last solution expresses a sequence of 65535 moves!
But what is the pattern behind these x
's and .
's? To find out you need to write the elements on separate lines and space them a bit. You get:
x
x x
x . x
x x x x
x . . . x
x x . . x x
x . x . x . x
x x x x x x x x
x . . . . . . . x
x x . . . . . . x x
x . x . . . . . x . x
x x x x . . . . x x x x
x . . . x . . . x . . . x
x x . . x x . . x x . . x x
x . x . x . x . x . x . x . x
x x x x x x x x x x x x x x x x
Amazingly, hidden in the solution there is a Sierpiński triangle.
So my solution for $n=2^k$ is: produce a Sierpiński triangle of level $k$ and execute the moves using the recursive execution rule given earlier.
The explanation of why it works, as explained by OHO's, is that the bottom part of the triangle explores all $2^{n \over 2}$ combinations of positions without changing the parity of opposing buttons (the bottom left and buttom right triangles are exactly half a turn apart), and the top half explores all $2^{n \over 2}$ possibilities for these parities. The bottom part is executed after every move in the top part, so, the two combined explore all $2^n$ positions.