import Mathlib.Data.Int.Interval
import Mathlib.Data.Finset.Prod
import Mathlib.Data.Finset.Union
import Mathlib.Logic.Relation
import Mathlib.Order.Bounds.Basic
import Mathlib.Tactic

/-!
# A 31-cell hole in a 23-cell trihexagonal shell

Source: https://oeis.org/A395802 (Peter Exley, 2026).

The centers of the hexagons form a triangular lattice. Its two triangular
face orientations give the two types of triangle cells, adjacent to the
three hexagons at the corners of the face.
-/

namespace ExleyTrihexagonal

inductive Tile where
  | hexagon (x y : ℤ)
  | up (x y : ℤ)
  | down (x y : ℤ)
  deriving DecidableEq

open Tile

def neighbors : Tile → Finset Tile
  | hexagon x y =>
    {up x y, up (x-1) y, up (x-1) (y-1), down x y, down x (y-1), down (x-1) (y-1)}
  | up x y => {hexagon x y, hexagon (x+1) y, hexagon (x+1) (y+1)}
  | down x y => {hexagon x y, hexagon x (y+1), hexagon (x+1) (y+1)}

def Adj (a b : Tile) : Prop := b ∈ neighbors a

/-- Tile-to-tile reachability using only tiles in the given set. -/
def Reach (s : Set Tile) : Tile → Tile → Prop :=
  Relation.EqvGen (fun a b => a ∈ s ∧ b ∈ s ∧ Adj a b)

def Connected (s : Set Tile) : Prop :=
  ∀ ⦃a b⦄, a ∈ s → b ∈ s → Reach s a b

/-- The hole, shell and exterior obey the conditions in OEIS A395802. -/
def Enclosing (h s : Finset Tile) : Prop :=
  h.Nonempty ∧ s.Nonempty ∧ Disjoint h s ∧
    Connected (h : Set Tile) ∧ Connected (s : Set Tile) ∧
    Connected ((h : Set Tile) ∪ (s : Set Tile))ᶜ ∧
    ∀ a ∈ h, ∀ b ∈ neighbors a, b ∈ h ∨ b ∈ s

def holeSizes (s : ℕ) : Set ℕ :=
  {n | ∃ h sh : Finset Tile, h.card = n ∧ sh.card = s ∧ Enclosing h sh}

/-- The capacity clause, restricted to the shell sizes listed in the source. -/
def Conjecture : Prop := ∀ s : ℕ, s = 5 ∨ s = 11 ∨ (15 ≤ s ∧ Odd s) →
  IsGreatest (holeSizes s) ((s^2+6)/18)

private def cells (f : ℤ → ℤ → Tile) (xy : List (ℤ × ℤ)) : Finset Tile :=
  xy.toFinset.image fun p => f p.1 p.2

private def hole : Finset Tile :=
  cells hexagon [(-1,-1),(-1,0),(0,-1),(0,0),(0,1),(1,0),(1,1)] ∪
  cells up [(-2,-2),(-2,-1),(-2,0),(-1,-2),(-1,-1),(-1,0),(-1,1),
    (0,-1),(0,0),(0,1),(1,0),(1,1)] ∪
  cells down [(-2,-2),(-2,-1),(-1,-2),(-1,-1),(-1,0),(0,-2),(0,-1),
    (0,0),(0,1),(1,-1),(1,0),(1,1)]

private def shell : Finset Tile :=
  cells hexagon [(-2,-2),(-2,-1),(-2,0),(-1,-2),(-1,1),(0,-2),(0,2),
    (1,-1),(1,2),(2,0),(2,1),(2,2)] ∪
  cells up [(-3,-2),(-3,-1),(0,-2),(0,2),(1,-1),(1,2)] ∪
  cells down [(-2,0),(-1,-3),(-1,1),(2,0),(2,1)]

private def outside : Set Tile := ((hole : Set Tile) ∪ (shell : Set Tile))ᶜ

private def Tile.coords : Tile → ℤ × ℤ
  | hexagon x y | up x y | down x y => (x,y)

private noncomputable def box (r : ℤ) : Finset Tile :=
  let points := (Finset.Icc (-r) r).product (Finset.Icc (-r) r)
  (points.image fun p => hexagon p.1 p.2) ∪ (points.image fun p => up p.1 p.2) ∪
    (points.image fun p => down p.1 p.2)

private lemma mem_box (r : ℤ) (a : Tile) : a ∈ box r ↔
    -r ≤ a.coords.1 ∧ a.coords.1 ≤ r ∧ -r ≤ a.coords.2 ∧ a.coords.2 ≤ r := by
  cases a <;> simp [box, Tile.coords, and_assoc]

set_option maxRecDepth 4096 in
private lemma confined : hole ∪ shell ⊆ box 3 := by decide

private lemma outside_iff (a : Tile) : a ∈ outside ↔ a ∉ hole ∪ shell := by
  simp [outside]

private lemma farX (a : Tile) (h : a.coords.1 ≤ -4 ∨ 4 ≤ a.coords.1) : a ∈ outside := by
  rw [outside_iff]
  intro hm
  have := (mem_box 3 a).mp (confined hm)
  omega

private lemma farY (a : Tile) (h : a.coords.2 ≤ -4 ∨ 4 ≤ a.coords.2) : a ∈ outside := by
  rw [outside_iff]
  intro hm
  have := (mem_box 3 a).mp (confined hm)
  omega

private lemma step {s : Set Tile} {a b : Tile} (ha : a ∈ s) (hb : b ∈ s)
    (h : Adj a b) : Reach s a b := .rel _ _ ⟨ha,hb,h⟩

private lemma line {s : Set Tile} (f : ℤ → Tile) (h : ∀ z, Reach s (f z) (f (z+1)))
    (a b : ℤ) : Reach s (f a) (f b) := by
  have up (x y : ℤ) (hxy : x ≤ y) : Reach s (f x) (f y) :=
    Int.leInduction (motive := fun z _ => Reach s (f x) (f z))
      (.refl _) (fun z _ ih => .trans _ _ _ ih (h z)) y hxy
  rcases le_total a b with hab|hba
  · exact up a b hab
  · exact .symm _ _ (up b a hba)

private lemma vertical (x y : ℤ) (hx : x ≤ -4 ∨ 4 ≤ x) :
    Reach outside (hexagon x y) (hexagon x 4) := by
  apply line (fun z => hexagon x z)
  intro z
  have h₁ := farX (hexagon x z) hx
  have h₂ := farX (down x z) hx
  have h₃ := farX (hexagon x (z+1)) hx
  exact .trans _ _ _ (.symm _ _ (step h₂ h₁ (by simp [Adj, neighbors])))
    (step h₂ h₃ (by simp [Adj, neighbors]))

private lemma horizontal (x y : ℤ) (hy : y ≤ -4 ∨ 4 ≤ y) :
    Reach outside (hexagon x y) (hexagon 4 y) := by
  apply line (fun z => hexagon z y)
  intro z
  have h₁ := farY (hexagon z y) hy
  have h₂ := farY (up z y) hy
  have h₃ := farY (hexagon (z+1) y) hy
  exact .trans _ _ _ (.symm _ _ (step h₂ h₁ (by simp [Adj, neighbors])))
    (step h₂ h₃ (by simp [Adj, neighbors]))

private lemma farHex (x y : ℤ)
    (h : (x ≤ -4 ∨ 4 ≤ x) ∨ (y ≤ -4 ∨ 4 ≤ y)) :
    Reach outside (hexagon x y) (hexagon 4 4) := by
  rcases h with hx|hy
  · exact .trans _ _ _ (vertical x y hx) (horizontal x 4 (Or.inr le_rfl))
  · exact .trans _ _ _ (horizontal x y hy) (vertical 4 y (Or.inr le_rfl))

private def flood (s : Finset Tile) (start : Tile) : ℕ → Finset Tile
  | 0 => {start}
  | n+1 => let prev := flood s start n
    prev ∪ ((prev.biUnion neighbors) ∩ s)

private lemma flood_sound {s : Finset Tile} {start : Tile} (hr : start ∈ s) (n : ℕ)
    {b : Tile} (hb : b ∈ flood s start n) : b ∈ s ∧ Reach (s : Set Tile) start b := by
  induction n generalizing b with
  | zero =>
    rw [flood, Finset.mem_singleton] at hb
    subst b
    exact ⟨hr,.refl _⟩
  | succ n ih =>
    rw [flood, Finset.mem_union, Finset.mem_inter, Finset.mem_biUnion] at hb
    rcases hb with hb|⟨⟨a,ha,he⟩,hb⟩
    · exact ih hb
    · obtain ⟨ha,hp⟩ := ih ha
      exact ⟨hb,.trans _ _ _ hp (step ha hb he)⟩

private lemma finite_conn {s : Finset Tile} {start : Tile} {n : ℕ}
    (hr : start ∈ s) (h : flood s start n = s) : Connected (s : Set Tile) := by
  intro a b ha hb
  have hreach (a : Tile) (ha : a ∈ s) : Reach (s : Set Tile) start a :=
    (flood_sound hr n (h.symm ▸ ha)).2
  exact .trans _ _ _ (.symm _ _ (hreach a ha)) (hreach b hb)

private noncomputable def exteriorPatch : Finset Tile := box 4 \ (hole ∪ shell)

private lemma patch_mem (a : Tile) : a ∈ exteriorPatch ↔ a ∈ box 4 ∧ a ∈ outside := by
  simp [exteriorPatch, outside_iff]

/-- Check a rooted spanning tree given in parent-before-child order. -/
private def visit (s : Finset Tile) : List (Tile × Tile) → Finset Tile → Finset Tile
  | [], reached => reached
  | (a,b)::rest, reached =>
    visit s rest (if a ∈ reached ∧ b ∈ s ∧ b ∈ neighbors a then insert b reached else reached)

private lemma visit_sound {s : Finset Tile} {root : Tile} (certificate : List (Tile × Tile))
    {reached : Finset Tile} (hs : ∀ a ∈ reached, a ∈ s ∧ Reach (s : Set Tile) root a)
    {b : Tile} (hb : b ∈ visit s certificate reached) : b ∈ s ∧ Reach (s : Set Tile) root b := by
  induction certificate generalizing reached with
  | nil => exact hs b hb
  | cons edge rest ih =>
    rcases edge with ⟨a,c⟩
    by_cases h : a ∈ reached ∧ c ∈ s ∧ c ∈ neighbors a
    · simp only [visit, if_pos h] at hb
      apply ih (reached := insert c reached) _ hb
      intro x hx
      rcases Finset.mem_insert.mp hx with rfl|hx
      · obtain ⟨ha,path⟩ := hs a h.1
        exact ⟨h.2.1,.trans _ _ _ path (step ha h.2.1 h.2.2)⟩
      · exact hs x hx
    · rw [visit, if_neg h] at hb
      exact ih hs hb

private def patchTree : List (Tile × Tile) := [
  (hexagon 4 4, down 3 3),
  (hexagon 4 4, down 4 3),
  (hexagon 4 4, down 4 4),
  (hexagon 4 4, up 3 3),
  (hexagon 4 4, up 3 4),
  (hexagon 4 4, up 4 4),
  (down 3 3, hexagon 3 3),
  (down 3 3, hexagon 3 4),
  (down 4 3, hexagon 4 3),
  (hexagon 3 3, down 2 2),
  (hexagon 3 3, down 3 2),
  (hexagon 3 3, up 2 2),
  (hexagon 3 3, up 2 3),
  (hexagon 3 4, down 2 3),
  (hexagon 3 4, down 3 4),
  (hexagon 3 4, up 2 4),
  (hexagon 4 3, down 4 2),
  (hexagon 4 3, up 3 2),
  (hexagon 4 3, up 4 3),
  (down 2 2, hexagon 2 3),
  (down 3 2, hexagon 3 2),
  (down 2 3, hexagon 2 4),
  (down 4 2, hexagon 4 2),
  (hexagon 2 3, down 1 2),
  (hexagon 2 3, up 1 3),
  (hexagon 3 2, down 3 1),
  (hexagon 3 2, up 2 1),
  (hexagon 2 4, down 1 3),
  (hexagon 2 4, down 2 4),
  (hexagon 2 4, up 1 4),
  (hexagon 4 2, down 4 1),
  (hexagon 4 2, up 3 1),
  (hexagon 4 2, up 4 2),
  (down 1 2, hexagon 1 3),
  (down 3 1, hexagon 3 1),
  (down 1 3, hexagon 1 4),
  (down 4 1, hexagon 4 1),
  (hexagon 1 3, down 0 2),
  (hexagon 1 3, up 0 3),
  (hexagon 3 1, down 3 0),
  (hexagon 3 1, up 2 0),
  (hexagon 1 4, down 0 3),
  (hexagon 1 4, down 1 4),
  (hexagon 1 4, up 0 4),
  (hexagon 4 1, down 4 0),
  (hexagon 4 1, up 3 0),
  (hexagon 4 1, up 4 1),
  (down 0 2, hexagon 0 3),
  (down 3 0, hexagon 3 0),
  (down 0 3, hexagon 0 4),
  (down 4 0, hexagon 4 0),
  (hexagon 0 3, down (-1) 2),
  (hexagon 0 3, up (-1) 2),
  (hexagon 0 3, up (-1) 3),
  (hexagon 3 0, down 2 (-1)),
  (hexagon 3 0, down 3 (-1)),
  (hexagon 3 0, up 2 (-1)),
  (hexagon 0 4, down (-1) 3),
  (hexagon 0 4, down 0 4),
  (hexagon 0 4, up (-1) 4),
  (hexagon 4 0, down 4 (-1)),
  (hexagon 4 0, up 3 (-1)),
  (hexagon 4 0, up 4 0),
  (down (-1) 2, hexagon (-1) 2),
  (down (-1) 2, hexagon (-1) 3),
  (down 2 (-1), hexagon 2 (-1)),
  (down 3 (-1), hexagon 3 (-1)),
  (down (-1) 3, hexagon (-1) 4),
  (down 4 (-1), hexagon 4 (-1)),
  (hexagon (-1) 2, down (-2) 1),
  (hexagon (-1) 2, up (-2) 1),
  (hexagon (-1) 2, up (-2) 2),
  (hexagon (-1) 3, down (-2) 2),
  (hexagon (-1) 3, up (-2) 3),
  (hexagon 2 (-1), down 1 (-2)),
  (hexagon 2 (-1), down 2 (-2)),
  (hexagon 2 (-1), up 1 (-2)),
  (hexagon 3 (-1), down 3 (-2)),
  (hexagon 3 (-1), up 2 (-2)),
  (hexagon (-1) 4, down (-2) 3),
  (hexagon (-1) 4, down (-1) 4),
  (hexagon (-1) 4, up (-2) 4),
  (hexagon 4 (-1), down 4 (-2)),
  (hexagon 4 (-1), up 3 (-2)),
  (hexagon 4 (-1), up 4 (-1)),
  (down (-2) 1, hexagon (-2) 1),
  (down (-2) 1, hexagon (-2) 2),
  (down (-2) 2, hexagon (-2) 3),
  (down 1 (-2), hexagon 1 (-2)),
  (down 2 (-2), hexagon 2 (-2)),
  (down 3 (-2), hexagon 3 (-2)),
  (down (-2) 3, hexagon (-2) 4),
  (down 4 (-2), hexagon 4 (-2)),
  (hexagon (-2) 1, down (-3) 0),
  (hexagon (-2) 1, up (-3) 0),
  (hexagon (-2) 1, up (-3) 1),
  (hexagon (-2) 2, down (-3) 1),
  (hexagon (-2) 2, up (-3) 2),
  (hexagon (-2) 3, down (-3) 2),
  (hexagon (-2) 3, up (-3) 3),
  (hexagon 1 (-2), down 0 (-3)),
  (hexagon 1 (-2), down 1 (-3)),
  (hexagon 1 (-2), up 0 (-3)),
  (hexagon 2 (-2), down 2 (-3)),
  (hexagon 2 (-2), up 1 (-3)),
  (hexagon 3 (-2), down 3 (-3)),
  (hexagon 3 (-2), up 2 (-3)),
  (hexagon (-2) 4, down (-3) 3),
  (hexagon (-2) 4, down (-2) 4),
  (hexagon (-2) 4, up (-3) 4),
  (hexagon 4 (-2), down 4 (-3)),
  (hexagon 4 (-2), up 3 (-3)),
  (hexagon 4 (-2), up 4 (-2)),
  (down (-3) 0, hexagon (-3) 0),
  (down (-3) 0, hexagon (-3) 1),
  (down (-3) 1, hexagon (-3) 2),
  (down (-3) 2, hexagon (-3) 3),
  (down 0 (-3), hexagon 0 (-3)),
  (down 1 (-3), hexagon 1 (-3)),
  (down 2 (-3), hexagon 2 (-3)),
  (down 3 (-3), hexagon 3 (-3)),
  (down (-3) 3, hexagon (-3) 4),
  (down 4 (-3), hexagon 4 (-3)),
  (hexagon (-3) 0, down (-4) (-1)),
  (hexagon (-3) 0, down (-3) (-1)),
  (hexagon (-3) 0, up (-4) (-1)),
  (hexagon (-3) 0, up (-4) 0),
  (hexagon (-3) 1, down (-4) 0),
  (hexagon (-3) 1, up (-4) 1),
  (hexagon (-3) 2, down (-4) 1),
  (hexagon (-3) 2, up (-4) 2),
  (hexagon (-3) 3, down (-4) 2),
  (hexagon (-3) 3, up (-4) 3),
  (hexagon 0 (-3), down (-1) (-4)),
  (hexagon 0 (-3), down 0 (-4)),
  (hexagon 0 (-3), up (-1) (-4)),
  (hexagon 0 (-3), up (-1) (-3)),
  (hexagon 1 (-3), down 1 (-4)),
  (hexagon 1 (-3), up 0 (-4)),
  (hexagon 2 (-3), down 2 (-4)),
  (hexagon 2 (-3), up 1 (-4)),
  (hexagon 3 (-3), down 3 (-4)),
  (hexagon 3 (-3), up 2 (-4)),
  (hexagon (-3) 4, down (-4) 3),
  (hexagon (-3) 4, down (-3) 4),
  (hexagon (-3) 4, up (-4) 4),
  (hexagon 4 (-3), down 4 (-4)),
  (hexagon 4 (-3), up 3 (-4)),
  (hexagon 4 (-3), up 4 (-3)),
  (down (-4) (-1), hexagon (-4) (-1)),
  (down (-4) (-1), hexagon (-4) 0),
  (down (-3) (-1), hexagon (-3) (-1)),
  (down (-4) 0, hexagon (-4) 1),
  (down (-4) 1, hexagon (-4) 2),
  (down (-4) 2, hexagon (-4) 3),
  (down (-1) (-4), hexagon (-1) (-4)),
  (down (-1) (-4), hexagon (-1) (-3)),
  (down 0 (-4), hexagon 0 (-4)),
  (down 1 (-4), hexagon 1 (-4)),
  (down 2 (-4), hexagon 2 (-4)),
  (down 3 (-4), hexagon 3 (-4)),
  (down (-4) 3, hexagon (-4) 4),
  (down 4 (-4), hexagon 4 (-4)),
  (hexagon (-4) (-1), down (-4) (-2)),
  (hexagon (-3) (-1), down (-3) (-2)),
  (hexagon (-3) (-1), up (-4) (-2)),
  (hexagon (-1) (-4), up (-2) (-4)),
  (hexagon (-1) (-3), down (-2) (-4)),
  (hexagon (-1) (-3), up (-2) (-3)),
  (hexagon (-4) 4, down (-4) 4),
  (hexagon 4 (-4), up 4 (-4)),
  (down (-4) (-2), hexagon (-4) (-2)),
  (down (-3) (-2), hexagon (-3) (-2)),
  (up (-2) (-4), hexagon (-2) (-4)),
  (down (-2) (-4), hexagon (-2) (-3)),
  (hexagon (-4) (-2), down (-4) (-3)),
  (hexagon (-3) (-2), down (-3) (-3)),
  (hexagon (-3) (-2), up (-4) (-3)),
  (hexagon (-2) (-4), up (-3) (-4)),
  (hexagon (-2) (-3), down (-3) (-4)),
  (hexagon (-2) (-3), down (-2) (-3)),
  (hexagon (-2) (-3), up (-3) (-3)),
  (down (-4) (-3), hexagon (-4) (-3)),
  (down (-3) (-3), hexagon (-3) (-3)),
  (up (-3) (-4), hexagon (-3) (-4)),
  (hexagon (-4) (-3), down (-4) (-4)),
  (hexagon (-3) (-3), up (-4) (-4)),
  (down (-4) (-4), hexagon (-4) (-4))]

set_option maxRecDepth 16384 in
set_option maxHeartbeats 0 in
private lemma patch_conn : Connected (exteriorPatch : Set Tile) := by
  have root_mem : hexagon 4 4 ∈ exteriorPatch := by decide
  have visited : visit exteriorPatch patchTree {hexagon 4 4} = exteriorPatch := by decide
  have reachable (x : Tile) (hx : x ∈ exteriorPatch) : Reach (exteriorPatch : Set Tile) (hexagon 4 4) x := by
    apply (visit_sound patchTree (root := hexagon 4 4) (reached := {hexagon 4 4}) ?_ (visited.symm ▸ hx)).2
    intro a ha
    rw [Finset.mem_singleton] at ha
    subst a
    exact ⟨root_mem,.refl _⟩
  intro a b ha hb
  exact .trans _ _ _ (.symm _ _ (reachable a ha)) (reachable b hb)

private lemma nearToAnchor (a : Tile) (ha : a ∈ outside) (hb : a ∈ box 4) :
    Reach outside a (hexagon 4 4) := by
  have h₁ := (patch_mem a).mpr ⟨hb,ha⟩
  have h₂ := (patch_mem (hexagon 4 4)).mpr ⟨(mem_box 4 _).mpr (by norm_num [Tile.coords]),farX _ (Or.inr le_rfl)⟩
  have p := patch_conn h₁ h₂
  exact Relation.EqvGen.mono
    (r := fun u v => u ∈ (exteriorPatch : Set Tile) ∧ v ∈ (exteriorPatch : Set Tile) ∧ Adj u v)
    (p := fun u v => u ∈ outside ∧ v ∈ outside ∧ Adj u v)
    (fun _ _ ⟨hx,hy,he⟩ => ⟨((patch_mem _).mp hx).2,((patch_mem _).mp hy).2,he⟩) _ _ p

private lemma outsideToAnchor (a : Tile) (ha : a ∈ outside) :
    Reach outside a (hexagon 4 4) := by
  by_cases hb : a ∈ box 4
  · exact nearToAnchor a ha hb
  · have hfar : (a.coords.1 ≤ -4 ∨ 4 ≤ a.coords.1) ∨ (a.coords.2 ≤ -4 ∨ 4 ≤ a.coords.2) := by
      rw [mem_box] at hb
      omega
    cases a with
    | hexagon x y => exact farHex x y hfar
    | up x y =>
      have hh : hexagon x y ∈ outside := hfar.elim (farX (hexagon x y)) (farY (hexagon x y))
      exact .trans _ _ _ (step ha hh (by simp [Adj, neighbors])) (farHex x y hfar)
    | down x y =>
      have hh : hexagon x y ∈ outside := hfar.elim (farX (hexagon x y)) (farY (hexagon x y))
      exact .trans _ _ _ (step ha hh (by simp [Adj, neighbors])) (farHex x y hfar)

private lemma outsideConn : Connected outside := by
  intro x y hx hy
  exact .trans _ _ _ (outsideToAnchor x hx) (.symm _ _ (outsideToAnchor y hy))

set_option maxRecDepth 8192 in
set_option maxHeartbeats 0 in
private lemma witness : Enclosing hole shell := by
  refine ⟨⟨hexagon 0 0, by decide⟩, ⟨hexagon (-2) (-2), by decide⟩, by decide,
    finite_conn (start := hexagon 0 0) (n := 3) (by decide) (by decide),
    finite_conn (start := hexagon (-2) (-2)) (n := 22) (by decide) (by decide),
    outsideConn, by decide⟩

theorem counterexample : (31 : ℕ) ∈ holeSizes 23 := by
  exact ⟨hole, shell, by decide, by decide, witness⟩

theorem disproof : ¬ Conjecture := by
  intro h
  have impossible : 31 ≤ 29 := (h 23 (Or.inr (Or.inr ⟨by decide,⟨11,by decide⟩⟩))).2 counterexample
  omega

end ExleyTrihexagonal

#print axioms ExleyTrihexagonal.disproof
