code wiki / _hdl_build / nx_pathfind_gate.nx

nx_pathfind_gate.nx source

↩ module page · 67 lines · 3771 B

1// nx_pathfind_gate.nx -- proves A* pathfinding (GR-S-R5 RESEARCHED->PRESENT). (1) OPTIMAL: on an open grid the 2// path length == Manhattan distance; (2) AROUND-WALL: a wall barrier forces a longer but valid path (and every 3// step is to an open, adjacent cell); (3) NO-PATH: a fully-walled target returns -1; (4) NEG-CONTROL: the 4// around-wall path is STRICTLY longer than Manhattan (proving it really detours, not just returns the straight 5// line). Pure-Nishi no-float. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_pathfind.nx" 8 9func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 11func row(id: *u8, ok: i64, pass: *i64) -> i64 { w(" " as *u8); w(id); w(": " as *u8); if ok==1 { w("OK\n" as *u8); pass[0]=pass[0]+1 } else { w("FAIL\n" as *u8) } return 0 } 12 13const GW: i64 = 8 14const GH: i64 = 8 15 16func main() -> i64 { 17 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 18 w("=== NX-PATHFIND GATE (A* grid, pure-Nishi no-float) ===\n" as *u8) 19 let n: i64 = GW*GH 20 let grid: *i64 = sys_mmap(n*8) as *i64 21 let g: *i64 = sys_mmap(n*8) as *i64 22 let f: *i64 = sys_mmap(n*8) as *i64 23 let came: *i64 = sys_mmap(n*8) as *i64 24 let openf: *i64 = sys_mmap(n*8) as *i64 25 let closed: *i64 = sys_mmap(n*8) as *i64 26 let path: *i64 = sys_mmap(n*8) as *i64 27 28 // (1) open grid: (0,0)->(7,7), Manhattan=14, A* must find 14 29 var i: i64 = 0; while i < n { grid[i]=0; i=i+1 } 30 let l1: i64 = pf_astar(grid, GW, GH, 0,0, 7,7, g,f,came,openf,closed, 0 as *i64) 31 w(" open grid (0,0)->(7,7): len="); wn(l1); w(" (Manhattan=14)\n") 32 row("OPTIMAL: open-grid path == Manhattan distance (14)\x00" as *u8, (l1==14) as i64, pass) 33 34 // (2) wall barrier: a vertical wall at x=4 for y=0..6 (gap at y=7) -> path must detour down then across 35 i=0; while i < n { grid[i]=0; i=i+1 } 36 var y: i64 = 0; while y <= 6 { grid[y*GW + 4]=1; y=y+1 } 37 let l2: i64 = pf_astar(grid, GW, GH, 0,0, 7,0, g,f,came,openf,closed, path) 38 w(" wall barrier (0,0)->(7,0): len="); wn(l2); w(" (Manhattan=7)\n") 39 // validate: every consecutive path cell is open + adjacent (4-neighbour) 40 var valid: i64 = 1 41 if l2 < 0 { valid = 0 } else { 42 var k: i64 = 0 43 while k <= l2 { 44 if grid[path[k]] != 0 { valid = 0 } 45 if k > 0 { 46 let ax: i64 = path[k-1]%GW; let ay: i64 = path[k-1]/GW 47 let bx: i64 = path[k]%GW; let by: i64 = path[k]/GW 48 let man: i64 = pf_abs(ax-bx)+pf_abs(ay-by) 49 if man != 1 { valid = 0 } 50 } 51 k = k + 1 52 } 53 } 54 row("AROUND-WALL: a valid path exists, every step open + adjacent\x00" as *u8, ((l2>0) as i64) & valid, pass) 55 row("NEG-CONTROL: the detour is STRICTLY longer than Manhattan (really routes around)\x00" as *u8, (l2 > 7) as i64, pass) 56 57 // (3) no path: fully wall off the target (1,1) with the border around it 58 i=0; while i < n { grid[i]=0; i=i+1 } 59 grid[0*GW+1]=1; grid[1*GW+0]=1; grid[1*GW+2]=1; grid[2*GW+1]=1 // surround (1,1) 60 let l3: i64 = pf_astar(grid, GW, GH, 7,7, 1,1, g,f,came,openf,closed, 0 as *i64) 61 w(" walled-off target: len="); wn(l3); w(" (expect -1)\n") 62 row("NO-PATH: a fully-walled target returns -1 (no fabricated route)\x00" as *u8, (l3 == 0-1) as i64, pass) 63 64 w("PATHFIND rows=4 pass="); wn(pass[0]) 65 if pass[0]==4 { w(" verdict=GREEN\n"); sys_exit(0); return 0 } 66 w(" verdict=RED\n"); sys_exit(1); return 1 67}