{
"id": "250",
"prompt": "An epidemic on a 16x12 grid, SIR style: each row is one 16-bit mask, so keep 12 infected-row masks and 12 immune-row masks in state slots. Every 10 frames run a generation: each susceptible cell counts its infected orthogonal neighbors and catches the disease with probability n*45/128 (one LCG roll per exposed cell); everyone currently infected then becomes immune. When the infection dies out, wipe the grid and reseed the center cell so the wave restarts. Paint susceptible cells dark green, infected red, recovered gray, and show live S / I / R counts via a popcount helper.",
"solution_rl": "fn bits(v: i32) -> i32 {\n let mut n: i32 = 0;\n let mut x: i32 = v;\n let mut i: i32 = 0;\n while i < 16 {\n n = n + (x & 1);\n x = x >> 1;\n i = i + 1;\n }\n n\n}\n\nfn frame(t: i32) {\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut r: i32 = 0;\n while r < 12 {\n host::display::state_set(8 + r, 0);\n host::display::state_set(20 + r, 0);\n r = r + 1;\n }\n host::display::state_set(14, 1 << 8);\n host::display::state_set(2, 991);\n }\n let mut inf = [0; 12];\n let mut imm = [0; 12];\n let mut r: i32 = 0;\n while r < 12 {\n inf[r] = host::display::state_get(8 + r);\n imm[r] = host::display::state_get(20 + r);\n r = r + 1;\n }\n if t % 10 == 0 {\n let mut newi = [0; 12];\n let mut seed: i32 = host::display::state_get(2);\n r = 0;\n while r < 12 {\n let mut c: i32 = 0;\n while c < 16 {\n let bit: i32 = 1 << c;\n if (inf[r] & bit) == 0 && (imm[r] & bit) == 0 {\n let mut n: i32 = 0;\n if c > 0 { n = n + ((inf[r] >> (c - 1)) & 1); }\n if c < 15 { n = n + ((inf[r] >> (c + 1)) & 1); }\n if r > 0 { n = n + ((inf[r - 1] >> c) & 1); }\n if r < 11 { n = n + ((inf[r + 1] >> c) & 1); }\n if n > 0 {\n seed = seed * 1103515245 + 12345;\n if ((seed >> 16) & 127) < n * 45 {\n newi[r] = newi[r] | bit;\n }\n }\n }\n c = c + 1;\n }\n r = r + 1;\n }\n let mut icount: i32 = 0;\n r = 0;\n while r < 12 {\n imm[r] = imm[r] | inf[r];\n inf[r] = newi[r];\n icount = icount + bits(inf[r]);\n r = r + 1;\n }\n if icount == 0 {\n r = 0;\n while r < 12 {\n imm[r] = 0;\n inf[r] = 0;\n r = r + 1;\n }\n inf[6] = 1 << 8;\n }\n r = 0;\n while r < 12 {\n host::display::state_set(8 + r, inf[r]);\n host::display::state_set(20 + r, imm[r]);\n r = r + 1;\n }\n host::display::state_set(2, seed);\n }\n host::display::clear(0x0a0a0a);\n let mut ni: i32 = 0;\n let mut nr: i32 = 0;\n r = 0;\n while r < 12 {\n let mut c: i32 = 0;\n while c < 16 {\n let mut col: i32 = 0x104020;\n if ((inf[r] >> c) & 1) == 1 { col = 0xff4030; ni = ni + 1; }\n else if ((imm[r] >> c) & 1) == 1 { col = 0x606060; nr = nr + 1; }\n host::display::fill_rect(64 + c * 24, 100 + r * 24, 22, 22, col);\n c = c + 1;\n }\n r = r + 1;\n }\n let ns: i32 = 192 - ni - nr;\n host::display::draw_string(64, 30, \"S\", 0x40c060, 2);\n host::display::draw_number(84, 30, ns, 0x40c060, 2);\n host::display::draw_string(180, 30, \"I\", 0xff4030, 2);\n host::display::draw_number(200, 30, ni, 0xff4030, 2);\n host::display::draw_string(300, 30, \"R\", 0x909090, 2);\n host::display::draw_number(320, 30, nr, 0x909090, 2);\n host::display::present();\n}\n",
"tags": [
"simulation",
"epidemic",
"bitmask",
"prng"
]
}