{
"id": "263",
"prompt": "Firefly synchronization: eight fireflies each carry a 0..99 phase counter in a state slot, all ticking up every frame and flashing when they roll over. The coupling rule: the moment any firefly fires, every OTHER firefly jumps its phase forward by 3 per flash it witnessed (capped just below rollover so it fires next frame instead of double-counting). Start the phases deliberately scattered and the swarm should slowly clump until all eight blink as one. Glowing halo while a fly's phase is under 8, brightness ramping with phase otherwise, plus a FLASHING NOW count that hits 8 when synchrony arrives.",
"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 let mut i: i32 = 0;\n while i < 8 {\n host::display::state_set(8 + i, (i * 37 + 11) % 100);\n i = i + 1;\n }\n }\n let mut ph = [0; 8];\n let mut i: i32 = 0;\n while i < 8 {\n ph[i] = host::display::state_get(8 + i);\n i = i + 1;\n }\n let mut fired: i32 = 0;\n i = 0;\n while i < 8 {\n ph[i] = ph[i] + 1;\n if ph[i] >= 100 {\n ph[i] = 0;\n fired = fired + 1;\n }\n i = i + 1;\n }\n if fired > 0 {\n i = 0;\n while i < 8 {\n if ph[i] != 0 {\n ph[i] = ph[i] + fired * 3;\n if ph[i] >= 100 { ph[i] = 99; }\n }\n i = i + 1;\n }\n }\n let mut lit: i32 = 0;\n i = 0;\n while i < 8 {\n host::display::state_set(8 + i, ph[i]);\n if ph[i] < 8 { lit = lit + 1; }\n i = i + 1;\n }\n host::display::clear(0x060a06);\n i = 0;\n while i < 8 {\n let x: i32 = w / 5 + (i % 4) * (w / 5);\n let y: i32 = h / 3 + (i / 4) * (h / 3);\n if ph[i] < 8 {\n host::display::fill_rect(x - 14, y - 14, 28, 28, 0x404810);\n host::display::fill_rect(x - 7, y - 7, 14, 14, 0xf0ff50);\n } else {\n let g: i32 = 30 + ph[i];\n host::display::fill_rect(x - 5, y - 5, 10, 10, g * 65536 + g * 256 + g / 2);\n }\n i = i + 1;\n }\n host::display::draw_string(8, 8, \"FLASHING NOW\", 0x607060, 1);\n host::display::draw_number(130, 8, lit, 0xf0ff50, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"oscillators",
"state",
"emergence"
]
}