#include "message.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
void send_message(vector<bool> M, vector<bool> C) {
vector<bool> xd = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1};
for (int i = 0; i < xd.size(); i++) {
vector<bool> p(31);
for (int j = 0; j < 31; j++) {
if (C[j]) {
p[j] = (rng() & 1);
} else {
p[j] = xd[i];
}
}
send_packet(p);
}
int S = M.size();
vector<bool> p(31);
for (int j = 0, k = 0; j < 31; j++) {
if (C[j]) {
p[j] = (rng() & 1);
} else {
p[j] = (S >> k & 1);
k++;
}
}
send_packet(p);
for (int i = 0, k = 0; i < (S + 15) / 16; i++) {
vector<bool> p(31);
for (int j = 0; j < 31; j++) {
if (C[j]) {
p[j] = (rng() & 1);
} else if (k < S) {
p[j] = M[k++];
}
}
send_packet(p);
}
}
vector<bool> receive_message(vector<vector<bool>> R) {
vector<bool> xd = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1};
vector<int> C(31, 1);
for (int i = 0; i < xd.size(); i++) {
for (int j = 0; j < 31; j++) {
if (xd[i]) {
C[j] &= R[i][j];
} else {
C[j] &= !R[i][j];
}
}
}
int i = xd.size(), S = 0;
for (int j = 0, k = 0; j < 31; j++) {
if (C[j]) {
S |= (1 << k) * R[i][j];
k++;
}
}
i++;
vector<bool> ans(S);
for (int k = 0; i < R.size(); i++) {
for (int j = 0; j < 31 && k < S; j++) {
if (C[j]) {
ans[k++] = R[i][j];
}
}
}
return ans;
}