// c++ -O3 -std=c++17 column_controls_338.cpp -o /tmp/cc338; /tmp/cc338 > results.json
#include <array>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
using U=uint64_t;
const int Q[8]={0,1,3,0,2,3,1,2};
struct RNG{U s;U next(){U z=(s+=0x9e3779b97f4a7c15ULL);z=(z^(z>>30))*0xbf58476d1ce4e5b9ULL;z=(z^(z>>27))*0x94d049bb133111ebULL;return z^(z>>31);}int n(int k){U a,lim=-U(k)%k;do{a=next();}while(a<lim);return a%k;}};
using Row=array<int,8>;
template<size_t N>using Grid=array<Row,N>;
struct Tot{int n=0,pos=0,unique=0,both=0,advance=0,trueunique=0,tie0=0,cyclic=0,both_tails=0;U perms=0;double bits=0;};
template<size_t N>void gen(Grid<N>&x,RNG&r,int snum,bool rot){
 for(auto &a:x){int k=rot?r.n(8):0;for(int j=0;j<8;j++){int u=r.n(4), gate=r.n(5);a[j]=rot?Q[(j+k)%8]:(gate<snum?Q[j]:u);}}}
void trial(const Grid<10>&t,const Grid<13>&x,RNG& rr,Tot&z,int gold,RNG*cy=nullptr){
 // Equal sample sizes in all columns: the denominator drops out of phase and scramble comparisons.
 int counts[8][4]={},sum[4]={};double co[8][4],pool[4];
 for(auto &row:t)for(int j=0;j<8;j++){counts[j][row[j]]++;sum[row[j]]++;}
 for(int j=0;j<8;j++)for(int q=0;q<4;q++)co[j][q]=log2((counts[j][q]+.5)/12.);
 for(int q=0;q<4;q++)pool[q]=log2((sum[q]+.5)/82.);
 double sc[8]={},abs_pool=0;
 for(auto &r:x)for(int j=0;j<8;j++){int q=r[j];abs_pool+=pool[q];for(int k=0;k<8;k++)sc[k]+=co[(j+k)%8][q];}
 // Small tolerance prevents repeated arithmetically identical products from separating due to rounding.
 const double eps=1e-10;double mx=*max_element(sc,sc+8);int ties=0;
 for(double s:sc)if(mx-s<=eps)ties++;
 bool win=mx-sc[0]<=eps&&ties==1, pos=sc[0]-abs_pool>eps;
 z.n++;z.bits+=sc[0]-abs_pool;z.pos+=pos;z.unique+=win;z.tie0+=(mx-sc[0]<=eps&&ties>1);z.trueunique+=(mx-sc[gold]<=eps&&ties==1);
 if(!win||!pos)return;
 z.both++;int exceed=0;
 // Uniform random permutations of an eight-element row can be indexed 0..40319.
 // Build exact per-row lookup of its sum across frozen column scores once per panel.
 static array<Row,40320> pr;static bool ready=false;
 if(!ready){Row p={0,1,2,3,4,5,6,7};int ix=0;do{pr[ix++]=p;}while(next_permutation(p.begin(),p.end()));ready=true;}
 // Select independent uniform permutation ranks in every row on each draw.
 static array<array<double,40320>,13> score;
 for(int i=0;i<13;i++)for(int v=0;v<40320;v++){
   double s=0;for(int j=0;j<8;j++)s+=co[j][x[i][pr[v][j]]];score[i][v]=s;
 }
 for(int perm=0;perm<9999&&exceed<500;perm++){
   double sy=0;for(int i=0;i<13;i++)sy+=score[i][rr.n(40320)];exceed+=(sy>=sc[0]-eps);z.perms++;
 }
 z.advance+=exceed<500;
 if(cy){
  double scores[13][8]={};for(int i=0;i<13;i++)for(int k=0;k<8;k++)for(int j=0;j<8;j++)scores[i][k]+=co[j][x[i][(j+k)%8]];
  int hits=0;for(int k=0;k<9999&&hits<500;k++){double s=0;for(int i=0;i<13;i++)s+=scores[i][cy->n(8)];hits+=(s>=sc[0]-eps);}
  z.cyclic+=hits<500;z.both_tails+=hits<500&&exceed<500;
 }
}
void show(string label,Tot z,bool comma=true){
 cout<<"{\"panel\":\""<<label<<"\",\"n\":"<<z.n<<",\"phase0_positive\":"<<z.pos<<",\"phase0_sole_best\":"<<z.unique<<",\"phase0_tied_best\":"<<z.tie0<<",\"gates_1_and_2\":"<<z.both<<",\"all_three\":"<<z.advance<<",\"gold_alignment_sole_best\":"<<z.trueunique<<",\"mean_phase0_delta_bits\":"<<z.bits/z.n<<",\"row_shuffles_drawn\":"<<z.perms<<",\"cyclic_null_diagnostic_passes\":"<<z.cyclic<<",\"passes_both_reference_tails\":"<<z.both_tails<<"}"<<(comma?",\n":"\n");}
int main(){cout<<setprecision(12)<<"{\"cases\":[\n";for(int mode=0;mode<7;mode++){
 int snum=min(mode,4);if(snum==4)snum=5;bool anti=mode==6;bool control=mode>=5;
 if(control)snum=0;
 int N=control?1024:256;Tot a,b;RNG seed{3381102ULL+U(mode)*98701},ra{338004751ULL+U(mode)*4111},rb{1023456071ULL+U(mode)*31097},rc{338119099ULL+U(mode)*177};
 for(int i=0;i<N;i++){
  Grid<10> t;Grid<13>x,x2;gen(t,seed,snum,anti);gen(x,seed,snum,anti);
  trial(t,x,ra,a,0,anti?&rc:nullptr);if(!control){for(int j=0;j<13;j++)for(int k=0;k<8;k++)x2[j][k]=x[j][(k+7)%8];trial(t,x2,rb,b,7);}
 }
 if(!control){assert(a.trueunique==b.trueunique);if(snum==5){assert(a.advance==N&&b.advance==0&&a.trueunique==N&&b.trueunique==N);assert(abs(a.bits/N-104*log2(3.5))<1e-9&&abs(b.bits/N+104*log2(6.))<1e-9);}}
 string s=to_string(snum)+"/5";if(control)show(anti?"rotating_rows_null":"uniform_null",a,mode!=6);else{show("aligned "+s,a);show("test_shift_right_one "+s,b);}
 }cout<<"],\"shuffles_per_eligible_panel\":9999,\"reject_after_upper_tail_hits\":500,\"template\":[0,1,3,0,2,3,1,2],\"units\":\"counts of synthetic panels; repeated rows permuted separately; plus-one randomization tail. gold_alignment is phase seven in rotated comparisons.\",\"roundoff_epsilon_bits\":1e-10}\n";}
