#include "message.h"
#include <bits/stdc++.h>
using namespace std;
int L = 71;
void send_message(std::vector<bool> M, std::vector<bool> C) {
vector<vector<bool>> R(L, vector<bool>(31, 0));
vector<int> good_bits;
for (int i=0; i<31; i++) {
if (C[i]==0) good_bits.push_back(i);
}
int FG = good_bits[0];
for (int i=0; i<5; i++) {
int bi = (FG&(1<<i))>0;
fill(R[i].begin(), R[i].end(), bi);
}
for (int i=5; i<36; i++) {
R[i][FG] = C[i-5];
}
//pad the complement of the last bit of the message out til 1025
int last_M = M.back();
while (M.size()<1025) {
M.push_back(1-last_M);
}
vector<pair<int,int>> message_bits;
for (int i=36; i<L; i++) message_bits.push_back({i, FG});
for (int i:good_bits) {
for (int j=5; j<L; j++) {
if (i!=FG && message_bits.size()<1025) {
message_bits.push_back({j, i});
}
}
}
for (int i=0; i<1025; i++) {
R[message_bits[i].first][message_bits[i].second] = M[i];
}
for (auto A:R) {
send_packet(A);
}
}
std::vector<bool> receive_message(std::vector<std::vector<bool>> R) {
int FG = 0;
for (int i=0; i<5; i++) {
int maj = 0;
for (auto j:R[i]) {
if (j==1) maj++;
else maj--;
}
if (maj>0) FG |= 1LL<<i;
}
//cout << "CALCULATED FG " << FG << endl;
vector<int> good_bits;
for (int i=5; i<36; i++) {
if (R[i][FG]==0) {
good_bits.push_back(i-5);
}
}
vector<pair<int,int>> message_bits;
for (int i=36; i<L; i++) message_bits.push_back({i, FG});
for (int i:good_bits) {
for (int j=5; j<L; j++) {
if (i!=FG && message_bits.size()<1025) {
message_bits.push_back({j, i});
}
}
}
vector<bool> res;
for (auto i:message_bits) {
res.push_back(R[i.first][i.second]);
}
while (res.size()>2 && res.back()==res[((int)res.size())-2]) {
res.pop_back();
}
assert(res.size()>=1);
res.pop_back();
return res;
}