/* SEND+MOST=MONEY in Picat. Alphametic problem were we maximize MONEY. This version do two things: - find the maximum of MONEY - and then find all solutions for the maximum value of MONEY. Problem from the lecture notes: http://www.ict.kth.se/courses/ID2204/notes/L01.pdf Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => % % first part: find the maximum value of MONEY % LD = new_list(8), LD :: 0..9, send_most_money(LD, MONEY), println("First, find maximum value of MONEY:"), solve([$max(MONEY)], LD), println([MONEY, LD]), % % second part: find all solutions for the maximum value of MONEY % LD2 = new_list(8), LD2 :: 0..9, send_most_money(LD2, MONEY), AllSolutions = solve_all(LD2), Len = length(AllSolutions), printf("\nHere are all (%d) solutions with MONEY = %d:\n", Len, MONEY), foreach(Solution in AllSolutions) println(Solution) end. % Total number of solutions: 16 go2 => LD = new_list(8), LD :: 0..9, send_most_money(LD, _MONEY), All = solve_all(LD), println(All), println(num_sols=All.len), nl. send_most_money([S,E,N,D,M,O,T,Y], MONEY) => all_different([S,E,N,D,M,O,T,Y]), S #> 0, M #> 0, MONEY #= 10000 * M + 1000 * O + 100 * N + 10 * E + Y, 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*S + T #= MONEY.