Wow! Nice solution. I have been thinking about it for a while and I still have two open quiestions:

Does it work in any case?

Does it find the optimal solution (minimum flips)?

The first one seems to be true (at least the cases tested) but I can't see why. I am not so sure about the second one.
Can you give me any clue about them?

Due to my background I would go for a graph search based solution. The nodes of the graph are the state of the row of pancakes and the neighbors of a node are those states reachable by a flip action.
It is not the fastest one, but it is exhaustive and the solution is optimal.

Wow! Nice solution. I have been thinking about it for a while and I still have two open quiestions:

The first one seems to be true (at least the cases tested) but I can't see why. I am not so sure about the second one.

Can you give me any clue about them?

Due to my background I would go for a graph search based solution. The nodes of the graph are the state of the row of pancakes and the neighbors of a node are those states reachable by a flip action.

It is not the fastest one, but it is exhaustive and the solution is optimal.

I cannot show you the mathematical explanation but I have some hints.

Let's take the example of the post but reversed:

These are exactly the same flips made above.

I think the key point here is that you should never touch

happypancakes, unless forced to do so because of a nearestunhappyone.