{
"id": "267",
"prompt": "A tiny foraging ecosystem: 16 grass columns regrow one unit every fourth frame (capped), while a sheep walks the field eating. The sheep AI is greedy: eat the current column two units per frame while it has grass, and when it is bare, scan up to 5 columns either side and walk toward the tallest patch (2px per frame). Its belly fills by eating and drains slowly with time, shown as a bar. Grass heights and the sheep's x, target and belly all persist in state slots; draw the sheep standing on top of the grass at its column.",
"solution_rl": "fn sgn(v: i32) -> i32 {\n if v > 0 { return 1; }\n if v < 0 { return -1; }\n 0\n}\n\nfn iabs(v: i32) -> i32 {\n if v < 0 { -v } else { v }\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let colw: i32 = w / 16;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut i: i32 = 0;\n while i < 16 {\n host::display::state_set(8 + i, 40 + (i * 13) % 37);\n i = i + 1;\n }\n host::display::state_set(2, w / 2);\n host::display::state_set(3, 8);\n host::display::state_set(4, 60);\n }\n let mut grass = [0; 16];\n let mut i: i32 = 0;\n while i < 16 {\n grass[i] = host::display::state_get(8 + i);\n if t % 4 == 0 && grass[i] < 90 { grass[i] = grass[i] + 1; }\n i = i + 1;\n }\n let mut x: i32 = host::display::state_get(2);\n let mut target: i32 = host::display::state_get(3);\n let mut belly: i32 = host::display::state_get(4);\n let goal: i32 = target * colw + colw / 2;\n if iabs(x - goal) <= 2 {\n if grass[target] > 2 {\n grass[target] = grass[target] - 2;\n if belly < 99 { belly = belly + 1; }\n } else {\n let cur: i32 = x / colw;\n let mut best: i32 = cur;\n let mut bestv: i32 = -1;\n let mut c: i32 = cur - 5;\n while c <= cur + 5 {\n if c >= 0 && c < 16 && grass[c] > bestv {\n bestv = grass[c];\n best = c;\n }\n c = c + 1;\n }\n target = best;\n }\n } else {\n x = x + sgn(goal - x) * 2;\n }\n if t % 8 == 0 && belly > 0 { belly = belly - 1; }\n i = 0;\n while i < 16 {\n host::display::state_set(8 + i, grass[i]);\n i = i + 1;\n }\n host::display::state_set(2, x);\n host::display::state_set(3, target);\n host::display::state_set(4, belly);\n host::display::clear(0x101418);\n let base: i32 = h - 20;\n i = 0;\n while i < 16 {\n host::display::fill_rect(i * colw, base - grass[i] * 3, colw - 2, grass[i] * 3, 0x2a7a34);\n i = i + 1;\n }\n host::display::fill_rect(0, base, w, 4, 0x40382c);\n let scol: i32 = x / colw;\n let mut sc: i32 = scol;\n if sc < 0 { sc = 0; }\n if sc > 15 { sc = 15; }\n let sy: i32 = base - grass[sc] * 3 - 18;\n host::display::fill_rect(x - 14, sy, 28, 16, 0xe8e8e0);\n host::display::fill_rect(x + 10, sy - 6, 10, 10, 0x303030);\n host::display::draw_string(8, 8, \"BELLY\", 0x808080, 1);\n host::display::fill_rect(60, 8, belly, 8, 0xd0a040);\n host::display::present();\n}\n",
"tags": [
"simulation",
"ecosystem",
"agent",
"state"
]
}