Lock Challenge in Prolog

prolog logic puzzles
write a program to solve 8 queens problem using prolog with output
prolog examples
zebra puzzle prolog
8 queen problem prolog github
swish prolog
swish prolog tutorial
ask in prolog

I just started learning prolog and I'm stuck trying to solve this puzzle :

I tried to add some rules like this example http://swish.swi-prolog.org/example/houses_puzzle.pl but I couldn't come up with a solution.

What I tried so far:

% Render the houses term as a nice table.
:- use_rendering(table,
         [header(h('N1', 'N2', 'N3'))]).
numbers(Hs) :-
    length(Hs, 1),
    member(h(6,8,2), Hs),
    member(h(6,1,4), Hs),
    member(h(2,0,6), Hs),
    member(h(7,3,8), Hs),
    member(h(7,8,0), Hs),
    correct_and_placed(6, 8, 2, Hs).

correct_and_place(A, B, C, R).

But I don't even know how to write a rule that can check if a number is correct and on the right place.

To the existing answers, I would like to add a version using CLP(FD) constraints.

The two building blocks I shall use are num_correct/3 and num_well_placed/3.

First, num_correct/3, relating two lists of integers to the number of common elements:

num_correct(Vs, Ns, Num) :-
        foldl(num_correct_(Vs), Ns, 0, Num).

num_correct_(Vs, Num, N0, N) :-
        foldl(eq_disjunction(Num), Vs, 0, Disjunction),
        Disjunction #<==> T,
        N #= N0 + T.

eq_disjunction(N, V, D0, D0 #\/ (N #= V)).

Sample query:

?- num_correct([1,2,3], [3,5], Num).
Num = 1.

As is characteristic for pure relations, this also works for much more general queries, for example:

?- num_correct([A], [B], Num).
Num in 0..1.

Second, I use num_well_placed/3, which relates two lists of integers to the number of indices where corresponding elements are equal:

num_well_placed(Vs, Ns, Num) :-
        maplist(num_well_placed_, Vs, Ns, Bs),
        sum(Bs, #=, Num).

num_well_placed_(V, N, B) :- (V #= N) #<==> B.

Again, a sample query and answer:

?- num_well_placed([8,3,4], [0,3,4], Num).
Num = 2.

The following predicate simply combines these two:

num_correct_placed(Vs, Hs, C, P) :-
        num_correct(Vs, Hs, C),
        num_well_placed(Vs, Hs, P).

Thus, the whole puzzle can be formulated as follows:

lock(Vs) :-
        Vs = [_,_,_],
        Vs ins 0..9,
        num_correct_placed(Vs, [6,8,2], 1, 1),
        num_correct_placed(Vs, [6,1,4], 1, 0),
        num_correct_placed(Vs, [2,0,6], 2, 0),
        num_correct_placed(Vs, [7,3,8], 0, 0),
        num_correct_placed(Vs, [7,8,0], 1, 0).

No search at all is required in this case:

?- lock(Vs).
Vs = [0, 4, 2].

Moreover, if I generalize away the last hint, i.e., if I write:

lock(Vs) :-
        Vs = [_,_,_],
        Vs ins 0..9,
        num_correct_placed(Vs, [6,8,2], 1, 1),
        num_correct_placed(Vs, [6,1,4], 1, 0),
        num_correct_placed(Vs, [2,0,6], 2, 0),
        num_correct_placed(Vs, [7,3,8], 0, 0),
        * num_correct_placed(Vs, [7,8,0], 1, 0).

then the unique solution can still be determined without search:

?- lock(Vs).
Vs = [0, 4, 2].

In fact, I can even also take away the penultimate hint:

lock(Vs) :-
        Vs = [_,_,_],
        Vs ins 0..9,
        num_correct_placed(Vs, [6,8,2], 1, 1),
        num_correct_placed(Vs, [6,1,4], 1, 0),
        num_correct_placed(Vs, [2,0,6], 2, 0),
        * num_correct_placed(Vs, [7,3,8], 0, 0),
        * num_correct_placed(Vs, [7,8,0], 1, 0).

and still the solution is unique, although I now have to use label/1 to find it:

?- lock(Vs), label(Vs).
Vs = [0, 4, 2] ;

Prolog Tutorial -- 2.11, 2.11 Chess queens challenge puzzle. The challenge is to set N queens on an NxN grid so that no queen can "take" any other queen. Queens can move  But opening the lock isn’t impossible—it just takes some logic. So let’s solve the problem together. The first clue reads, “682: One digit is right and in its place.”We can’t do

I hope there are better ways but...

You can implement "one number is correct and well placed" as follows

oneRightPlace(X, Y, Z, X, S2, S3) :-
  \+ member(Y, [S2, S3]),
  \+ member(Z, [S2, S3]).

oneRightPlace(X, Y, Z, S1, Y, S3) :-
  \+ member(X, [S1, S3]),
  \+ member(Z, [S1, S3]).

oneRightPlace(X, Y, Z, S1, S2, Z) :-
  \+ member(X, [S1, S2]),
  \+ member(Y, [S1, S2]).

For "one number is correct but wrong placed, you can use

oneWrongPlace(X, Y, Z, S1, S2, S3) :-
  member(X, [S2, S3]),
  \+ member(Y, [S1, S2, S3]),
  \+ member(Z, [S1, S2, S3]).

oneWrongPlace(X, Y, Z, S1, S2, S3) :-
  member(Y, [S1, S3]),
  \+ member(X, [S1, S2, S3]),
  \+ member(Z, [S1, S2, S3]).

oneWrongPlace(X, Y, Z, S1, S2, S3) :-
  member(Z, [S1, S2]),
  \+ member(X, [S1, S2, S3]),
  \+ member(Y, [S1, S2, S3]).

For "two number are correct but wrong placed", you can write

twoWrongPlace(X, Y, Z, S1, S2, S3) :-
  member(X, [S2, S3]),
  member(Y, [S1, S3]),
  \+ member(Z, [S1, S2, S3]).

twoWrongPlace(X, Y, Z, S1, S2, S3) :-
  member(X, [S2, S3]),
  member(Z, [S1, S2]),
  \+ member(Y, [S1, S2, S3]).

twoWrongPlace(X, Y, Z, S1, S2, S3) :-
  member(Y, [S1, S3]),
  member(Z, [S1, S2]),
  \+ member(X, [S1, S2, S3]).

And, for "nothing is correct", become simply

zeroPlace(X, Y, Z, S1, S2, S3) :-
  \+ member(X, [S1, S2, S3]),
  \+ member(Y, [S1, S2, S3]),
  \+ member(Z, [S1, S2, S3]).

Now you can put all togheter and write

  member(S1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
  member(S2, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
  member(S3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
  oneRightPlace(6, 8, 2, S1, S2, S3),
  oneWrongPlace(6, 1, 4, S1, S2, S3),
  twoWrongPlace(2, 0, 6, S1, S2, S3),
  zeroPlace(7, 3, 8, S1, S2, S3),
  oneWrongPlace(7, 8, 0, S1, S2, S3).

obtaining (in S1, S2 and S3) the right solution.

The preceding examples are written without the use of clp(fd), that I don't know well but that (I suppose) can semplify a lot.

Logic Puzzles with Prolog, Premise: You are on an island where every inhabitant is either a knight or a knave. Knights always tell the truth, and knaves always lie. Using Prolog and its CLP(B)  Today I tried the shift lock challenge were one round I use shift lock and the other I don’t! Also ft. My very kind fans that joined in with me! :3.

So, as with all problems in this vein, I tend to be tempted to write a generic solver as opposed to a specific solver. Borrowing from a mastermind implementation I wrote a while ago (spawned by a question on here) I present the following:

compare(List,Reference,RightPlace,WrongPlace) takes two lists, and unifies RightPlace with the number of elements of the first list that appear at the same point in the second list, and WrongPlace with the number of elements that appear at a different point in the second list (where a duplicate element is only counted if it is duplicated in both lists). It does this using...

right_place(List,Reference,RightPlace) which wraps an accumulator and consumes elements from the head of each list, incrementing where they match, and...

any_match(List,Reference,Matches) which wraps an accumulator that consumes the head of the List list, and selects it from the Reference list where possible, incrementing where this occurs.

WrongPlace is then the number of RightPlace elements subtracted from the number of Matches.

Finally, find_solutions(Soln) creates a list of elements in the domain (0-9) using clpfd, then maps indomain to create the combinations. Each combination is then compared with each hint using forall, to ensure that all hint constraints are satisfied. Put it all together with the hints, and you get:

:- use_module(library(clpfd)).

compare(List,Reference,RightPlace,WrongPlace) :-
    WrongPlace #= Matches - RightPlace.

right_place(List,Reference,RightPlace) :-

right_place([Match|List],[Match|Reference],Accumulator,RightPlace) :-
    NewAccumulator is Accumulator + 1,
right_place([A|List],[B|Reference],Accumulator,RightPlace) :-
    A \= B,

any_match(List,Reference,Matches) :-

any_match([Match|List],Reference,Accumulator,Matches) :-
    NewAccumulator is Accumulator + 1,
any_match([Match|List],Reference,Accumulator,Matches) :-

find_solutions(Soln) :-
    Soln ins 0..9,


EURO-PAR '95: Parallel Processing: First International EURO-PAR , Compiler Optimizations in Reform Prolog: Experiments on the KSR-1 analyses of Reform Prolog and evaluate their effectiveness in eliminating suspension and locking 1 Introduction An important challenge in parallel processing is to keep​  In today's video I tried out a challenge that seemed not that hard, but proved me wrong. In this challenge I turned off shift lock and saw how far I could get in the towers. This was lots of fun

Not sure I need to explain this much. You generate all possibilities, and then you code the constraints.

code(A,B,C) :-
  ( A = 6 ; B = 8 ; C = 2 ),
  ( A = 1, \+ member(B,[6,4]), \+ member(C,[6,4])
  ; A = 4, \+ member(B,[6,1]), \+ member(C,[6,1])
  ; B = 6, \+ member(A,[1,4]), \+ member(C,[1,4])
  ; B = 4, \+ member(A,[6,1]), \+ member(C,[6,1])
  ; C = 6, \+ member(B,[1,4]), \+ member(A,[1,4])
  ; C = 1, \+ member(B,[6,4]), \+ member(A,[6,4]) ),
  ( A = 0, B = 2, C \= 6
  ; A = 0, B = 6, C \= 2
  ; A = 6, B = 2, C \= 0
  ; B = 2, C = 0, A \= 6
  ; B = 6, C = 2, A \= 0
  ; B = 6, C = 0, A \= 2
  ; C = 2, A = 0, B \= 6
  ; C = 2, A = 6, B \= 0
  ; C = 0, A = 6, B \= 2 ),
  \+ member(A,[7,3,8]), \+ member(B,[7,3,8]), \+ member(C,[7,3,8]),
  ( A = 8, \+ member(B,[7,0]), \+ member(C,[7,0])
  ; A = 0, \+ member(B,[7,8]), \+ member(C,[7,8])
  ; B = 7, \+ member(A,[8,0]), \+ member(C,[8,0])
  ; B = 0, \+ member(A,[7,8]), \+ member(C,[7,8])
  ; C = 7, \+ member(B,[8,0]), \+ member(A,[8,0])
  ; C = 8, \+ member(B,[7,0]), \+ member(A,[7,0]) ).

Here is the result:

| ?- code(A,B,C).
A = 0,
B = 4,
C = 2 ? ;

Current Issues in Computing and Philosophy, very problematic.7In an intelligent reaction to this transfer challenge, Black et al​. Finally, as to deductive reasoning, Prolog locks those who program in it into  Prolog purists might prefer this new version because it does not produce the side effects of assert and retract. Exercise 2.19.3 Write a program that can detect and eliminate redundant steps in a plan. Exercise 2.19.1 is especially interesting. Note that validity is supposed to capture the meaning of "physically possible".

Lock-free atom garbage collection for multithreaded Prolog, Lock-free atom garbage collection for multithreaded Prolog - Volume 16 Special Issue - JAN WIELEMAKER, KERI HARRIS. Working with Prolog lists. A list is either empty or it is composed of a first element (head) and a tail, which is a list itself. In Prolog we represent the empty list by the atom [] and a non-empty list by a term [H|T] where H denotes the head and T denotes the tail. P01 (*) Find the last element of a list. Example:?- my_last(X,[a,b,c,d]). X = d

Multi‐dimensional lock‐free arrays for multithreaded mode‐directed , However, combining threads and tabling in a Prolog system introduces several new challenges at the underlying engine. For example, in a  Time to play the Chastity Challenge! This will determine how long you have to remain locked in chastity. Press RANDOM right there once - or at at the top of the page - and the image that comes up is how long you have to stay locked up.

PROLOGUE ROUND 25 GUIDE [WW2 ZOMBIES] [MOUNTAINEER , In particular the simulation is lock-step. sequential programming languages, e.g., Modula-2 (GMr], Prolog [Bol-Boã], Prolog III (BS), I do not know any distributed parallel algorithm that presents a conceptual challenge for EA formalization. PROLOG, Episode 1 Lock Down di LINE WEBTOON. gadis yang tidak bisa pergi dari ranjang. skip content. WEBTOON. Lock Down PROLOG. Episode sebelumnya #1 Episode selanjutnya.