# lock-code

<figure><img src="/files/vtfM6FOWyHHdYIgetZ8z" alt=""><figcaption></figcaption></figure>

```python
from z3 import *

s = Solver()
code = IntVector("x", 3) # tạo 1 list gồm 3 pt thuộc kiểu int
# code = [x__0, x__1, x__2]

def GoodValueBadPlace(nums, count): # tạo ra tổng các trường hợp có thể xảy ra.
    exps = []
    for i in range(len(nums)):
        for j in range(len(code)):
            if i == j: # loại trừ trường hợp cùng vị trí
                continue
            else:
                exps.append(If(nums[i] == code[j], 1, 0)) 
    return Sum(exps) == count
# If(biểu thức, 1, 0) - Nếu biểu thức đúng thì ra 1 sai thì trả về 0

# one number is correct and well placed (1 trong 3 số đúng và đúng luôn cả vị trí)
s.add(Or(code[0] == 2, code[1] == 9, code[2] == 1))
# one number is correct but wrong placed
s.add(GoodValueBadPlace([2, 4, 5], 1))
# two numbers are correct but wrong placed
s.add(GoodValueBadPlace([4, 6, 3], 2))
# nothing is correct
s.add(And(code[0] != 5, code[1] != 7, code[2] != 9))
# one number is correct but wrong placed
s.add(GoodValueBadPlace([5, 6, 9], 1))

while s.check():
    m = s.model()
    print(m[code[0]], m[code[1]], m[code[2]])
    s.add(Or([m[code[i]] != code[i] for i in range(3)]))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/ctf/cheatsheet/python/z3py/vi-du-z3-su-dung-trong-ctf/lock-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
