import Mathlib.Tacticnamespace PuncturedColouringsvariable {ι C : Type*} [DecidableEq ι] [DecidableEq C]def erase (p : ι) (w : ι → Option C) (i : ι) : Option C :=if i = p then none else w idef permits (palette : Finset C) (before : ι → Option C) (i : ι) (c : C) : Prop :=match before i with| none => c ∈ palette| some a => if a ∈ palette then c ∈ palette else c = adef follows (palette : Finset C) (before after : ι → Option C) : Prop :=∀ i c, after i = some c → permits palette before i ctheorem erase_subword {w : ι → Option C} {p i : ι} {c : C} (h : erase p w i = some c) :w i = some c := byby_cases e : i = p <;> simp_all [erase]theorem followsErase {S : Finset C} {u w : ι → Option C} (h : follows S u w) (p : ι) :follows S u (erase p w) := byintro i c hiexact h i c (erase_subword hi)def valid (rows : Set (Fin 4 → ι)) (good : (Fin 4 → C) → Prop) (w : ι → Option C) : Prop :=∀ row ∈ rows, ∀ colours, (∀ j, w (row j) = some (colours j)) → good colourstheorem validErase {rows : Set (Fin 4 → ι)} {good : (Fin 4 → C) → Prop}{w : ι → Option C} (hw : valid rows good w) (p : ι) : valid rows good (erase p w) := byintro row hrow cols hcexact hw row hrow cols (fun j => erase_subword (hc j))def atMostOneHole (w : ι → Option C) : Prop :=∀ i j, w i = none → w j = none → i = jdef move (rows : Set (Fin 4 → ι)) (good : (Fin 4 → C) → Prop) (k : ℕ)(u w : ι → Option C) : Prop :=atMostOneHole w ∧ valid rows good w ∧ ∃ S : Finset C, S.card ≤ k ∧ follows S u wtheorem moveAllowsEveryDeletion {rows : Set (Fin 4 → ι)} {good : (Fin 4 → C) → Prop} {k : ℕ}{u w : ι → Option C} (h : move rows good k u w) (full : ∀ i, w i ≠ none) (p : ι) :move rows good k u (erase p w) := byobtain ⟨_,hw,S,hs,hf⟩ := hhave only (i : ι) (hi : erase p w i = none) : i = p := byby_contra eexact full i (by simpa [erase,e] using hi)exact ⟨fun i j hi hj => (only i hi).trans (only j hj).symm,validErase hw p,S,hs,followsErase hf p⟩theorem omittedDeletionObstructs {rows : Set (Fin 4 → ι)} {good : (Fin 4 → C) → Prop} {k : ℕ}{states : Set (ι → Option C)}(closed : ∀ u ∈ states, ∀ v, (∃ i, v i = none) → move rows good k u v → v ∈ states){u w : ι → Option C} (hu : u ∈ states) (full : ∀ i, w i ≠ none){p : ι} (absent : erase p w ∉ states) : ¬move rows good k u w := byintro h; exact absent (closed u hu _ ⟨p,by simp [erase]⟩ (moveAllowsEveryDeletion h full p))def total (w : ι → C) (i : ι) : Option C := some (w i)theorem restoreOne (p : ι) (w : ι → C) :follows {w p} (erase p (total w)) (total w) := byintro i c hcchange some (w i) = some c at hcobtain rfl : w i = c := Option.some.inj hcby_cases e : i = p· subst i; simp [permits,erase]· simp [permits,erase,e,total]theorem punctureCanRestore {rows : Set (Fin 4 → ι)} {good : (Fin 4 → C) → Prop} {k : ℕ}(hk : 1 ≤ k) (p : ι) (w : ι → C) (hw : valid rows good (total w)) :move rows good k (erase p (total w)) (total w) := byexact ⟨fun _ _ h _ => by simp [total] at h,hw,{w p}, by simpa using hk, restoreOne p w⟩def holeList : List ℕ := [0, 1, 2, 11, 14, 16, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38]def witnessList : List ℕ := [11498498316511472183594228577674103195008014865650795769201157432418823, 15235934777742483934121914466685745761756077583018390457145270215705720, 11570880699959534668452260028798641995438931663526657310018938903987136, 8724771580379342705042740928994428318145675826894068880732914692432904, 18417274621232585847109394219718098737516796469538305191792649553588360, 539349647046889780710307157755976848709030179116840749606330828940382, 22072035138723557192506746184643548400783596614146132906871875024224392, 545986056459008648237300235799543899539211579364941920657664030894493, 16591546092237366478448725020159378894456912591884231448987398160615048, 67441871505984745426194842197791439489446115046616820598081595681948, 15433103890307393664828206668229680992501567255816231379681948499444800, 15247643699633349273584541248952108268116598579630084406682286086161472, 18903982904469476585602327238209649293829099821828243668982929457742472, 11139515047264060120324028340057926680941782938886916724741145505662016, 18890506444868515379696365039145381132082202855680839285996981806957632, 67446293699048487480235167346012043575992590639394158578163861433484, 539570349592387899841881338768096348607940725115153268625860631815270, 12582138642491835511956402352528772768886914734796247084736034091611784, 540178747217286230324899878805681477041153018670939227600231535610899, 539349647046889780710307157755976850331622947408974381833261071557726, 22611247261589753386389202485383619596846707023321809356574119433437320, 15459846628565220879243528828448947638082353331400098002995933268935744]def atPos (s i : ℕ) := ((witnessList[s]?.getD 0) >>> (3*i)) % 8def equalPairs (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)set_option maxHeartbeats 60000000set_option maxRecDepth 100000theorem witnessHoles : ∀ s : Fin 22, ∀ i : Fin 78,atPos s i = 7 ↔ i.val = holeList[s.val]?.getD 78 := by decidetheorem puncturedProgressions : ∀ s : Fin 22, ∀ a : Fin 78, ∀ d : Fin 26,0 < d.val → a.val+3*d.val<78 →let u := atPos s a; let v := atPos s (a.val+d.val)let w := atPos s (a.val+2*d.val); let z := atPos s (a.val+3*d.val)u=7 ∨ v=7 ∨ w=7 ∨ z=7 ∨ equalPairs u v w z ≤ 1 := by decidedef maximalWords : List ℕ := [904911715429935787385572270211716221573805921017891087062174549282726, 1349746436028804576081366070274941650237157120574807894207864596630483, 24215621784496317677956398504903356389164241418896206183751408460853626, 17611914093906900129315494760871412457969492104456392857383900919910585, 15973953172520580137285221684121690529811392616156224760167150207921681, 1776032359162255779641073162237349767708836288225310899248625996944836, 3045693185499869025797497350409955354852357945075956034259047619686211, 2623565124977642132879948870960098775302401709614589358164072928750278, 1343842047486808963655427479701507449400273380734703946402386865671554, 875407210886499674431234006611452972934076752180969520853291344661123, 1121161371222550588334631712088329479347177100145073827359892629983685, 2148586504047455089536750624602397989770159351495509905855531945500868, 1825073571864733808735028178493402887275579302437684593949002183057479, 1515537735114168414373875535872737465293041305787009298416657926053954, 2962020197669891376430600903711707203630856919123458580922529675845698, 181243780862455846388203297145677217723706300964327190494209259342832, 156179020469688145369904880960220507581439345282777103812801712358488, 375239246438379266374702800683099760626713959154912953505337769734488, 905778877573152849165940882478791488621992336817255396889677430018360, 397748043375732568800016476276860420149186622669167437420544251924632, 283171270150791021257944932827495371737618526880277616754030848737560, 310392157747750602648250006234367341486199928298459835937114931080208]def maximalMasks : List ℕ := [193958291011757266764432, 174767912136868385595984, 96979145505878633382216, 87383956068434192797992, 48489572752939316691108, 193958291011757266764418, 193958291011757266702978, 193958291011757265719938, 193958290730351008486018, 193958291011757266764306, 154145749287774273446986, 154145749283377300677706, 154141425827735025001546, 170600533227502372430922, 154072250537458614182986, 230221274804422116188209, 230221274769238817581105, 229652740682097622540329, 194800067378873599338537, 240852226755127090948137, 240914774981374103341081, 236415922034905039524389]def fullColour (s i : ℕ) := ((maximalWords[s]?.getD 0) >>> (3*i)) % 8abbrev inClass (s i : ℕ) := ((maximalMasks[s]?.getD 0) >>> i) % 2 = 1theorem maximalClassSupport : ∀ s : Fin 22, ∀ i : Fin 78,fullColour s i = 0 ↔ inClass s i := by decidetheorem seventeenPerClass : ∀ s : Fin 22,(Finset.univ.filter (fun i : Fin 78 => fullColour s i = 0)).card = 17 := by decidetheorem maximalProgressions : ∀ s : Fin 22, ∀ a : Fin 78, ∀ d : Fin 26,0 < d.val → a.val+3*d.val<78 →equalPairs (fullColour s a) (fullColour s (a.val+d.val))(fullColour s (a.val+2*d.val)) (fullColour s (a.val+3*d.val)) ≤ 1 := by decidedef pairShade (i : ℕ) := ((445741904669149390401057233603967706018173073365851416598782714188482 : ℕ) >>> (3*i)) % 8theorem pairClassCounts :(Finset.univ.filter (fun i : Fin 78 => pairShade i = 0)).card = 17 ∧(Finset.univ.filter (fun i : Fin 78 => pairShade i = 1)).card = 14 := by decidetheorem pairProgressions : ∀ a : Fin 78, ∀ d : Fin 26, 0<d.val → a.val+3*d.val<78 →equalPairs (pairShade a) (pairShade (a.val+d.val))(pairShade (a.val+2*d.val)) (pairShade (a.val+3*d.val)) ≤ 1 := by decideend PuncturedColourings