Skip to content

Instantly share code, notes, and snippets.

@chrisamaphone
Created January 5, 2017 02:19
Show Gist options
  • Save chrisamaphone/93732506c1c190e59d9cdd59b8fd2abf to your computer and use it in GitHub Desktop.
Save chrisamaphone/93732506c1c190e59d9cdd59b8fd2abf to your computer and use it in GitHub Desktop.
asp solution to 17 puzzle
component(a2).
component(a5).
component(a6).
component(b6).
value(a2, 2).
value(a5, 5).
value(a6, 6).
value(b6, 6).
oper(plus).
oper(minus).
oper(times).
oper(div).
separate(C, C') :- component(C), component(C'), C != C'.
separate(C, arith(Oper, E1, E2)) :-
oper(Oper),
separate(C, E1),
separate(C, E2),
separate(E1, E2).
separate(X, Y) :- separate(Y, X).
expression(arith(Oper, E1, E2)) :-
oper(Oper),
separate(E1, E2).
complete(E) :-
expression(E),
not separate(_, E).
solution(E) :-
complete(E),
value(E, 17).
value(arith(plus, E1, E2), V1+V2) :-
expression(arith(Oper, E1, E2)),
value(E1, V1),
value(E2, V2).
value(arith(minus, E1, E2), V1-V2) :-
expression(arith(Oper, E1, E2)),
value(E1, V1),
value(E2, V2).
value(arith(times, E1, E2), V1*V2) :-
expression(arith(Oper, E1, E2)),
value(E1, V1),
value(E2, V2).
value(arith(div, E1, E2), V1/V2) :-
expression(arith(Oper, E1, E2)),
value(E1, V1),
value(E2, V2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment