# misc/subtraction

### Writeup by Aryan

This is the source code we are provided with.

```python
import random

n = 696969

a = []
for i in range(n):
    a.append(random.randint(0, n))
    a[i] -= a[i] % 2

print(' '.join(list(map(str, a))))

for turns in range(20):
    c = int(input())
    for i in range(n):
        a[i] = abs(c - a[i])

    if len(set(a)) == 1:
        print(open('/flag.txt', 'r').read())
        break
```

If we keep subtracting by the mean of the dataset, the numbers will eventually all equal each other. The proof is left as an exercise to the reader.

```python
import socket
import statistics

def main():
    host = 'challs.n00bzunit3d.xyz'
    port = 10029

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))
        data = b''
        while b'\n' not in data:
            data += s.recv(4096)
        data = data.decode().strip()
        numbers = list(map(int, data.split()))
        print(f"Received numbers: {len(numbers)}")

        for turn in range(20):
            print(f"Iteration {turn + 1}:")
            c = int(statistics.mean(numbers))
            s.sendall(f"{c}\n".encode())
            print(f"Sent value of c: {c}")
            numbers = list(set(abs(c - x) for x in numbers))
            print(f"Amount of numbers: {len(numbers)}")
            
            if len(set(numbers)) == 1:
                response = b''
                while b'\n' not in response:
                    response += s.recv(4096)
                response = response.decode().strip()
                print(response)

if __name__ == "__main__":
    main()

```

Flag: `n00bz{1_sh0uld_t34ch_my_br0th3r_logs_e18be9bd2874}`


---

# 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://centinels.gitbook.io/home/writeups/n00bzctf/misc-subtraction.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.
