- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 121 lines of Turing from the credited upstream file ccc05j4.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123456789 10var g : array 0 .. 21, 0 .. 21 of boolean11var w, h, cw, ch : int12var c, r, direction : int13var steps : int14var moving : boolean15 16get w, h, cw, ch, steps1718for i : 0 .. 2119 for j : 0 .. 2120 if i >= 1 and i <= h and j >= 1 and j <= w and21 not ( (i <= ch and (j <= cw or j > w - cw)) or22 (i > h - ch and (j <= cw or j > w - cw))) then23 g (i, j) := true24 else25 g (i, j) := false26 end if27 end for28end for29 3031323334353637383940414243 44c := cw + 145r := 146direction := 047for i : 1 .. steps48 g (r, c) := false49 moving := true50 if direction = 0 then51 if g (r - 1, c) then52 r := r - 153 direction := 9054 elsif g (r, c + 1) then55 c := c + 156 direction := 057 elsif g (r + 1, c) then58 r := r + 159 direction := 27060 elsif g (r, c - 1) then61 c := c - 162 direction := 18063 else64 moving := false65 end if66 elsif direction = 90 then67 if g (r, c - 1) then68 c := c - 169 direction := 18070 elsif g (r - 1, c) then71 r := r - 172 direction := 9073 elsif g (r, c + 1) then74 c := c + 175 direction := 076 elsif g (r + 1, c) then77 r := r + 178 direction := 27079 else80 moving := false81 end if82 elsif direction = 180 then83 if g (r + 1, c) then84 r := r + 185 direction := 27086 elsif g (r, c - 1) then87 c := c - 188 direction := 18089 elsif g (r - 1, c) then90 r := r - 191 direction := 9092 elsif g (r, c + 1) then93 c := c + 194 direction := 095 else96 moving := false97 end if98 elsif direction = 270 then99 if g (r, c + 1) then100 c := c + 1101 direction := 0102 elsif g (r + 1, c) then103 r := r + 1104 direction := 270105 elsif g (r, c - 1) then106 c := c - 1107 direction := 180108 elsif g (r - 1, c) then109 r := r - 1110 direction := 90111 else112 moving := false113 end if114 end if115 exit when not moving116 117end for118put c119put r120 121