Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <bitset>
- #include <sstream>
- #include <strings.h>
- #include <cstring>
- using namespace std;
- long dissolve(bitset<64>);
- void collect(fstream&, fstream&);
- bool resolve(fstream&, fstream&);
- bitset<64> recollect(bitset<64> p, int y, bool);
- bitset<64> side = 0;
- bitset<8> set_64 = 0;
- bitset<8> mid_rn = 0;
- int cv = 0, st = 0;
- const int dd = 8;
- int ll = 7;
- bitset<64> recollect(bitset<64> p, int y, bool check) {
- if (y == 64)
- return p;
- bitset<64> n = 0, z = p;
- n[0] = p[0];
- int t = 1;
- side[y] = check;
- while (t < y) {
- n[t] = (p[t] ^ p[t-1] == 1) ? 0 : 1;
- t++;
- }
- if (t < 64)
- n[t] = check;
- if (y%dd == 7)
- st++;
- if (y%dd == 7 && n[t-1] != set_64[st]) {
- cv++;
- for (int i = 0 ; i < cv%dd ; i++)
- st--;
- if (st < 1) {
- st = 1;
- cv = 0;
- }
- n = recollect(n,(st*8-1),~side[st*8-1]);
- }
- else if (y%dd == 7 && n[t-1] == set_64[st])
- n = recollect(n,++y,side[y]);
- cv = 0;
- int c = 1, j = 0;
- while (c - 1 < dd) {
- if (n[c*dd-1] == set_64[c-1])
- j++;
- c++;
- }
- c = 0;
- cout << j;
- cout << " H" << flush;
- if (j < dd) {
- if (st%2 == 0)
- n = recollect(n,++ll,side[ll]);
- else
- n = recollect(n,++ll,~side[ll]);
- }
- return n;
- }
- bool resolve(fstream& fin, fstream& fout) {
- stringstream q;
- q << fin.rdbuf();
- string j = q.str();
- q.str("");
- int x = 0, k = 0;
- bool check = 1;
- bitset<64> y = 0, z = 0;
- while (x + 1 < j.length()) {
- z = (int)(j[x]);
- set_64 = (int)(j[++x]);
- y = recollect(z, dd, 0);
- ll = dd;
- for (int i = 0 ; i < 64 ; i++) {
- k = (k << 1) + y[i];
- if (i%8 == 0) {
- fout << (char)k;
- k = 0;
- }
- }
- x++;
- }
- fout << (char)k;
- }
- long dissolve(bitset<64> p) {
- int j = dd, a = 63;
- bitset<64> n = 0;
- int c = 0;
- while (c != 8) {
- int t = 1;
- n = 0;
- n[0] = p[0];
- while (t < a - 1) {
- n[t] = (p[t] ^ p[t-1] == 1) ? 0 : 1;
- t++;
- }
- if (a%8 == 7) {
- mid_rn[c] = n[a];
- c++;
- }
- a--;
- p = n;
- }
- return p.to_ulong();
- }
- void collect(fstream& fin, fstream& fout) {
- stringstream k;
- k << fin.rdbuf();
- string j = k.str();
- k.str("");
- bitset<64> lk = 0;
- long long x = 0, t = 0;
- while (x < j.length()) {
- while (x < j.length() && x % dd != 0) {
- lk = (lk.to_ulong() << 8);
- lk = lk.to_ulong() + (long)(j[x++]);
- }
- mid_rn = 0;
- long s = dissolve(lk);
- for (int z = 0 ; z < 8 ; z++)
- set_64[z] = mid_rn[7-z];
- fout << (char)(s) << (char)(set_64.to_ulong());
- x++;
- }
- return;
- }
- int main(int argc, char * argv[]) {
- fstream fin, fout;
- int i = 0, j = 3;
- bool x = 0;
- if (argc > 3) {
- if (strcmp("-c", argv[1]) == 0) {
- i = 2;
- }
- else if (strcmp("-d", argv[1]) == 0) {
- i = 2;
- x = 1;
- }
- else {
- i = 3;
- j = 1;
- }
- }
- else if (argc <= 3)
- printf("Syntax Error: %s -c <input> <output>", argv[0]);
- fin.open(argv[i], ios::in | ios::binary);
- fout.open(argv[j], ios::out | ios::binary);
- if (!x)
- collect(fin, fout);
- else
- resolve(fin, fout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement