1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <bits/time.h>
#include <stdbool.h>
#include <stdio.h>
static PyObject *spy_easy(PyObject *self, PyObject *args);
static PyObject *spy_hard(PyObject *self, PyObject *args);
static PyMethodDef spy_methods[] = {
{ "easy", spy_easy, METH_VARARGS, "Play Spy Game (Easy)" },
{ "hard", spy_hard, METH_VARARGS, "Play Spy Game (Hard)" },
{ 0 }
};
static struct PyModuleDef spy_module = {
PyModuleDef_HEAD_INIT,
"spy",
"Spy Game Module",
-1,
spy_methods,
};
PyObject *
spy_game(PyObject *self, PyObject *args, unsigned count)
{
char user_input[256];
uint8_t numbers[count];
struct timespec start, end;
uint64_t start_ns, end_ns;
uint64_t total_ns, total_ok;
size_t swap1, swap2;
size_t swap1_in, swap2_in;
size_t i, k;
bool ok;
if (!PyArg_ParseTuple(args, ""))
return NULL;
printf("Ready? ");
getchar();
printf("\n");
total_ok = 0;
total_ns = 0;
for (i = 0; i < 8; i++) {
for (k = 0; k < count; k++)
numbers[k] = k;
swap1 = rand() % count;
swap2 = (swap1 + 1 + rand() % (count - 1)) % count;
numbers[swap1] ^= numbers[swap2];
numbers[swap2] ^= numbers[swap1];
numbers[swap1] ^= numbers[swap2];
printf("Before: ");
for (k = 0; k < count; k++)
printf("%u ", numbers[k]);
printf("\n");
clock_gettime(CLOCK_REALTIME, &start);
start_ns = start.tv_nsec + start.tv_sec * 1e9;
fflush(stdin);
printf("Index 1: ");
(void)fgets(user_input, sizeof(user_input), stdin);
swap1_in = strtoull(user_input, NULL, 10);
printf("Index 2: ");
(void)fgets(user_input, sizeof(user_input), stdin);
swap2_in = strtoull(user_input, NULL, 10);
clock_gettime(CLOCK_REALTIME, &end);
end_ns = end.tv_nsec + end.tv_sec * 1e9;
numbers[swap1_in] ^= numbers[swap2_in];
numbers[swap2_in] ^= numbers[swap1_in];
numbers[swap1_in] ^= numbers[swap2_in];
printf("After: ");
for (k = 0; k < count; k++)
printf("%u ", numbers[k]);
printf("\n");
ok = (swap1_in == swap1 && swap2_in == swap2)
|| (swap1_in == swap2 && swap2_in == swap1);
printf("You answered %s in %lu nanoseconds!\n",
ok ? "correctly" : "incorrectly",
end_ns - start_ns);
printf("\n");
total_ok += ok;
total_ns += end_ns - start_ns;
}
if (count == 256 && total_ok == 5 && total_ns < 1000) {
return PyUnicode_FromString("REWARD");
} else if (count == 10 && total_ok == 5 && total_ns < 1e9 * 60) {
return PyUnicode_FromString("MOTIVATE");
}
return PyUnicode_FromString("SLOW");
}
PyObject *
spy_easy(PyObject *self, PyObject *args)
{
return spy_game(self, args, 10);
}
PyObject *
spy_hard(PyObject *self, PyObject *args)
{
return spy_game(self, args, 256);
}
PyMODINIT_FUNC PyInit_spy(void) {
setvbuf(stdout, NULL, _IONBF, 0);
return PyModule_Create(&spy_module);
};
|