Bending Spoons interview question

Logical reasoning test, programming exercise.