import Mathlib.Tacticnamespace ParityPalettesdef differentThree (a b c d : Nat) : Bool :=!((a == b && (c == d || a == c || a == d)) ||(a == c && (b == d || a == d)) || (a == d && b == c) || (b == c && b == d))def digit (w pos : Nat) : Nat := w / 4^pos % 4def endOK (w length : Nat) : Bool :=(List.range ((length-1)/3)).all fun i =>let d := i+1differentThree (digit w 0) (digit w d) (digit w (2*d)) (digit w (3*d))def legal : Nat → Nat → Bool| 0, _ => true| n+1, w => endOK w (n+1) && legal n (w/4)def seed : List Nat := [6, 18, 22, 24, 25, 26, 27]def extend (n : Nat) (ws : List Nat) : List Nat :=ws.flatMap fun w => ([0,1,2,3].map fun c => 4*w+c).filter fun z => endOK z ndef layer : Nat → List Nat| 0 => seed| k+1 => extend (k+5) (layer k)theorem cover (k w : Nat) (hstart : w/4^k ∈ seed) (h : legal (k+4) w = true) :w ∈ layer k := byinduction k generalizing w with| zero => simpa [layer] using hstart| succ k ih =>have hs : (w/4)/4^k ∈ seed := bysimpa [Nat.div_div_eq_div_mul, pow_succ, Nat.mul_comm] using hstartchange (endOK w (k+5) && legal (k+4) (w/4)) = true at hhave both := Bool.and_eq_true_iff.mp happly List.mem_flatMap.mprrefine ⟨w/4, ih (w/4) hs both.2, List.mem_filter.mpr ⟨?_, both.1⟩⟩apply List.mem_map.mprrefine ⟨w%4, ?_, by omega⟩have hm := Nat.mod_lt w (by decide : 0<4)simpomegatheorem digit_succ (w i : Nat) : digit w (i+1) = digit (w/4) i := bysimp [digit, pow_succ, Nat.div_div_eq_div_mul, Nat.mul_comm]def rename (p : Fin 4 → Fin 4) : Nat → Nat → Nat| 0, _ => 0| n+1, w => 4 * rename p n (w/4) + (p ⟨w%4, Nat.mod_lt _ (by decide)⟩).valdef permDigit (p : Fin 4 → Fin 4) (x : Nat) : Nat := (p ⟨x%4, Nat.mod_lt _ (by decide)⟩).valtheorem rename_shift (p : Fin 4 → Fin 4) (n w : Nat) :rename p (n+1) w / 4 = rename p n (w/4) := bysimp only [rename]have h := (p ⟨w%4, Nat.mod_lt _ (by decide)⟩).isLtomegatheorem rename_digit (p : Fin 4 → Fin 4) (n w i : Nat) (hi : i<n) :digit (rename p n w) i = permDigit p (digit w i) := byinduction n generalizing w i with| zero => omega| succ n ih =>cases i with| zero =>simp [rename, digit, permDigit, Nat.add_mod, Nat.mod_eq_of_lt (p _).isLt]| succ i =>rw [digit_succ, rename_shift, digit_succ w, ih (w/4) i (by omega)]theorem perm_eq (p : Fin 4 → Fin 4) (hp : Function.Injective p) (a b : Nat)(ha : a<4) (hb : b<4) : (permDigit p a == permDigit p b) = (a==b) := byapply Bool.eq_iff_iff.mprsimp [permDigit, Nat.mod_eq_of_lt ha, Nat.mod_eq_of_lt hb, hp.eq_iff, Fin.val_inj]theorem digit_lt (w i : Nat) : digit w i < 4 := Nat.mod_lt _ (by decide)theorem rename_legal (p : Fin 4 → Fin 4) (hp : Function.Injective p) (n w : Nat) :legal n (rename p n w) = legal n w := byinduction n generalizing w with| zero => rfl| succ n ih =>simp only [legal, rename_shift, ih]congr 1apply Bool.eq_iff_iff.mprsimp only [endOK, List.all_eq_true, List.mem_range]constructor <;> intro H j hjall_goalshave h := H j hjhave h0 : 0<n+1 := by omegahave h1 : j+1<n+1 := by omegahave h2 : 2*(j+1)<n+1 := by omegahave h3 : 3*(j+1)<n+1 := by omegasimpa only [rename_digit _ _ _ _ h0, rename_digit _ _ _ _ h1,rename_digit _ _ _ _ h2, rename_digit _ _ _ _ h3, differentThree,perm_eq p hp _ _ (digit_lt _ _) (digit_lt _ _)] using htheorem rename_prefix (p : Fin 4 → Fin 4) (k w : Nat) :rename p (k+4) w / 4^k = rename p 4 (w / 4^k) := byinduction k generalizing w with| zero => simp| succ k ih =>rw [pow_succ', ← Nat.div_div_eq_div_mul, ← Nat.div_div_eq_div_mul]rw [show k+1+4 = (k+4)+1 by omega, rename_shift, ih]def permCode (code : Nat) (c : Fin 4) : Fin 4 :=⟨digit code c.val, digit_lt _ _⟩def permutationCodes : List Nat :=[27,30,39,45,54,57,75,78,99,108,114,120,135,141,147,156,177,180,198,201,210,216,225,228]def chosenPerm (i : Fin 24) : Fin 4 → Fin 4 := permCode (permutationCodes[i.val]!)set_option maxRecDepth 100000 inset_option maxHeartbeats 0 intheorem small_normalization : ∀ w : Fin 256, legal 4 w.val = true →∃ i : Fin 24, Function.Injective (chosenPerm i) ∧ rename (chosenPerm i) 4 w.val ∈ seed := bydecide +kerneltheorem legal_prefix (k w : Nat) (h : legal (k+4) w = true) :legal 4 (w / 4^k) = true := byinduction k generalizing w with| zero => simpa using h| succ k ih =>rw [pow_succ', ← Nat.div_div_eq_div_mul]exact ih (w/4) (Bool.and_eq_true_iff.mp h).2theorem normalize (k w : Nat) (h : legal (k+4) w = true) (width : w < 4^(k+4)) :∃ p : Fin 4 → Fin 4, Function.Injective p ∧ rename p (k+4) w ∈ layer k := byhave hw : w / 4^k < 256 := byapply (Nat.div_lt_iff_lt_mul (by positivity : 0<4^k)).mprsimpa [pow_add, Nat.mul_comm] using widthobtain ⟨c, hi, hs⟩ := small_normalization ⟨_, hw⟩ (legal_prefix k w h)refine ⟨chosenPerm c, hi, cover k _ ?_ ?_⟩· rwa [rename_prefix]· rwa [rename_legal _ hi]end ParityPalettes