cryptarithmetic puzzle - Need assistance with similar More Money code for Prolog -
every letter below in puzzle uniquely represent 1 of 10 digits in 0, 1, …, 9. no 2 letters represent same digit. each word below in puzzle, first letter not 0.
ex: shine - == knit
all i've got code this...
:- lib(ic). exampleone(list) :- list = [s, h, i, n, e, t, a, k], list :: 0..9, diff_list(list), (10000*s - 1000*h - 100*i - 10*n - e) - (1000*t - 100*h - 10*a - n) $= (1000*k - 100*n - 10*i - t), s $\= 0, t $\= 0, k $\= 0, shallow_backtrack(list). shallow_backtrack(list) :- ( foreach(var, list) once(indomain(var)) ). diff_list(list) :- ( fromto(list, [x|tail], tail, []) ( fromto(y, tail, param(x) x $\= y ) ). comparelists(list) :- length(list, n), ( foreach(input1, list), count(i, 1, n), param(n, list) ( foreach(input2, list), count(j, 1, n), param(list, input1, i, n) ( ( $\= j, input1 $\= input2 ) -> true; fail ) ) ).
i'm kinda stuck on part. when ran code without comparelists(list) function, result gives me "no". when added comparelists(list) function, answer still gives me "no". wondering if "no" right answer or did write wrong in code? appreciated. thanks!
thanks!
almost minuses in model (except one) must pluses. if there minus sign in 'shine - than', shine still (10000*s + 1000*h + 100*i + 10*n + e) - it's simple math.
also no need reinvent standard predicates alldifferent
(named diff_list
in code) , labeling
(named shallow_backtrack
in code). not program has lot of unneeded code, standard predicates more efficient in many situations , more flexible.
here complete corrected program uses standard ic library predicates:
:- lib(ic). exampleone(list) :- list = [s, h, i, n, e, t, a, k], list :: 0..9, alldifferent(list), (10000*s + 1000*h + 100*i + 10*n + e) - (1000*t + 100*h + 10*a + n) $= (1000*k + 100*n + 10*i + t), s $\= 0, t $\= 0, k $\= 0, labeling(list).
Comments
Post a Comment