import Mathlib.Tacticimport Mathlib.GroupTheory.Perm.Finnamespace AffineColoursdef equalPairs {α : Type*} [DecidableEq α] (a b c d : α) : ℕ :=(if a=b then 1 else 0) + (if a=c then 1 else 0) + (if a=d then 1 else 0) +(if b=c then 1 else 0) + (if b=d then 1 else 0) + (if c=d then 1 else 0)theorem pairCriterion {α : Type*} [DecidableEq α] (a b c d : α) :3 ≤ ({a,b,c,d} : Finset α).card ↔ equalPairs a b c d ≤ 1 := byby_cases hab : a=b <;> by_cases hac : a=c <;> by_cases had : a=d <;>by_cases hbc : b=c <;> by_cases hbd : b=d <;> by_cases hcd : c=d <;>simp_all [equalPairs, Finset.card_insert_of_notMem]def packed : ℕ := 4249435753119052576007099789721455590363978824005653704617637945869838442270659337976577868545def shade (i : ℕ) : ℕ := (packed >>> (4*i)) % 16def rotate (c : ℕ) := if c=0 then 0 else 1 + 3*((c-1)/3) + c%3def valid (w : ℕ → ℕ) := ∀ a d : ℕ, 0<d → a+3*d<78 →3 ≤ ({w a,w (a+d),w (a+2*d),w (a+3*d)} : Finset ℕ).cardset_option maxHeartbeats 4000000set_option maxRecDepth 20000theorem tenRange : ∀ i : Fin 78, shade i < 10 := by decidetheorem tenEquivariant : ∀ i : Fin 78, shade ((23*(i.val+1))%79-1) = rotate (shade i) := by decidetheorem rotateOrderThree : ∀ i : Fin 10, rotate (rotate (rotate i)) = i := by decidetheorem tenCheck : ∀ a : Fin 78, ∀ d : Fin 26, 0<d.val → a.val+3*d.val<78 →equalPairs (shade a) (shade (a.val+d.val)) (shade (a.val+2*d.val)) (shade (a.val+3*d.val)) ≤ 1 := by decidetheorem tenValid : valid shade := byintro a d hd hbexact (pairCriterion ..).2 (tenCheck ⟨a,by omega⟩ ⟨d,by omega⟩ hd hb)theorem affineCubeParameters : ∀ a b : Fin 78,((a.val^3)%78=1 ∧ ((a.val^2+a.val+1)*b.val)%78=0) ↔(a.val=1 ∧ (b.val=0 ∨ b.val=26 ∨ b.val=52)) ∨ ((a.val=55 ∨ a.val=61) ∧ b.val%2=0) := by decidetheorem affineThirteenthMultiplier : ∀ a : Fin 78, (a.val^13)%78=1 ↔ a.val=1 := by decidetheorem thirteenthShift : ∀ b : Fin 78, (13*b.val)%78=0 ↔ b.val%6=0 := by decidetheorem paletteOrderThirteen {k : ℕ} (hk : k<13) (σ : Equiv.Perm (Fin k)) (hσ : σ^13=1) : σ=1 := byhave hdiv := orderOf_dvd_of_pow_eq_one hσrcases (Nat.dvd_prime (show Nat.Prime 13 by decide)).mp hdiv with h | h· exact orderOf_eq_one_iff.mp h· have hc : orderOf σ ∣ Fintype.card (Equiv.Perm (Fin k)) := orderOf_dvd_cardrw [h, Fintype.card_perm, Fintype.card_fin] at hcexact False.elim ((show ¬13 ∣ k.factorial from fun hd => by interval_cases k <;> norm_num [Nat.factorial] at hd) hc)/-- The order-thirteen multiplier 18 forces a monochromatic triple in the row 62,63,64,65. -/theorem noThirteenPrime {w : ℕ → ℕ} (hw : valid w)(hinv : ∀ i : Fin 78, w ((18*(i.val+1))%79-1) = w i) : False := byhave step (i : ℕ) (h : i<78) : w ((18*(i+1))%79-1) = w i := hinv ⟨i,h⟩have he : w 61 = w 63 := calcw 61 = w 20 := step 20 (by decide)_ = w 66 := step 66 (by decide)_ = w 51 := step 51 (by decide)_ = w 37 := step 37 (by decide)_ = w 45 := step 45 (by decide)_ = w 63 := step 63 (by decide)have hf : w 63 = w 64 := step 64 (by decide)have hr := hw 61 1 (by decide) (by decide)change 3 ≤ ({w 61,w 62,w 63,w 64} : Finset ℕ).card at hrhave hc := (pairCriterion (w 61) (w 62) (w 63) (w 64)).mp hrrw [he, hf] at hcsimp [equalPairs] at hctheorem noThirteenShift {w : ℕ → ℕ} (hw : valid w)(hinv : ∀ i : Fin 78, w ((i.val+6)%78) = w i) : False := byhave h₁ : w 6 = w 0 := hinv 0have h₂ : w 12 = w 6 := hinv 6have h₃ : w 18 = w 12 := hinv 12have hr := hw 0 6 (by decide) (by decide)change 3 ≤ ({w 0,w 6,w 12,w 18} : Finset ℕ).card at hrrw [h₃,h₂,h₁] at hrsimp at hrtheorem fourthOrderParameters : ∀ a b : Fin 78,(((a.val^2)%78≠1 ∨ ((a.val+1)*b.val)%78≠0) ∧(a.val^4)%78=1 ∧ ((a.val+1)*(a.val^2+1)*b.val)%78=0) ↔a.val=5 ∨ a.val=47 ∨ ((a.val=31 ∨ a.val=73) ∧ b.val%3=0) := by decidetheorem exponentTwoCommutes {G : Type*} [Group G] (h : ∀ x : G, x*x=1) (a b : G) : a*b=b*a := byhave hinv (x : G) : x⁻¹=x := calcx⁻¹ = x⁻¹*(x*x) := by rw [h]; simp_ = x := by groupcalca*b = (a*b)⁻¹ := (hinv _).symm_ = b⁻¹*a⁻¹ := mul_inv_rev a b_ = b*a := by rw [hinv,hinv]end AffineColours