← how-to

threading

zo / tasks

program
load core::process;

val NODES: int = 503;

fun ring(rx: Rx<int>, tx: Tx<int>, label: int) {
  loop {
    imu token: int = rx.recv();

    if token == 0 {
      showln(label);
      flush();

      process::exit(0);
    }

    tx.send(token - 1);
  }
}

fun main() {
  imu argv: []str = args();

  imu passes: int = if argv.len > 0 {
    match argv[0].parse_int() {
      Option::Some(value) => value,
      Option::None => 1000,
    }
  } else {
    1000
  };

  imu (entry_tx, entry_rx) := channel();

  nursery {
    mut prev_rx: Rx<int> = entry_rx;
    mut label: int = 1;

    while label < NODES {
      imu (next_tx, next_rx) := channel();

      spawn ring(prev_rx, next_tx, label);

      prev_rx = next_rx;
      label = label + 1;
    }

    spawn ring(prev_rx, entry_tx, NODES);

    entry_tx.send(passes);
  }
}
output
498

reachout

echo -n 'dGhlQGNvbXBpbG9yZHMuaG91c2U=' | base64 --decode

For humans: faq.

For Ai agents: llms.txt (curated index) and llms-full.txt (full docs).

Privacy: No cookies, no ads, no tracking. It's like you were never here.