-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights-out-general.lp
31 lines (25 loc) · 987 Bytes
/
lights-out-general.lp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
% Run this by using:
% clingo lights-out-general.lp sample-problem.lp
% Definitions of problem topology
coord(1..N, 1..M) :- dim(N, M).
adjacent(X, Y, A, B) :-
|X-A| + |Y-B| <= 1,
coord(X, Y),
coord(A, B).
% Each cell needs to be selected at most once in the solution.
% This is represented by the choice of action/2 in model.
0 {action(X, Y)} 1 :- coord(X, Y).
% Penalize each action, i.e. minimize the total number of solution steps.
:~ action(X, Y). [1@1,(X,Y)]
% An action must be performed odd number of times in neighborhood of each cell in order
% to switch invert it. In other words it cannot be performed even number of times.
:- C = #count{ (A,B) : adjacent(A, B, X, Y), action(A, B)},
C \ 2 = 1,
coord(X, Y),
not x(X, Y).
% Cells marked by x/2, on the other hand, should keep their values.
% So an action cannot be performed odd number of times.
:- C = #count{ (A,B) : adjacent(A, B, X, Y), action(A, B)},
C \ 2 = 0,
x(X, Y).
#show action/2.