/* Number lock problem in Picat. From Presh Talwalkar (MindYourDecisions) """ Puzzles like this have been shared with the dubious claim that "only a genius can solve" them. But they are still fun problems so let's work one out. A number lock requires a 3 digit code. Based on these hints, can you crack the code? 682 - one number is correct and in the correct position 645 - one number is correct but in the wrong position 206 - two numbers are correct but in the wrong positions 738 - nothing is correct 780 - one number is correct but in the wrong position Video: https://youtu.be/-etLb-8sHBc """ Today Moshe Vardi published a related problem (https://twitter.com/vardi/status/1164204994624741376 ) where all hints, except for the second where identical: 682 - one number is correct and in the correct position 614 - one number is correct but in the wrong position <-- This has different digits. 206 - two numbers are correct but in the wrong positions 738 - nothing is correct 780 - one number is correct but in the wrong position This model is more general than http://hakank.org/picat/number_lock.pi In go/0, we solve the all puzzles. In go2/0, we generate new hints that can replace the second hint. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. % import sat. main => go. % % Test the two given puzzles. % Note that we test for unicity of the solutions. % go => foreach(P in 1..7) println(problem=P), data(P,Data), foreach(D in Data) println(D) end, L = findall(X,number_lock(Data,X)), println("Answer"=L), nl end. % % Generate new puzzles, i.e. % generate a new variant of digits instead of the second hint: % % [[6,4,5],0,1], % - one number is correct but in the wrong position % or % [[6,1,4],0,1], % - one number is correct but in the wrong position % % This might give a new solution (X) as well. % % A new puzzle is thus: % Data = [ % [[6,8,2],1,1], % - one number is correct and in the correct position % %% We will replace these digits: % %% [[6,4,5],0,1], % - one number is correct but in the wrong position % [[2,0,6],0,2], % - two numbers are correct but in the wrong positions % [[7,3,8],0,0], % - nothing is correct % [[7,8,0],0,1] % - one number is correct but in the wrong position % ], % PLUS % one of the new hints. % % According to model, there are 248 possible new hints. % Some examples: % [digits = [1,3,3],x = [0,1,2]] % [digits = [1,3,6],x = [0,1,2]] % % [digits = [1,1,4],x = [0,4,2]] % [digits = [3,1,4],x = [0,4,2]] % % [digits = [1,1,5],x = [0,5,2]] % [digits = [3,1,5],x = [0,5,2]] % % [digits = [1,1,9],x = [0,9,2]] % [digits = [3,1,9],x = [0,9,2]] % % % Interestingly, there are only four possible values of X (solution to the puzzle): % - 012 % - 042 % - 052 % - 092 % % go2 ?=> NewDigits = new_list(3), NewDigits :: 0..9, NewPuzzles = [], L = findall([NewDigits,X],number_lock_generate(NewDigits, X)), foreach([Digits,X] in L) L2 = findall([Digits,X2],number_lock_generate(Digits,X2)), % Is it a unique solution? if L2.len == 1 then NewPuzzles := NewPuzzles ++ L2 end end, % Print all new hints foreach([D,X] in NewPuzzles) println([digits=D,x=X]) end, println(len=NewPuzzles.len), nl. % % number_lock(Data,X) % % The main problem. % number_lock(Data, X) => N = length(Data[1,1]), % number of digits X = new_list(N), X :: 0..9, foreach([Digits,NumCorrectPosition,NumCorrectNumber] in Data) check(Digits,X,NumCorrectPosition,NumCorrectNumber) end, solve($[],X). % % Generate a new hint to replace the second hint in the two puzzles. % See go2/0. % number_lock_generate(NewDigits, X) => N = 3, % number of digits % decision variables X = new_list(N), X :: 0..9, Data = [ [[6,8,2],1,1], % - one number is correct and in the correct position %% We will replace these three digits %% [[6,4,5],0,1], % - one number is correct but in the wrong position [[2,0,6],0,2], % - two numbers are correct but in the wrong positions [[7,3,8],0,0], % - nothing is correct [[7,8,0],0,1] % - one number is correct but in the wrong position ], foreach([Digits,NumCorrectPosition,NumCorrectNumber] in Data) check(Digits,X,NumCorrectPosition,NumCorrectNumber) end, check(NewDigits,X,0,1), Vars = X ++ NewDigits, solve($[],Vars). % % 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 % /* From Presh Talwalkar (MindYourDecisions) """ Puzzles like this have been shared with the dubious claim that "only a genius can solve" them. But they are still fun problems so let's work one out. A number lock requires a 3 digit code. Based on these hints, can you crack the code? 682 - one number is correct and in the correct position 645 - one number is correct but in the wrong position 206 - two numbers are correct but in the wrong positions 738 - nothing is correct 780 - one number is correct but in the wrong position Video: https://youtu.be/-etLb-8sHBc """ */ data(1,Data) => Data = [ [[6,8,2],1,1], % - one number is correct and in the correct position [[6,4,5],0,1], % - one number is correct but in the wrong position [[2,0,6],0,2], % - two numbers are correct but in the wrong positions [[7,3,8],0,0], % - nothing is correct [[7,8,0],0,1] % - one number is correct but in the wrong position ]. /* Moshe Vardi: https://twitter.com/vardi/status/1164204994624741376 682 - one number is correct and in the correct position 614 - one number is correct but in the wrong position <-- This has different digits 206 - two numbers are correct but in the wrong positions 738 - nothing is correct 780 - one number is correct but in the wrong position */ data(2,Data) => Data = [ [[6,8,2],1,1], % - one number is correct and in the correct position [[6,1,4],0,1], % - one number is correct but in the wrong position [[2,0,6],0,2], % - two numbers are correct but in the wrong positions [[7,3,8],0,0], % - nothing is correct [[7,8,0],0,1] % - one number is correct but in the wrong position ]. /* 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/ Note: It has two solutions: [1,5,8,0,0] [6,5,0,3,2] If we assume distinctness of the numbers then the answer is [6,5,0,3,2] */ % This is also modelled in number_lock_5_digits.pi data(3,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 ]. /* From https://twitter.com/sonukg4india/status/1591081634534936576 """ A padlock has a 4-digit key code. 2657: Has two correct digits but neither are in the correct place 0415: Has one correct digit but it's in the wrong place 4268: Has no correct digitss. 1749: Has two correct digits, both in the correct places. All the 4 digits in the key are different. What is the code for the padlock? """ */ data(4,Data) => Data = [ [[2,6,5,7],0,2], % - Two numbers are correct but not in right position [[0,4,1,5],0,1], % - One number is correct but not in right position [[4,2,6,8],0,0], % - No correct digit [[1,7,4,9],2,2] % - Two numbers are correct and both in right position ]. /* From https://twitter.com/sonukg4india/status/1591343310505115648 """ Can you crack the code 795 One number is correct and well placed 741 One number is correct but wrong place 463 Two numbers are correct but in wrong place 127 Nothing is correct 169 One number is correct but wrong place """ */ data(5,Data) => Data = [ [[7,9,5],1,1], [[7,4,1],0,1], [[4,6,3],0,2], [[1,2,7],0,0], [[1,6,9],0,1] ]. /* 725: 1 number is correct and placed in the wrong spot 317: 2 numbers are correct and placed in the wrong spot 793: 1 number is correct and placed in the correct spot 849: Nothing is correct 891: 1 number is correct and in the wrong spot */ data(6,Data) => Data = [ [[7,2,5],0,1], [[3,1,7],0,2], [[7,9,3],1,1], [[8,4,9],0,0], [[8,9,1],0,1] ]. /* MindYourDecisions 9285: one number is correct but in the wrong position 1937: two numbers are correct but in the wrong positions 5201: one number is correct and in the right position 6507: nothing is correct 8524: two numbers are correct but in the wrong positions Two solutions: [[3,8,4,1],[4,4,9,1]] If we should assume that all digits should be distinct: [3,8,4,1]. */ data(7,Data) => Data = [ [[9,2,8,5],0,1], [[1,9,3,7],0,2], [[5,2,0,1],1,1], [[6,5,0,7],0,0], [[8,5,2,4],0,2] ].