#include "message.h"
#include <iostream>
#define all(a) (a).begin(), (a).end()
#define popcount(x) __builtin_popcountll(x)
using namespace std;
void send_message(vector<bool> message, vector<bool> C) {
if (message.size() <= 64) {
for (auto x : message) {
send_packet(vector<bool>(31, x));
}
return;
}
message.push_back(1);
while (message.size() < 1035)
message.push_back(0);
int f0 = 0; while (C[f0] == 1) f0++;
for (int i = 0; i < 4; i++) {
send_packet(vector<bool>(31, (f0 >> i) & 1));
}
int c_ptr = 0;
for (int i = 0, ptr = 0; i < 69; i++) {
vector<bool> packet(31);
if (c_ptr < 31)
packet[f0] = C[c_ptr++];
for (int j = f0 + 1; j < 31; j++) {
if (C[j] == 0) {
packet[j] = message[ptr++];
}
}
send_packet(packet);
}
}
int get_maj(vector<bool> C) {
int cnt[2] = {0, 0};
for (auto x : C)
cnt[x]++;
// cout<<(cnt[0] < cnt[1] ? 0 : 1)<<'\n';
return cnt[0] > cnt[1] ? 0 : 1;
}
vector<bool> receive_message(vector<vector<bool>> packets) {
if (packets.size() <= 64) {
vector<bool> res;
for (auto p : packets)
res.push_back(get_maj(p));
return res;
}
int f0 = 0;
for (int i = 0; i < 4; i++) {
f0 |= (get_maj(packets[i])) << i;
}
vector<int> pos = {};
for (int i = 4; i < 4 + 31; i++) {
if (packets[i][f0] == 0) {
pos.push_back(i - 4);
}
}
vector<bool> message;
for (int i = 4; i < 73; i++) {
for (int j : pos) if (j != f0) {
message.push_back(packets[i][j]);
}
}
int x = 0;
while (x == 0) {
x = message.back();
message.pop_back();
}
return message;
}