|
| 1 | +/-! |
| 2 | +Regression test for #6332 |
| 3 | +-/ |
| 4 | + |
| 5 | +open Function (uncurry) |
| 6 | +open Std (Range) |
| 7 | + |
| 8 | +section Matrix |
| 9 | + |
| 10 | + structure Matrix (α) where |
| 11 | + array : Array α |
| 12 | + width : Nat |
| 13 | + deriving |
| 14 | + BEq, DecidableEq, Hashable, Inhabited, Nonempty, Repr |
| 15 | + |
| 16 | + instance : GetElem (Matrix α) (Nat × Nat) α |
| 17 | + (fun mat (i, j) => i * mat.width + j < mat.array.size) |
| 18 | + where |
| 19 | + getElem |
| 20 | + | mat, (i, j), h => mat.array[i * mat.width + j] |
| 21 | + |
| 22 | + instance : GetElem? (Matrix α) (Nat × Nat) α |
| 23 | + (fun mat (i, j) => i * mat.width + j < mat.array.size) |
| 24 | + where |
| 25 | + getElem? |
| 26 | + | mat, (i, j) => mat.array[i * mat.width + j]? |
| 27 | + getElem! |
| 28 | + | mat, (i, j) => mat.array[i * mat.width + j]! |
| 29 | + |
| 30 | + namespace Matrix |
| 31 | + def height (mat : Matrix α) := mat.array.size / mat.width |
| 32 | + |
| 33 | + def set! [Inhabited α] (mat : Matrix α) : Nat × Nat -> α -> Matrix α |
| 34 | + | (i, j), x => Matrix.mk (mat.array.set! (i * mat.width + j) x) mat.width |
| 35 | + end Matrix |
| 36 | + |
| 37 | +end Matrix |
| 38 | + |
| 39 | +section Prod |
| 40 | + |
| 41 | + instance [HAdd α1 β1 γ1] [HAdd α2 β2 γ2] |
| 42 | + : HAdd (α1 × α2) (β1 × β2) (γ1 × γ2) where |
| 43 | + hAdd := fun (a1, a2) (b1, b2) => (a1 + b1, a2 + b2) |
| 44 | + |
| 45 | + instance [HSub α1 β1 γ1] [HSub α2 β2 γ2] |
| 46 | + : HSub (α1 × α2) (β1 × β2) (γ1 × γ2) where |
| 47 | + hSub := fun (a1, a2) (b1, b2) => (a1 - b1, a2 - b2) |
| 48 | + |
| 49 | + instance [HAdd α1 β γ1] [HAdd α2 β γ2] |
| 50 | + : HAdd (α1 × α2) β (γ1 × γ2) where |
| 51 | + hAdd := fun (a1, a2) b => (a1 + b, a2 + b) |
| 52 | + |
| 53 | + instance [HSub α1 β γ1] [HSub α2 β γ2] |
| 54 | + : HSub (α1 × α2) β (γ1 × γ2) where |
| 55 | + hSub := fun (a1, a2) b => (a1 - b, a2 - b) |
| 56 | + |
| 57 | + instance [Membership α1 γ1] [Membership α2 γ2] |
| 58 | + : Membership (α1 × α2) (γ1 × γ2) where |
| 59 | + mem | (xs, ys), (x, y) => x ∈ xs /\ y ∈ ys |
| 60 | + |
| 61 | + instance [Membership α1 γ1] [Membership α2 γ2] |
| 62 | + (pair : α1 × α2) (coll : γ1 × γ2) |
| 63 | + [Decidable (pair.fst ∈ coll.fst)] |
| 64 | + [Decidable (pair.snd ∈ coll.snd)] |
| 65 | + : Decidable (pair ∈ coll) := |
| 66 | + inferInstanceAs (Decidable (pair.fst ∈ coll.fst /\ pair.snd ∈ coll.snd)) |
| 67 | + |
| 68 | +end Prod |
| 69 | + |
| 70 | +section Offset |
| 71 | + |
| 72 | + @[unbox] |
| 73 | + structure Offset where |
| 74 | + inner : Int |
| 75 | + deriving |
| 76 | + BEq, DecidableEq, Hashable, Inhabited, |
| 77 | + Nonempty, Ord, TypeName |
| 78 | + |
| 79 | + instance : ToString Offset where |
| 80 | + toString a := toString a.inner |
| 81 | + |
| 82 | + instance (n : Nat) : OfNat Offset n where |
| 83 | + ofNat := Offset.mk $ Int.ofNat n |
| 84 | + |
| 85 | + instance : Neg Offset where |
| 86 | + neg a := Offset.mk $ -a.inner |
| 87 | + |
| 88 | + instance : HSub Nat Offset Nat where |
| 89 | + hSub a b := match b.inner with |
| 90 | + | .ofNat b => a - b |
| 91 | + | .negSucc b => a + b.succ |
| 92 | + |
| 93 | +end Offset |
| 94 | + |
| 95 | +section Range |
| 96 | + |
| 97 | + instance : ToString Range where |
| 98 | + toString r := s!"[{r.start}:{r.stop}:{r.step}]" |
| 99 | + |
| 100 | + instance : Membership Nat Range where |
| 101 | + mem r i := r.start <= i && i < r.stop && (i - r.start) % r.step == 0 |
| 102 | + |
| 103 | + instance (i : Nat) (r : Range) : Decidable (i ∈ r) := |
| 104 | + inferInstanceAs (Decidable (r.start <= i && i < r.stop && (i - r.start) % r.step == 0)) |
| 105 | + |
| 106 | + instance : HAdd Range Nat Range where |
| 107 | + hAdd r d := { r with start := r.start + d, stop := r.stop + d } |
| 108 | + |
| 109 | +end Range |
| 110 | + |
| 111 | +namespace Prod |
| 112 | + |
| 113 | + @[inline] |
| 114 | + def turnRight : Offset × Offset -> Offset × Offset |
| 115 | + | (di, dj) => (dj, -di) |
| 116 | + |
| 117 | + @[inline] |
| 118 | + def turnLeft : Offset × Offset -> Offset × Offset |
| 119 | + | (di, dj) => (-dj, di) |
| 120 | + |
| 121 | +end Prod |
| 122 | + |
| 123 | +inductive Tile |
| 124 | +| space |
| 125 | +| obstruction |
| 126 | +| path (step : Nat) |
| 127 | +deriving Inhabited, BEq |
| 128 | + |
| 129 | +open Tile |
| 130 | + |
| 131 | +def solve (mat : Matrix Tile) (guard : Nat × Nat) := do |
| 132 | + let mut mat := mat |
| 133 | + let mut pos := guard + (1 : Nat) |
| 134 | + let mut dir : Offset × Offset := (-1, 0) |
| 135 | + while pos ∈ borders do |
| 136 | + match mat[pos - (1 : Nat)]! with |
| 137 | + | obstruction => |
| 138 | + pos := pos - dir |
| 139 | + dir := dir.turnRight |
| 140 | + | _ => pure () |
| 141 | + let orig := mat[pos - (1 : Nat)]! |
| 142 | + mat := mat.set! (pos - (1 : Nat)) obstruction |
| 143 | + _ <- searchLoop pos dir.turnLeft |
| 144 | + mat := mat.set! (pos - (1 : Nat)) orig |
| 145 | + break |
| 146 | +where |
| 147 | + borders := ([:mat.height], [:mat.width]) + 1 |
| 148 | + searchLoop (pos : Nat × Nat) (dir : Offset × Offset) := do |
| 149 | + let mut pos := pos |
| 150 | + println!"{pos}" |
| 151 | + println!"{dir}" |
| 152 | + println!"{borders}" |
| 153 | + let mut i := 0 |
| 154 | + -- segfault |
| 155 | + while pos ∈ borders do |
| 156 | + println!"{pos}" |
| 157 | + pos := pos - dir |
| 158 | + i := i + 1 |
| 159 | + pure false |
| 160 | + |
| 161 | +def main := do |
| 162 | + _ <- solve ( |
| 163 | + Matrix.mk (Array.range 100 |>.map fun _ => space) 10 |
| 164 | + ) (6, 4) |
| 165 | + |
| 166 | +/-- |
| 167 | +info: (7, 5) |
| 168 | +(0, -1) |
| 169 | +([1:11:1], [1:11:1]) |
| 170 | +(7, 5) |
| 171 | +(7, 6) |
| 172 | +(7, 7) |
| 173 | +(7, 8) |
| 174 | +(7, 9) |
| 175 | +(7, 10) |
| 176 | +-/ |
| 177 | +#guard_msgs in |
| 178 | +#eval! main |
0 commit comments