{
"id": "248",
"prompt": "A sandpile with real avalanches: 16 column heights persist in state slots (there are no persistent arrays in rustlite). Every third frame one grain drops into the column under pointer_x. Then run six relaxation passes: any column more than 2 grains taller than a neighbor sheds one grain that way - so pouring in one spot builds a cone that periodically slumps sideways. When the tallest pile nears the top of the screen, subtract one grain from every column so the bed sinks. Draw the sand stacks and a spout marker over the pour column.",
"solution_rl": "fn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let colw: i32 = w / 16;\n let mut arr = [0; 16];\n let mut i: i32 = 0;\n while i < 16 {\n arr[i] = host::display::state_get(8 + i);\n i = i + 1;\n }\n let mut pour: i32 = host::display::pointer_x() / colw;\n if pour < 0 { pour = 0; }\n if pour > 15 { pour = 15; }\n if t % 3 == 0 {\n arr[pour] = arr[pour] + 1;\n }\n let mut pass: i32 = 0;\n while pass < 6 {\n i = 0;\n while i < 16 {\n if i < 15 && arr[i] > arr[i + 1] + 2 {\n arr[i] = arr[i] - 1;\n arr[i + 1] = arr[i + 1] + 1;\n }\n if i > 0 && arr[i] > arr[i - 1] + 2 {\n arr[i] = arr[i] - 1;\n arr[i - 1] = arr[i - 1] + 1;\n }\n i = i + 1;\n }\n pass = pass + 1;\n }\n let mut top: i32 = 0;\n i = 0;\n while i < 16 {\n if arr[i] > top { top = arr[i]; }\n i = i + 1;\n }\n if top * 4 > h - 40 {\n i = 0;\n while i < 16 {\n if arr[i] > 0 { arr[i] = arr[i] - 1; }\n i = i + 1;\n }\n }\n i = 0;\n while i < 16 {\n host::display::state_set(8 + i, arr[i]);\n i = i + 1;\n }\n host::display::clear(0x14100c);\n i = 0;\n while i < 16 {\n let ch: i32 = arr[i] * 4;\n host::display::fill_rect(i * colw, h - ch, colw - 1, ch, 0xc8a050);\n i = i + 1;\n }\n host::display::fill_rect(pour * colw + colw / 2 - 3, 10, 6, 10, 0xf0e0b0);\n host::display::draw_string(8, 30, \"POINT TO POUR\", 0x707070, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"sand",
"automaton",
"state"
]
}