{
"id": "246",
"prompt": "Predator-prey population cycles, integer-only Lotka-Volterra: rabbits and foxes live in two state slots (stored x256 so the tiny per-frame derivatives survive integer math). Each frame rabbits gain from breeding minus getting eaten, foxes gain from eating minus starvation - use the shared R*F>>8 interaction term, divide the deltas down hard so the cycle takes a couple hundred frames, and clamp both to 64..8192 so nothing explodes or goes extinct. Show live bars plus numbers for both populations AND a phase-portrait panel where a ring buffer of 8 recent (R,F) samples in state slots traces the orbit.",
"solution_rl": "fn 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 host::display::state_set(1, 1536);\n host::display::state_set(2, 384);\n }\n let mut r: i32 = host::display::state_get(1);\n let mut f: i32 = host::display::state_get(2);\n let rf: i32 = (r * f) >> 8;\n let dr: i32 = (r * 2 - rf) / 64;\n let df: i32 = (rf - f * 4) / 128;\n r = r + dr;\n f = f + df;\n if r < 64 { r = 64; }\n if r > 8192 { r = 8192; }\n if f < 64 { f = 64; }\n if f > 8192 { f = 8192; }\n host::display::state_set(1, r);\n host::display::state_set(2, f);\n if t % 12 == 0 {\n let head: i32 = host::display::state_get(3);\n host::display::state_set(16 + head * 2, r);\n host::display::state_set(17 + head * 2, f);\n host::display::state_set(3, (head + 1) % 8);\n }\n host::display::clear(0x0c100c);\n host::display::draw_string(20, 20, \"RABBITS\", 0x60c060, 1);\n host::display::fill_rect(20, 34, r / 16, 12, 0x40a040);\n host::display::draw_number(20 + r / 16 + 8, 32, r / 16, 0xffffff, 1);\n host::display::draw_string(20, 60, \"FOXES\", 0xd08040, 1);\n host::display::fill_rect(20, 74, f / 16, 12, 0xc06020);\n host::display::draw_number(20 + f / 16 + 8, 72, f / 16, 0xffffff, 1);\n let bx: i32 = w - 220;\n let by: i32 = h - 220;\n host::display::fill_rect(bx, by, 200, 200, 0x141a14);\n let mut i: i32 = 0;\n while i < 8 {\n let pr: i32 = host::display::state_get(16 + i * 2);\n let pf: i32 = host::display::state_get(17 + i * 2);\n if pr > 0 {\n host::display::fill_rect(bx + pr / 48, by + 196 - pf / 48, 3, 3, 0x506050);\n }\n i = i + 1;\n }\n host::display::fill_rect(bx + r / 48 - 2, by + 196 - f / 48 - 2, 5, 5, 0xffffff);\n host::display::draw_string(bx, by - 16, \"PHASE ORBIT\", 0x606060, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"predator-prey",
"state",
"fixed-point"
]
}