% Encoding: 1
% Program: 
which_solver(bprolog).

solve(A) :-
	length(Q, 8),
	dom(1, Q, 0, 100),   % cspvar(d, 0, 100)
	dom(2, Q, 0, 100),   % cspvar(e, 0, 100)
	dom(3, Q, 0, 100),   % cspvar(m, 0, 100)
	dom(4, Q, 0, 100),   % cspvar(n, 0, 100)
	dom(5, Q, 0, 100),   % cspvar(o, 0, 100)
	dom(6, Q, 0, 100),   % cspvar(r, 0, 100)
	dom(7, Q, 0, 100),   % cspvar(s, 0, 100)
	dom(8, Q, 0, 100),   % cspvar(y, 0, 100)
	constr(1, Q),   % required(#=(+(+(+(+(+(+(+(+(+(+(+(+(*(s, 1000), *(e, 100)), *(n, 10)), d), *(m, 1000)), *(o, 100)), *(r, 10)), e), *(m, -10000)), *(o, -1000)), *(n, -100)), *(e, -10)), *(y, -1)), 0))
	constr(2, Q),   % required(#\=(m, 0))
	constr(3, Q),   % required(#\=(s, 0))
	labeling([], Q),
	form_result(Q, A).

dom(A, B, C, D) :-
	nth(A, B, E),
	domain([E], C, D).

vname(1,d).
vname(2,e).
vname(3,m).
vname(4,n).
vname(5,o).
vname(6,r).
vname(7,s).
vname(8,y).
% required(#=(+(+(+(+(+(+(+(+(+(+(+(+(*(s, 1000), *(e, 100)), *(n, 10)), d), *(m, 1000)), *(o, 100)), *(r, 10)), e), *(m, -10000)), *(o, -1000)), *(n, -100)), *(e, -10)), *(y, -1)), 0))
constr(1, Q) :- 
	nth(7, Q, X7),
	nth(2, Q, X2),
	nth(4, Q, X4),
	nth(1, Q, X1),
	nth(3, Q, X3),
	nth(5, Q, X5),
	nth(6, Q, X6),
	nth(2, Q, X2),
	nth(3, Q, X3),
	nth(5, Q, X5),
	nth(4, Q, X4),
	nth(2, Q, X2),
	nth(8, Q, X8),
	#=(+(+(+(+(+(+(+(+(+(+(+(+(*(X7, 1000), *(X2, 100)), *(X4, 10)), X1), *(X3, 1000)), *(X5, 100)), *(X6, 10)), X2), *(X3, -10000)), *(X5, -1000)), *(X4, -100)), *(X2, -10)), *(X8, -1)), 0).

% required(#\=(m, 0))
constr(2, Q) :- 
	nth(3, Q, X3),
	#\=(X3, 0).

% required(#\=(s, 0))
constr(3, Q) :- 
	nth(7, Q, X7),
	#\=(X7, 0).




form_result(Q,A) :-
	form_result(Q, 1, A).
form_result([],_,[]).
form_result([V|VarsTail],N,[(Name,V)|MTail]) :- 
	vname(N,Name),
	N2 is N + 1,
	form_result(VarsTail,N2,MTail).

%
% By Marcello Balduccini [102208]
%
% Copyright Eastman Kodak Company 2008  All Right Reserved.
%

%:- use_module(library(system)).
%:- use_module(library(timeout)).


batch(sicstus) :-
	use_module(library(system)),
	%nofileerrors,
	set_prolog_flag(fileerrors,off),
	nodebug,
	set_prolog_flag(unknown,fail),
	solve(S),
%	massage_solution(S,S2),
	S=S2,
%	\+ skip_solution(S2),
	\+ solution_exists(S2),
	output_assignments(S),
	halt.


batch(bprolog) :-
	%nofileerrors,
	%set_prolog_flag(fileerrors,off),
	set_prolog_flag(debug,off),
	set_prolog_flag(unknown,fail),
	solve(S),
%	massage_solution(S,S2),
	S=S2,
%	\+ skip_solution(S2),
	\+ solution_exists(S2),
	output_assignments(S),
	halt.

%
% Additional equalities generated by clprq
% must be turned into (A,B) expressions
% to have a match with skip_solution/1.
%
solution_exists(S) :-
	skip_solution(E),
	term_to_string(S,X),
	term_to_string(E,X).

term_to_string(A,B) :-
	which_solver(bprolog),
	term2string(A,B).

term_to_string(A,B) :-
	which_solver(sicstus3),
	write_to_chars(A,B).

term_to_string(A,B) :-
	which_solver(sicstus4),
	write_to_codes(A,B).

% [marcy 062711] Apparently no longer needed.
%
massage_solution([],[]).
massage_solution([A|ATail],[B|BTail]) :-
	A =.. [F|Args],
	F = '=',
	!,
	B =.. [','|Args],
	massage_solution(ATail,BTail).
massage_solution([A|ATail],[A|BTail]) :-
	massage_solution(ATail,BTail).

batch(_) :-  % batch must always succeed.
        write('++failed'),nl,
        halt. 

batch_to(bprolog,Timeout_Millisec) :-
	time_out(batch(bprolog), Timeout_Millisec, success),
	halt.

batch_to(sicstus,Timeout_Millisec) :-
	use_module(library(timeout)),
	time_out(user:batch(sicstus), Timeout_Millisec, success),
	halt.

batch_to(_,_) :-  % batch must always succeed.
        write('++failed (TIMEOUT)'),nl,
        halt.

output_assignments([(ASPName,Var)|Tail]) :-
	!,
	write('++'),writeq(ASPName=Var),nl,
	output_assignments(Tail).

output_assignments([Exp|Tail]) :-
	write('++'),writeq(Exp),nl,
	output_assignments(Tail).

output_assignments([]) :-
	write('++succeeded'), nl.



/* Wrappers for certain global constraints */
prepare_disjoint2_list([],[],[],[],[]).
%
prepare_disjoint2_list([S1|S1t],[D1|D1t],[S2|S2t],[D2|D2t],[t(S1,D1,S2,D2)|Tail]) :-
	prepare_disjoint2_list(S1t,D1t,S2t,D2t,Tail).
%
disjoint2(L1,L2,L3,L4) :-
	prepare_disjoint2_list(L1,L2,L3,L4,Rectangles),
	disjoint2(Rectangles).
%
bp_prepare_disjoint2_list([],[],[],[],[]).
%
bp_prepare_disjoint2_list([S1|S1t],[D1|D1t],[S2|S2t],[D2|D2t],[[S1,S2,D1,D2]|Tail]) :-
	bp_prepare_disjoint2_list(S1t,D1t,S2t,D2t,Tail).
%
bp_disjoint2(L1,L2,L3,L4) :-
	bp_prepare_disjoint2_list(L1,L2,L3,L4,Rectangles),
	diffn(Rectangles).

get_tid(TID) :-
	curr_tid(TID),
	!.
get_tid(0).

set_tid(TID) :-
	retractall(curr_tid(_)),
	assert(curr_tid(TID)).

prepare_cumulative_list([],[],[],[]).
prepare_cumulative_list([S|St],[D|Dt],[R|Rt],[task(S,D,Q,R,TID)|Tail]) :-
	#=(Q,S+D),
	prepare_cumulative_list(St,Dt,Rt,Tail),
	get_tid(TID),
	TID2 is TID + 1,
	set_tid(TID2).
sics4_cumulative(St,Dur,Res,Max) :-
	prepare_cumulative_list(St,Dur,Res,Tasks),
	cumulative(Tasks,[limit(Max)]).

bp_cumulative([_],[_],[Res],Max) :-
	!,
	Res=<Max.
bp_cumulative(St,Dur,Res,Max) :-
	cumulative(St,Dur,Res,Max).
dors_prolog_stack_load(no).
dors_prolog_stack_save(no).
dors_prolog_stack_type(single).
:- cl('/tmp/tezcsp-8-8323328-ceygCd').
:- cl('/tmp/tezcsp-7-8323328-qqrlJe').
?- batch(bprolog).
% End of program
% Auxiliary file: /tmp/tezcsp-8-8323328-ceygCd (defined part)
% End of auxiliary file: /tmp/tezcsp-8-8323328-ceygCd
% Auxiliary file: /tmp/tezcsp-7-8323328-qqrlJe (previous CP solutions)
% End of auxiliary file: /tmp/tezcsp-7-8323328-qqrlJe
