/* CUBE+WISE=MORE in Picat. From Alex Fleischer's talk "Why puzzles are very interesting for OR consulting?" https://u.pcloud.link/publink/show?code=TdN @22:36 """ CUBE + WISE = MORE Use each number between 0 and 9 only once, to replace the letters in the sentence. The sum must be correct, you must use each number EXACTLY once. With the correct, sum, there are many solutions. But which solution has the highest value for the letters, R, O and I? Target: Maximize the value of ROI. There is only 1 correct largest value for ROI. What number (like 123) corresponds to the letters ROI? One can solve this challenge with a pencil but it takes some time. """ Also see Alex' blogpost: "Maximize ROI with Cubewise" https://alexfleischer-84755.medium.com/maximize-roi-with-cubewise-e05140b12f7f Alex' OPL code: https://github.com/AlexFleischerParis/ponderthis/blob/master/cubewise2020.mod Running this with oplrun (IBM ILOG CPLEX Optimization Studio Community Edition)_ $ oplrun cubewise2020.mod """ CUBE = 2730 WISE = 1860 MORE = 4590 ROI=958 """ 1,7,3,0,2,8,6,4,5,9] = 958 [C = 1,U = 7,B = 3,E = 0,W = 2,I = 8,S = 6,M = 4,O = 5,R = 9] Note that there are four optimal assignments: C,U,B,E,W,I,S,M,O,R [1,7,3,0,2,8,6,4,5,9] = 958 [1,7,6,0,2,8,3,4,5,9] = 958 [2,7,3,0,1,8,6,4,5,9] = 958 [2,7,6,0,1,8,3,4,5,9] = 958 The constant values of these four solutons are: U=7 E=0 I=8 <- M=4 O=5 <- R=9 <- -> ROI = 958 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp,util. main => go. go ?=> S = "CUBEWISMOR", p(X, ROI), println(X=ROI), println([C=V : {C,V} in zip(S,X)]), nl, println("All optional solutions:"), All = findall(X2=ROI,p(X2,ROI)), println(" " ++ S.map(to_string).join(',')), foreach(A in All) println(A) end, nl. go => true. /* There are 336 solutions to this puzzle */ go2 ?=> All = findall(X=ROI,p(X,ROI,true)), foreach(A in All ) println(A) end, println(num_sols=All.len), nl. go2 => true. p(X, ROI) => p(X, ROI,false). p(X, ROI,All) => X = [C,U,B,E,W,I,S,M,O,R], X :: 0..9, all_different(X), C*1000 + U*100 + B*10 + E + W*1000 + I*100 + S*10 + E #= M*1000 + O*100 + R*10 + E, ROI #= 100*R + 10*O + I, if var(ROI), All == false then solve($[max(ROI)],X) else solve(X) end.