===== ISTEM =====
Solve the following programming task.

TASK: TIK Register Machine

Write a function 'runTik(program)' that runs a program written in TIK, a small made-up language, and returns an array of the numbers it outputs.

Program text:
- Instructions are separated by newlines. Leading and trailing spaces on a line are ignored.
- Lines that are empty (or only spaces) and lines whose first non-space character is '#' are ignored and are not instructions.
- Within an instruction, tokens are separated by one or more spaces.

Machine:
- There are four registers named a, b, c and d. All start at 0.
- Every value stored in a register is reduced to the range 0..999 by taking it modulo 1000, always giving a non-negative result (for example -1 becomes 999 and 1003 becomes 3).
- An integer literal is an optional '-' followed by one or more digits ('+5' and '5.0' are not valid).
- An operand written as X below is either a register name or an integer literal. Its value is the register's value, or the literal reduced to 0..999 in the same way.

Instructions (R must be a register name, N must be an integer literal):
- set R X: R becomes the value of X.
- add R X: R becomes R plus the value of X.
- mul R X: R becomes R times the value of X.
- out X: appends the value of X to the output.
- jmp X N: if the value of X is not 0, the next instruction is the one N instructions away from this one (N may be negative; ignored lines are not counted). Otherwise execution continues with the next instruction.
Instruction names and register names are lowercase; anything else is invalid.

Running:
- Before running anything, check the whole program. If any line that is not ignored is not a valid instruction (unknown name, wrong number of operands, or an invalid register, literal or operand), throw an Error whose message is 'bad instruction at line K', where K is the 1-based line number of the first such line in the original text, counting ignored lines too. In that case nothing is run.
- Execution starts at the first instruction and stops normally when the next instruction position is before the first instruction or after the last one.
- A program may execute at most 10,000 instructions. If it has not stopped after executing 10,000 instructions, throw an Error with the message 'step limit'.

STARTER CODE (javascript):
function runTik(program) {
  // Write your code here
}

module.exports = { runTik };


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```javascript
function runTik(program) {
  const lines = program.split('\n').map(line => line.trim());
  const output = [];
  let pc = 0;
  let step = 0;
  const regs = { a: 0, b: 0, c: 0, d: 0 };

  function normalize(val) {
    return val % 1000;
  }

  function validateLine(line, lineNum) {
    if (!line) return false;
    if (line.startsWith('#')) return false;
    const parts = line.split(/\s+/);
    if (parts.length < 1) return false;
    const op = parts[0];
    switch (op) {
      case 'set':
      case 'add':
      case 'mul':
      case 'out':
      case 'jmp':
        break;
      default:
        throw new Error(`bad instruction at line ${lineNum}`);
    }

    if (op === 'set' || op === 'add' || op === 'mul') {
      if (parts.length !== 3) throw new Error(`bad instruction at line ${lineNum}`);
      if (!['a', 'b', 'c', 'd'].includes(parts[1])) throw new Error(`bad instruction at line ${lineNum}`);
    } else if (op === 'out') {
      if (parts.length !== 2) throw new Error(`bad instruction at line ${lineNum}`);
    } else if (op === 'jmp') {
      if (parts.length !== 3) throw new Error(`bad instruction at line ${lineNum}`);
      if (!['a', 'b', 'c', 'd'].includes(parts[1])) throw new Error(`bad instruction at line ${lineNum}`);
    }
  }

  for (let lineNum = 1; lineNum <= lines.length; lineNum++) {
    const line = lines[lineNum - 1];
    if (!line) continue;
    if (line.startsWith('#')) continue;
    validateLine(line, lineNum);
  }

  while (step < 10000 && pc >= 0 && pc < lines.length) {
    const line = lines[pc];
    if (!line) {
      pc++;
      step++;
      continue;
    }
    const parts = line.split(/\s+/);
    const op = parts[0];
    step++;

    if (op === 'set') {
      const reg = parts[1];
      const val = parts[2];
      if (val.startsWith('-') || val.startsWith('+')) {
        if (!/^-?\d+$/.test(val)) throw new Error(`bad instruction at line ${pc + 1}`);
        regs[reg] = normalize(parseInt(val, 10));
      } else if (/^\d+$/.test(val)) {
        regs[reg] = normalize(parseInt(val, 10));
      } else {
        throw new Error(`bad instruction at line ${pc + 1}`);
      }
    } else if (op === 'add') {
      const reg = parts[1];
      const val = parts[2];
      if (val.startsWith('-') || val.startsWith('+')) {
        if (!/^-?\d+$/.test(val)) throw new Error(`bad instruction at line ${pc + 1}`);
        regs[None] = normalize(parseInt(val, 10));
      } else if (/^\d+$/.test(val)) {
        regs[reg] = normalize(regs[reg] + parseInt(val, 10));
      } else {
        throw new Error(`bad instruction at line ${pc + 1}`);
      }
    } else if (op === 'mul') {
      const reg = parts[1];
      const val = parts[2];
      if (val.startsWith('-') || val.startsWith('+')) {
        if (!/^-?\d+$/.test(val)) throw new Error(`bad instruction at line ${pc + 1}`);
        regs[reg] = normalize(regs[reg] * parseInt(val, 10));
      } else if (/^\d+$/.test(val)) {
        regs[reg] = normalize(regs[reg] * parseInt(val, 10));
      } else {
        throw new Error(`bad instruction at line ${pc + 1}`);
      }
    } else if (op === 'out') {
      const val = parts[1];
      if (val.startsWith('-') || val.startsWith('+')) {
        if (!/^-?\d+$/.test(val)) throw new Error(`bad instruction at line ${pc + 1}`);
        output.push(normalize(parseInt(val, 10)));
      } else if (/^\d+$/.test(val)) {
        output.push(normalize(parseInt(val, 10)));
      } else {
        throw new Error(`bad instruction at line ${pc + 1}`);
      }
    } else if (op === 'jmp') {
      const condReg = parts[1];
      const condVal = parts[2];
      if (val.startsWith('-') || val.startsWith('+')) {
        if (!/^-?\d+$/.test(val)) throw new Error(`bad instruction at line ${pc + 1}`);
        const cond = normalize(parseInt(condVal, 10));
        if (cond !== 0) {
          const offset = parseInt(condVal, 10);
          pc += offset;
        } else {
          pc++;
        }
      } else if (/^\d+$/.test(val)) {
        const cond = normalize(parseInt(condVal, 10));
        if (cond !== 0) {
          const offset = parseInt(condVal, 10);
          pc += offset;
        } else {
          pc++;
        }
      } else {
        throw new Error(`bad instruction at line ${pc + 1}`);
      }
    }

    pc++;
  }

  if (step >= 10000) throw new Error('step limit');

  return output;
}
```