{
"id": "238",
"prompt": "Your cursor is the hawk: five birds drift back toward the middle of the screen, but while the pointer is held down any bird within 120px of it (squared-distance test, no sqrt) flees, accelerating directly away. Damp every velocity by 7/8 each frame so motion stays smooth, clamp positions to the screen, and print how many birds are scared right now. Per-bird x/y/vx/vy persist in arithmetic state-slot groups because locals reset every frame.",
"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 < 5 {\n host::display::state_set(8 + i * 4, (100 + i * 60) * 8);\n host::display::state_set(9 + i * 4, (120 + (i % 3) * 90) * 8);\n i = i + 1;\n }\n }\n let px: i32 = host::display::pointer_x();\n let py: i32 = host::display::pointer_y();\n let hot: bool = host::display::pointer_down() == 1;\n host::display::clear(0x101418);\n let mut scared: i32 = 0;\n let mut i: i32 = 0;\n while i < 5 {\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 let dx: i32 = x / 8 - px;\n let dy: i32 = y / 8 - py;\n let d2: i32 = dx * dx + dy * dy;\n if hot && d2 < 14400 {\n vx = vx + sgn(dx) * 6;\n vy = vy + sgn(dy) * 6;\n scared = scared + 1;\n } else {\n vx = vx + (w * 4 - x) / 128;\n vy = vy + (h * 4 - y) / 128;\n }\n vx = vx * 7 / 8;\n vy = vy * 7 / 8;\n x = x + vx;\n y = y + vy;\n if x < 80 { x = 80; }\n if x > (w - 10) * 8 { x = (w - 10) * 8; }\n if y < 80 { y = 80; }\n if y > (h - 10) * 8 { y = (h - 10) * 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 - 5, y / 8 - 5, 10, 10, 0xe0e0d0);\n i = i + 1;\n }\n if hot {\n host::display::fill_rect(px - 5, py - 5, 10, 10, 0xff3030);\n }\n host::display::draw_string(8, 8, \"SCARED\", 0x808080, 1);\n host::display::draw_number(70, 8, scared, 0xff5050, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"boids",
"pointer",
"state"
]
}