/* Number lock - 5 digit (Mastermind like) puzzle in Picat. https://puzzling.stackexchange.com/questions/97032/5-digit-puzzle-code-looking-for-solution """ Can somebody help me solve this, or can you teach me how? 4 7 2 9 1 - One number is correct but not in right position 9 4 6 8 7 - One number is correct but not in right position 3 1 8 7 2 - Two numbers are correct but only one is in right position 1 5 7 3 9 - Two numbers are correct and both in right position """ Also see: https://g-ar.github.io/posts/solving-mastermind-like-problems-using-z3-theorem-prover/ This is the same type of puzzle as in number_lock.pi and number_lock2.pi There are two solutions: [1,5,8,0,0] [6,5,0,3,2] If we assume that the digits should be distinct there is only one solution: [6,5,0,3,2] This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> data(1,Data), N = length(Data[1,1]), % number of digits X = new_list(N), X :: 0..9, % all_different(X), % Should we assume distinctness? foreach([Digits,NumCorrectPosition,NumCorrectNumber] in Data) check(Digits,X,NumCorrectPosition,NumCorrectNumber) end, solve($[],X), println(X), fail, nl. go => true. % % How many % pos: correct values and positions % val: correct values (regardless if there are correct position or not) check(A, B, Pos, Val) => N = A.len, % number of entries in correct position (and correct values) sum([A[J] #= B[J] : J in 1..N]) #= Pos, % number of entries which has correct values (regardless if there are in correct position or not) sum([A[J] #= B[K] : J in 1..N, K in 1..N ]) #= Val. data(1,Data) => Data = [ [[4,7,2,9,1],0,1], % - One number is correct but not in right position [[9,4,6,8,7],0,1], % - One number is correct but not in right position [[3,1,8,7,2],1,2], % - Two numbers are correct but only one is in right position [[1,5,7,3,9],2,2] % - Two numbers are correct and both in right position ].