/* Hidden beauty problem in Picat. http://blog.tanyakhovanova.com/2017/03/the-hidden-beauty/ """ It is rare when a word equation coincides with a number equation. Problem. A store sells letter magnets. The same letters cost the same and different letters might not cost the same. The word ONE costs 1 dollar, the word TWO costs 2 dollars, and the word ELEVEN costs 11 dollars. What is the cost of TWELVE? """ All solutions give that TWELSE costs 12. Here's the output for the CP solver (which activates some presolve constraints). max = 4 before = [o = 0,n = 0,e = 1,t = _029a0::[0 ..2],w = _029f0::[0 ..2],l = 4,v = 4] x = [0,0,1,0,2,4,4] [o = 0,n = 0,e = 1,t = 0,w = 2,l = 4,v = 4] twelve = 12 x = [0,0,1,1,1,4,4] [o = 0,n = 0,e = 1,t = 1,w = 1,l = 4,v = 4] twelve = 12 x = [0,0,1,2,0,4,4] [o = 0,n = 0,e = 1,t = 2,w = 0,l = 4,v = 4] twelve = 12 As we can see, for max=4 there are some fixed values: O=N=0 E=1 L=V=4 and both T and W can be any values as long as T+W=2. For max = 5 there are much more uncertainties and complex relationships which can be seen by the domains presolve: max = 5 before = [o = _028b0::[0 ..1],n = _02900::[0 ..1],e = _02950::[0 ..1],t = _029a0::[0 ..2],w = _029f0::[0 ..2],l = _02a40::[2 ..5],v = _02a90::[2 ..5]] Here are all solutions: x = [0,0,1,0,2,3,5] [o = 0,n = 0,e = 1,t = 0,w = 2,l = 3,v = 5] twelve = 12 x = [0,0,1,0,2,4,4] [o = 0,n = 0,e = 1,t = 0,w = 2,l = 4,v = 4] twelve = 12 x = [0,0,1,0,2,5,3] [o = 0,n = 0,e = 1,t = 0,w = 2,l = 5,v = 3] twelve = 12 x = [0,0,1,1,1,3,5] [o = 0,n = 0,e = 1,t = 1,w = 1,l = 3,v = 5] twelve = 12 x = [0,0,1,1,1,4,4] [o = 0,n = 0,e = 1,t = 1,w = 1,l = 4,v = 4] twelve = 12 x = [0,0,1,1,1,5,3] [o = 0,n = 0,e = 1,t = 1,w = 1,l = 5,v = 3] twelve = 12 x = [0,0,1,2,0,3,5] [o = 0,n = 0,e = 1,t = 2,w = 0,l = 3,v = 5] twelve = 12 x = [0,0,1,2,0,4,4] [o = 0,n = 0,e = 1,t = 2,w = 0,l = 4,v = 4] twelve = 12 x = [0,0,1,2,0,5,3] [o = 0,n = 0,e = 1,t = 2,w = 0,l = 5,v = 3] twelve = 12 x = [0,1,0,0,2,5,5] [o = 0,n = 1,e = 0,t = 0,w = 2,l = 5,v = 5] twelve = 12 x = [0,1,0,1,1,5,5] [o = 0,n = 1,e = 0,t = 1,w = 1,l = 5,v = 5] twelve = 12 x = [0,1,0,2,0,5,5] [o = 0,n = 1,e = 0,t = 2,w = 0,l = 5,v = 5] twelve = 12 The only fixed value is O=1. all other values varies as long as the constraints are satisfied ONE=1 TWO=2 ELEVEN=11 and TWELVE is 12 in all solutions. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => member(Max,2..10), println(max=Max), X = [O,N,E,T,W,L,V], X :: 0..Max, E+L+E+V+E+N #= 11, O+N+E #= 1, T+W+O #= 2, TWELVE #= T+W+E+L+V+E, % TWELVE #!= 12, % Testing println(before=[o=O,n=N,e=E,t=T,w=W,l=L,v=V]), Vars = X ++ [TWELVE], solve($[],Vars), println(x=X), println([o=O,n=N,e=E,t=T,w=W,l=L,v=V]), println(twelve=TWELVE), nl, fail, nl. go2 => X = [O,N,E,T,W,L,V], X :: 0..10, E+L+E+V+E+N #= 11, O+N+E #= 1, T+W+O #= 2, TWELVE #= T+W+E+L+V+E, MAX #= max(X), % TWELVE #!= 12, % Testing println(before=[o=O,n=N,e=E,t=T,w=W,l=L,v=V]), Vars = X ++ [TWELVE], solve($[],Vars), println(max=MAX), println(x=X), println([o=O,n=N,e=E,t=T,w=W,l=L,v=V]), println(twelve=TWELVE), nl, fail, nl.