code wiki / _hdl_build / _claude_ch_census.nx
_claude_ch_census.nx source
↩ module page · 76 lines · 2796 B
1// _claude_ch_census.nx -- CLAUDE's lane in the CH-census autonomy race (handwritten,
2// independent of the team's emitter output; different structure on purpose: ONE pass fills
3// the whole census instead of count + five finds). Same contract: read /tmp/race_input.bin;
4// print EXTN= then SNI=/GR=/SG=/SV=/KS= (body offsets; -1 absent; -2 when refused).
5// license_tier: ORIGINAL
6import "nx_syscalls.nx"
7
8func cc_dec(label: *u8, v: i64) -> i64 {
9 var n: i64 = 0
10 while label[n] != (0 as u8) { n = n + 1 }
11 sys_write(1, label, n)
12 var av: i64 = v
13 if av < 0 {
14 sys_write(1, "-" as *u8, 1)
15 av = 0 - av
16 }
17 if av == 0 { sys_write(1, "0" as *u8, 1) }
18 if av > 0 {
19 let buf: *u8 = sys_mmap(32)
20 var pos: i64 = 0
21 var x: i64 = av
22 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }
23 let out: *u8 = sys_mmap(32)
24 var oi: i64 = 0
25 while oi < pos { out[oi] = buf[pos - 1 - oi]; oi = oi + 1 }
26 sys_write(1, out, pos)
27 }
28 sys_write(1, "\n" as *u8, 1)
29 return 0
30}
31
32// one pass: count entries and record the FIRST body offset of each watched type.
33// out[0]=count(-1 refused), out[1..5]= SNI/GR/SG/SV/KS body offsets (-1 absent).
34func cc_walk(b: *u8, n: i64, out: *i64) -> i64 {
35 out[0] = 0
36 out[1] = 0 - 1; out[2] = 0 - 1; out[3] = 0 - 1; out[4] = 0 - 1; out[5] = 0 - 1
37 var off: i64 = 0
38 while off < n {
39 if off + 4 > n { out[0] = 0 - 1; return 0 - 1 }
40 let ty: i64 = ((b[off] & 0xff) << 8) | (b[off + 1] & 0xff)
41 let l: i64 = ((b[off + 2] & 0xff) << 8) | (b[off + 3] & 0xff)
42 if off + 4 + l > n { out[0] = 0 - 1; return 0 - 1 }
43 let body: i64 = off + 4
44 if ty == 0 { if out[1] < 0 { out[1] = body } }
45 if ty == 10 { if out[2] < 0 { out[2] = body } }
46 if ty == 13 { if out[3] < 0 { out[3] = body } }
47 if ty == 43 { if out[4] < 0 { out[4] = body } }
48 if ty == 51 { if out[5] < 0 { out[5] = body } }
49 out[0] = out[0] + 1
50 off = body + l
51 }
52 return out[0]
53}
54
55func main() -> i64 {
56 let lenp: *i64 = sys_mmap(16) as *i64
57 let b: *u8 = sys_read_file("/tmp/race_input.bin\x00" as *u8, lenp)
58 let n: i64 = lenp[0]
59 let out: *i64 = sys_mmap(64) as *i64
60 let c: i64 = cc_walk(b, n, out)
61 cc_dec("EXTN=\x00" as *u8, out[0])
62 if c < 0 {
63 cc_dec("SNI=\x00" as *u8, 0 - 2)
64 cc_dec("GR=\x00" as *u8, 0 - 2)
65 cc_dec("SG=\x00" as *u8, 0 - 2)
66 cc_dec("SV=\x00" as *u8, 0 - 2)
67 cc_dec("KS=\x00" as *u8, 0 - 2)
68 return 0
69 }
70 cc_dec("SNI=\x00" as *u8, out[1])
71 cc_dec("GR=\x00" as *u8, out[2])
72 cc_dec("SG=\x00" as *u8, out[3])
73 cc_dec("SV=\x00" as *u8, out[4])
74 cc_dec("KS=\x00" as *u8, out[5])
75 return 0
76}