/* Mastermind (Transum) in Picat. https://www.transum.org/Software/SW/Starter_of_the_day/starter_May27.ASP """ The teacher thinks of a four digit number made up of digits in the range 1 to 4. Each student guesses what the four digit number might be. The teacher gives one tick for each digit the student has in the right place. What number is the teacher thinking of? Name Guess Num correct -------------------------- Adam: 1241 1 Adrian: 1142 3 Kyle: 4231 1 Erik: 1113 1 Ian: 2142 3 Nathaniel 3443 1 Carlos: 3423 0 Alex: 1323 0 Bryan: 1413 0 Jagdish 3424 0 Julian: 1134 1 Sean: 4214 1 Carter: 4441 2 Hayden: 3122 2 Jeremiah 2232 1 Cole: 4332 2 Brayden 3111 1 Wyatt 4121 2 Chase 1423 0 Steven 3223 0 """ I would say 4142. Which is confirmed by the model. Note that quite a few of the hints are not needed for this model. Here's the values of X shown at each step of the loop before solve/1. x = [DV_02718_1..4,DV_02768_1..4,DV_027b8_1..4,DV_02808_1..4] x = [DV_02718_1..4,DV_02768_1..4,DV_027b8_1..4,DV_02808_1..4] x = [DV_02718_1..4,DV_02768_1..4,DV_027b8_1..4,DV_02808_1..4] x = [DV_02718_1..4,DV_02768_1..4,DV_027b8_1..4,DV_02808_1..4] x = [DV_02718_1..4,DV_02768_1..4,DV_027b8_1..4,DV_02808_1..4] x = [DV_02718_1..4,DV_02768_1..4,DV_027b8_1..4,DV_02808_1..4] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] x = [4,1,4,2] I.e. we need only 7 hints to find the solution. However, we only need 4 hints to get a unique solution when using solve/1: data = [[adam,1241,1],[adrian,1142,3],[kyle,4231,1],[erik,1113,1]] [[x = [4,1,4,2],count = 4]] This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. main => go. go ?=> X = new_list(4), X :: 1..4, data(Data1), Data = chunks_of(Data1,3), println(len=Data.len), foreach([_Name,Num1,Correct] in Data) Nums = [I.to_int : I in Num1.to_string], sum([X[I] #= Nums[I] : I in 1..4]) #= Correct, println(x=X) end, nl, println(solve), solve(X), println(x=X), fail, nl. go => true. /* Four hints suffice: c = 1 data = [[adam,1241,1]] c = 2 data = [[adam,1241,1],[adrian,1142,3]] c = 3 data = [[adam,1241,1],[adrian,1142,3],[kyle,4231,1]] c = 4 data = [[adam,1241,1],[adrian,1142,3],[kyle,4231,1],[erik,1113,1]] [[x = [4,1,4,2]] */ go2 ?=> data(Data1), Data = chunks_of(Data1,3), member(C,1..10), println(c=C), DataSlice = [[Data[I,J] : J in 1..3] : I in 1..C], println(data=DataSlice), All = findall(x=X,mastermind(DataSlice, X)), All.len == 1, println(All), nl. go2 => true. mastermind(Data, X) => X = new_list(4), X :: 1..4, foreach([_Name,Num1,Correct] in Data) Nums = [I.to_int : I in Num1.to_string], sum([X[I] #= Nums[I] : I in 1..4]) #= Correct end, solve(X). data(Data) => Data = [ adam, 1241, 1, adrian, 1142, 3, kyle, 4231, 1, erik, 1113, 1, ian, 2142, 3, nathaniel, 3443, 1, carlos, 3423, 0, alex, 1323, 0, bryan, 1413, 0, jagdish, 3424, 0, julian, 1134, 1, sean, 4214, 1, carter, 4441, 2, hayden, 3122, 2, jeremiah, 2232, 1, cole, 4332, 2, brayden, 3111, 1, wyatt, 4121, 2, chase, 1423, 0, steven, 3223, 0 ].