% % This problem was published as % Decision Management Community Challenge Sep-2019 "Crack the code" % at https://dmcommunity.org/challenge/challenge-sep-2019 % % Crack a 3 digit code based on these hints: % 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. % % % Below is a CP (Constraint Programming) solution, using % the open-source ECLiPSe system (http://eclipseclp.org) % % Running this code: % $ eclipse -f dmc_challenge_2019_sep.ecl -e solve % % Result: % [0, 5, 2] % % % Author: Joachim Schimpf, 2019 % Creative Commons Attribution 4.0 International License. % :- lib(ic). :- lib(ic_global). solve :- model(Xs), labeling(Xs), writeln(Xs). model(Xs) :- length(Xs, 3), Xs #:: 0..9, clue(Xs, [6,8,2], 1, 1), clue(Xs, [6,4,5], 1, 0), clue(Xs, [2,0,6], 2, 0), clue(Xs, [7,3,8], 0, 0), clue(Xs, [7,8,0], 1, 0). clue(Xs, Ns, NOcc, NHit) :- ( foreach(N,Ns), foreach(Occ,Occs), param(Xs) do occurrences(N, Xs, Occ) ), NOcc #= sum(Occs), ( foreach(X,Xs), foreach(N,Ns), foreach(Hit,Hits) do Hit #= (X #= N) ), NHit #= sum(Hits).