{
"id": "237",
"prompt": "Boids on a budget: four birds that flock. Each bird steers gently toward the group centroid (1/64 of the offset per frame) but gets shoved away from any flockmate within a 30px box - a sign-based push, since rustlite has no sqrt for real normalization. Clamp velocity so nobody rockets off and wrap the world toroidally. No structs and no globals here, so bird i owns state slots 8+i*4 holding x, y, vx, vy in 1/8-px fixed point. Give each bird its own palette color and mark the centroid with a small gray dot.",
"solution_rl": "fn sgn(v: i32) -> i32 {\n if v > 0 { return 1; }\n if v < 0 { return -1; }\n 0\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut i: i32 = 0;\n while i < 4 {\n host::display::state_set(8 + i * 4, (80 + i * 90) * 8);\n host::display::state_set(9 + i * 4, (70 + i * 80) * 8);\n host::display::state_set(10 + i * 4, 10 - i * 5);\n host::display::state_set(11 + i * 4, 4 + i * 3);\n i = i + 1;\n }\n }\n let mut sx: i32 = 0;\n let mut sy: i32 = 0;\n let mut i: i32 = 0;\n while i < 4 {\n sx = sx + host::display::state_get(8 + i * 4);\n sy = sy + host::display::state_get(9 + i * 4);\n i = i + 1;\n }\n let cx: i32 = sx / 4;\n let cy: i32 = sy / 4;\n host::display::clear(0x08101c);\n let pal: [i32; 4] = [0xff6050, 0x60ff70, 0x60a0ff, 0xffd050];\n i = 0;\n while i < 4 {\n let b: i32 = 8 + i * 4;\n let mut x: i32 = host::display::state_get(b);\n let mut y: i32 = host::display::state_get(b + 1);\n let mut vx: i32 = host::display::state_get(b + 2);\n let mut vy: i32 = host::display::state_get(b + 3);\n vx = vx + (cx - x) / 64;\n vy = vy + (cy - y) / 64;\n let mut j: i32 = 0;\n while j < 4 {\n if j != i {\n let ox: i32 = host::display::state_get(8 + j * 4);\n let oy: i32 = host::display::state_get(9 + j * 4);\n let dx: i32 = x - ox;\n let dy: i32 = y - oy;\n if dx > -240 && dx < 240 && dy > -240 && dy < 240 {\n vx = vx + sgn(dx) * 3;\n vy = vy + sgn(dy) * 3;\n }\n }\n j = j + 1;\n }\n if vx > 22 { vx = 22; }\n if vx < -22 { vx = -22; }\n if vy > 22 { vy = 22; }\n if vy < -22 { vy = -22; }\n x = x + vx;\n y = y + vy;\n if x < 0 { x = x + w * 8; }\n if x >= w * 8 { x = x - w * 8; }\n if y < 0 { y = y + h * 8; }\n if y >= h * 8 { y = y - h * 8; }\n host::display::state_set(b, x);\n host::display::state_set(b + 1, y);\n host::display::state_set(b + 2, vx);\n host::display::state_set(b + 3, vy);\n host::display::fill_rect(x / 8 - 4, y / 8 - 4, 8, 8, pal[i]);\n i = i + 1;\n }\n host::display::fill_rect(cx / 8 - 1, cy / 8 - 1, 3, 3, 0x405060);\n host::display::present();\n}\n",
"tags": [
"simulation",
"boids",
"state",
"fixed-point"
]
}