#include "encoder.h"
#include "encoderlib.h"
#include <bits/stdc++.h>
using namespace std;
string convert(int num){
string s = "";
for(int j = 7; j >= 0; j--){
if(num >= pow(2, j)){
num -= pow(2, j);
s += '1';
}
else s += '0';
}
return s;
}
void encode(int N, int M[])
{
int n = N;
// break each number into 8-byte strings
string all_nums;
for(int i = 0; i < n; i++) all_nums += convert(M[i]);
for(int i = 0; i < all_nums.size() - 1; i += 2){
int val = 0;
if(all_nums[i] == '1') val += 1;
if(all_nums[i + 1] == '1') val += 2;
for(int j = 0; j < val; j++){
send((i / 2));
}
}
}
#include "decoder.h"
#include "decoderlib.h"
#include <bits/stdc++.h>
using namespace std;
int convert(string& s, int l, int r){
int res = 0;
for(int i = l; i <= r; i++){
if(s[i] == '1') res += pow(2, (i - l));
}
return res;
}
void decode(int N, int L, int X[])
{
int n = N;
vector<string> ref = {"00", "10", "01", "11"};
map<int, int> freq;
for(int i = 0; i < L; i++){
freq[X[i]]++;
}
string s;
for(int i = 0; i < N; i++){
s += "00000000";
}
for(auto e : freq){
int ind = e.first * 2;
string replace = ref[e.second];
s[ind] = replace[0];
s[ind + 1] = replace[1];
}
// convert back to decimal
for(int i = 0; i < n; i++){
output(convert(s, i * 8, (i + 1) * 8 - 1));
}
}