| 1 | module |
| 2 | |
| 3 | public import Mathlib |
| 4 | |
| 5 | @[expose] public section |
| 6 | |
| 7 | /-! |
| 8 | # Input, part 3: the controlled subdivision process |
| 9 | |
| 10 | A *node* is a pair `(ν, N)`: an exponent vector `ν` with slope `Q = 2^e2 3^e3 5^e5 7^e7`, and `N : ℕ`. |
| 11 | It stands for the interval `[N, N+1) / (36 Q)`, for the state (cell) `N % 36`, and for the affine map |
| 12 | `x ↦ Q x - N / 36` (floor). One step picks the letter `a = act ν (N % 36)` and cuts the interval into |
| 13 | `a` equal parts: the children are `(ν · a, a N + r)`, `r < a`. |
| 14 | -/ |
| 15 | |
| 16 | set_option linter.style.header false |
| 17 | open Finset |
| 18 | |
| 19 | namespace E424.Inp |
| 20 | |
| 21 | /-- Exponents of 2, 3, 5, 7. -/ |
| 22 | @[ext] structure Ex where |
| 23 | e2 : ℕ |
| 24 | e3 : ℕ |
| 25 | e5 : ℕ |
| 26 | e7 : ℕ |
| 27 | deriving DecidableEq |
| 28 | |
| 29 | namespace Ex |
| 30 | |
| 31 | def slope (ν : Ex) : ℕ := 2 ^ ν.e2 * 3 ^ ν.e3 * 5 ^ ν.e5 * 7 ^ ν.e7 |
| 32 | |
| 33 | /-- Total number of prime factors: the clock. -/ |
| 34 | def om (ν : Ex) : ℕ := ν.e2 + ν.e3 + ν.e5 + ν.e7 |
| 35 | |
| 36 | /-- Multiply by a letter. -/ |
| 37 | def mul (ν : Ex) (a : ℕ) : Ex := |
| 38 | if a = 2 then { ν with e2 := ν.e2 + 1 } |
| 39 | else if a = 3 then { ν with e3 := ν.e3 + 1 } |
| 40 | else if a = 5 then { ν with e5 := ν.e5 + 1 } |
| 41 | else if a = 9 then { ν with e3 := ν.e3 + 2 } |
| 42 | else { ν with e2 := ν.e2 + 1, e7 := ν.e7 + 1 } |
| 43 | |
| 44 | end Ex |
| 45 | |
| 46 | /-- The three imbalances. They vanish at multiples of `(24, 27, 12, 2)`. -/ |
| 47 | def h1 (ν : Ex) : ℤ := 2 * ν.e2 - 2 * ν.e3 + 2 * ν.e5 - 9 * ν.e7 |
| 48 | def h2 (ν : Ex) : ℤ := ν.e2 - 2 * ν.e3 + ν.e5 + 9 * ν.e7 |
| 49 | def h3 (ν : Ex) : ℤ := ν.e2 - 2 * ν.e5 |
| 50 | |
| 51 | /-- The signed imbalances `h1, -h1, h2, -h2, h3, -h3`. -/ |
| 52 | def g (κ : Fin 6) (ν : Ex) : ℤ := ![h1 ν, -h1 ν, h2 ν, -h2 ν, h3 ν, -h3 ν] κ |
| 53 | |
| 54 | /-- `Ω` of a letter. -/ |
| 55 | def ell (a : ℕ) : ℕ := if a = 9 ∨ a = 14 then 2 else 1 |
| 56 | |
| 57 | /-- The letter used at state `j`. Only states 13, 20, 23 look at the imbalance. -/ |
| 58 | def act (ν : Ex) (j : ℕ) : ℕ := |
| 59 | if j ≤ 4 then 14 else if j ≤ 7 then 9 else if j ≤ 12 then 5 |
| 60 | else if j = 13 then (if 0 < h1 ν then 3 else 5) |
| 61 | else if j ≤ 19 then 3 |
| 62 | else if j = 20 then (if 0 < h2 ν then 3 else 2) |
| 63 | else if j = 21 then 3 |
| 64 | else if j = 22 then 2 |
| 65 | else if j = 23 then (if 0 < h3 ν then 2 else 3) |
| 66 | else 2 |
| 67 | |
| 68 | /-- The states. -/ |
| 69 | def Alive (j : ℕ) : Prop := 3 ≤ j ∧ j < 36 |
| 70 | |
| 71 | /-- The stopped tree: subdivide (at most `n` times) until `Ω ≥ L`. -/ |
| 72 | def leaves (L : ℕ) : ℕ → Ex → ℕ → Finset (Ex × ℕ) |
| 73 | | 0, ν, N => {(ν, N)} |
| 74 | | n + 1, ν, N => |
| 75 | if L ≤ ν.om then {(ν, N)} |
| 76 | else (range (act ν (N % 36))).biUnion fun r => |
| 77 | leaves L n (ν.mul (act ν (N % 36))) (act ν (N % 36) * N + r) |
| 78 | |
| 79 | end E424.Inp |
| 80 | |