// Releve final : les checks du carnet.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Fract { num: i128, den: i128 }
fn gcd(mut a: i128, mut b: i128) -> i128 {
while b != 0 { let t = b; b = a % b; a = t; }
a
}
impl Fract {
fn new(num: i128, den: i128) -> Self {
let g = gcd(if num == 0 { 1 } else { if num < 0 { -num } else { num } },
if den == 0 { 1 } else { if den < 0 { -den } else { den } });
let (n, d) = (num / g, den / g);
if d < 0 { Fract { num: -n, den: -d } } else { Fract { num: n, den: d } }
}
fn integer(n: i128) -> Self { Fract { num: n, den: 1 } }
fn recip(&self) -> Fract { Fract::new(self.den, self.num) }
fn add(&self, o: &Fract) -> Fract {
Fract::new(self.num * o.den + o.num * self.den, self.den * o.den)
}
fn mul(&self, o: &Fract) -> Fract {
Fract::new(self.num * o.num, self.den * o.den)
}
fn scale(&self, n: i128) -> Fract {
Fract::new(self.num * n, self.den)
}
fn neg(&self) -> Fract {
Fract { num: -self.num, den: self.den }
}
}
impl std::fmt::Display for Fract {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.den == 1 { write!(f, "{}", self.num) }
else { write!(f, "{}/{}", self.num, self.den) }
}
}
fn py_bool(b: bool) -> String {
if b { "True".to_string() } else { "False".to_string() }
}
type Quad = (Fract, Fract);
fn qmul(x: Quad, y: Quad) -> Quad {
(x.0.mul(&y.0).add(&x.1.mul(&y.1).scale(2)),
x.0.mul(&y.1).add(&x.1.mul(&y.0)))
}
// preserve (check 3)
type Matrix = (i64, i64, i64, i64);
fn preserve(m: Matrix, coef: i64) -> bool {
let (a, b, c, d) = m;
let q = |t: i64, x: i64| coef * t * t - x * x;
(-2..=2).all(|t| (-2..=2).all(|x| q(a * t + b * x, c * t + d * x) == q(t, x)))
}
// chaines, pas (checks 4, 5, 6)
type Chain = (&'static str, &'static str, &'static str);
fn pas(c: Chain) -> (Vec<&'static str>, i64) {
(vec![c.0, c.1, c.2, "(2,2)"], 1)
}
let chaines: Vec<(&'static str, Chain)> = vec![
("electron", ("a1", "x1", "a2")),
("muon", ("a1", "x1", "a2")),
("tau", ("a1", "x1", "a2")),
("proton", ("s1", "x1", "s4")),
("neutron", ("P.f1", "P.x1", "P.f2")),
("mot photon", ("s3", "x1", "s2")),
("toile univers", ("B00.f1", "B00.x1", "B00.f2")),
];
let enregistrees: std::collections::BTreeSet<Chain> =
chaines.iter().map(|&(_, c)| c).collect();
let renv: Vec<Chain> = chaines.iter().map(|&(_, c)| (c.2, c.1, c.0)).collect();
let deltas: Vec<(Vec<&'static str>, i64)> =
chaines.iter().map(|&(_, c)| pas(c)).collect();
// coupures, rows52, enumerer (checks 9, 10, 11)
fn coupures(s: &str, zero_initial: bool) -> Vec<Fract> {
let mut out = vec![Fract::integer(s.parse::<i128>().unwrap())];
for i in 1..s.len() {
if !zero_initial && s[i..].len() > 1 && s.as_bytes()[i] == b'0' {
continue;
}
let num: i128 = s[..i].parse().unwrap();
let den: i128 = s[i..].parse().unwrap();
out.push(Fract::new(num, den));
}
out
}
static ROWS52: &[(&str, &str, &str, &str)] = &[
("536569972", "251458", "197792604162025", "16279142425"),
("84760912", "2518", "3124543897625", "2083022438425"),
("34759474", "252", "38440455782425", "25626909081625"),
("91759312", "2518", "3382532915225", "2255015116825"),
];
fn enumerer(zero_initial: bool) -> Vec<((Fract, Fract), (i64, i64, i64), Vec<(Fract, Fract, Fract, Fract)>)> {
let mut sols = Vec::new();
let h_cuts = coupures("5384", zero_initial);
for h1 in &h_cuts {
for h2 in &h_cuts {
let h2q = qmul((*h1, *h2), (*h1, *h2));
for &sa in &[1i64, -1] {
for &sb in &[1i64, -1] {
for &sq in &[1i64, -1] {
let mut retenues = Vec::new();
let mut ok = true;
for &(A_s, B_s, Qa_s, Qb_s) in ROWS52 {
let a_cuts = coupures(A_s, zero_initial);
let b_cuts = coupures(B_s, zero_initial);
let qa_cuts = coupures(Qa_s, zero_initial);
let qb_cuts = coupures(Qb_s, zero_initial);
let mut trouve: Option<(Fract, Fract, Fract, Fract)> = None;
for A in &a_cuts {
for B in &b_cuts {
for Qa in &qa_cuts {
for Qb in &qb_cuts {
let lhs = qmul((Qa.scale(sq as i128), Qb.neg()), h2q);
let rhs = (A.scale(sa as i128), B.scale(sb as i128));
if lhs == rhs {
trouve = Some((*A, *B, *Qa, *Qb));
break;
}
}
if trouve.is_some() { break; }
}
if trouve.is_some() { break; }
}
if trouve.is_some() { break; }
}
match trouve {
None => { ok = false; break; }
Some(t) => { retenues.push(t); }
}
}
if ok && retenues.len() == ROWS52.len() {
sols.push(((*h1, *h2), (sa, sb, sq), retenues));
}
}
}
}
}
}
sols
}
// Bureaux attendus (de la cellule 13)
let rangees_verifiees: Vec<&'static str> = vec!["electron", "muon", "tau", "toile univers"];
let bureaux_decodes: Vec<(&'static str, ((i128, i128), (i128, i128)))> = vec![
("electron", ((536569, 972), (25, 1458))),
("muon", ((847609, 12), (25, 18))),
("tau", ((3475947, 4), (25, 2))),
("toile univers", ((917593, 12), (25, 18))),
];
// Rejouer le decodage (autonomie complete)
let solutions = enumerer(false);
let h = solutions[0].0;
let retenues = solutions[0].2.clone();
// Quotients decodes (check 8)
let mut quotients_decodes: Vec<(&'static str, Quad)> = Vec::new();
for i in 0..rangees_verifiees.len() {
let (_A, _B, Qa, Qb) = &retenues[i];
quotients_decodes.push((rangees_verifiees[i], (*Qa, *Qb)));
}
quotients_decodes.push(("proton", (Fract::integer(0), Fract::integer(0))));
quotients_decodes.push(("neutron", (Fract::integer(0), Fract::integer(0))));
// Les 11 checks dans l'ordre source
let checks: Vec<(&'static str, bool)> = vec![
("eq. 75 : 2942 + 609 + 148 = 3699",
2942i64 + 609 + 148 == 3699),
("eq. 76 : tau = 1/4, reciproque du rapport 4",
Fract::integer(4).recip() == Fract::new(1, 4)),
("quatre cartes de signe a tau = 1/4", {
let mut cartes: Vec<Matrix> = Vec::new();
for a in -3..=3i64 { for b in -3..=3i64 { for c in -3..=3i64 { for d in -3..=3i64 {
let m = (a, b, c, d);
if preserve(m, 4) { cartes.push(m); }
}}}}
cartes.sort();
let mut signes: Vec<Matrix> = vec![(1,0,0,1),(1,0,0,-1),(-1,0,0,1),(-1,0,0,-1)];
signes.sort();
cartes == signes
}),
("renversement auto-inverse (7/7)",
renv.iter().zip(&chaines).all(|(r, &(_, c))| (r.2, r.1, r.0) == c)),
("aucune chaine renversee enregistree",
renv.iter().all(|r| !enregistrees.contains(r))),
("pas : 1 site + 1 lien (7/7, construction)",
deltas.iter().all(|(c2, l)| c2.len() == 4 && *l == 1)),
("45 cellules = 9 x 5", 9i64 * 5 == 45),
("parting muon/electron (quotients decodes)",
quotients_decodes[1].1 != quotients_decodes[0].1),
("decodage : une seule coupure reconstitue l'identite",
solutions.len() == 1),
("decodage : bureau = quotient * h^2 aux 4 rangees",
retenues.iter().all(|&(ref A, ref B, ref Qa, ref Qb)|
qmul((Qa.clone(), Qb.clone().neg()), qmul(h, h)) == (*A, *B))),
("decodage : la coupure est celle annoncee", {
let attendus: Vec<Quad> = rangees_verifiees.iter().map(|&n| {
let entry = bureaux_decodes.iter().find(|(name, _)| *name == n).unwrap();
let ((n1, d1), (n2, d2)) = entry.1;
(Fract::new(n1, d1), Fract::new(n2, d2))
}).collect();
let retenues_ab: Vec<Quad> = retenues.iter().map(|&(ref A, ref B, _, _)| (*A, *B)).collect();
retenues_ab == attendus
}),
];
for (nom, ok) in &checks {
println!("{} -> {}", nom, py_bool(*ok));
}
let total: usize = checks.iter().filter(|&(_, ok)| *ok).count();
println!("Total : {} / {}", total, checks.len());