% Chinese Rings puzzle

:- ensure_loaded(solver).

initial(rings([1, 1, 1, 1, 1, 1])).
final(rings([0, 0, 0, 0, 0, 0])).

% The leftmost ring can be put on or off at will.

rings([1 | R]) => rings([0 | R]).
rings([0 | R]) => rings([1 | R]).

% The ring k from the left can be put on or off only if
% the ring to its left is on and all rings to the left
% of that are off.

rings(S1) => rings(S2) :-
  divide([], S1, L1, R1),
  change(R1, R2),
  restore(L1, R2, S2).

change([1, 1 | R], [1, 0 | R]).
change([1, 0 | R], [1, 1 | R]).

divide(L, [1 | R], L, [1 | R]).
divide(L, [0 | R], L1, R1) :-
  divide([0 | L], R, L1, R1).

restore([], L, L).
restore([A | L], R, S) :-
  restore(L, [A | R], S).

