open Array;; type instruction = | Reset of int | Incr of int | Set of int*int | Jump of int*int*int;; type program = instruction array;; type registers = int array;; let max_registers = 10000;; let registres = Array.make (max_registers+1) 0;; exception Segmentation_fault exception Memory_exhausted let next_by_reset ind r = if r <= max_registers then ind+1 else Segmentation_fault;; let next_by_incr ind r = if r <= max register then ind+1 else : sig end let run_instruction regist instruct indice = match instruct with | Reset(r) -> if r <= max_registers then indice+1 else failwith "Segmentation_fault" | Incr(r) -> if i <= max_registers then indice+1 else failwith "Segmentation_fault" | Set(r1,r2) -> if r1 <= max_registers then if r2 <= max_registers then indice+1 else failwith "Segmentation_fault" else failwith "Memory_exhausted" | Jump(r1,r2,i) -> if r1 <= max_registers then if r2 <= max_registers then if (Array.get regist r1) = (Array.get regist r2) then i else indice+1 else failwith "Memory_exhausted" else failwith "Memory_exhausted";; let exec_instruction regist instruct = match instruct with | Reset(r) -> let _ = Array.set regist r 0 in regist | Incr(i) -> let _ = Array.set regist i ((Array.get regist i)+1) in regist | Set(r1,r2) -> let _ = Array.set regist r2 (Array.get regist r1) in regist | Jump(r1,r2,i) -> regist;; let max_steps = 100000;; let rec step_program regist prog steps = if steps = 0 then regist else if not ((Array.length prog) = (Array.get regist 0)) then let regist2 = exec_instruction regist (Array.get prog (Array.get regist 0)) in let nxt = run_instruction regist (Array.get prog (Array.get regist 0)) (Array.get regist 0) in let _ = Array.set regist2 0 nxt in step_program regist2 prog (steps-1) else exec_instruction regist (Array.get prog (Array.get regist 0));; let p = Array.of_list [Jump(1,3,5);Incr(2);Incr(3);Jump(1,3,5);Jump(1,1,1);Set(2,0)];; let r = Array.of_list [0;5;7;0;0;0;0;0];; let ans = print_int (Array.get (step_program r p max_steps) 0);; let ans = print_int (Array.get (step_program r p max_steps) 1);; let ans = print_int (Array.get (step_program r p max_steps) 2);; let ans = print_int (Array.get (step_program r p max_steps) 3);; let ans = print_int (Array.get (step_program r p max_steps) 4);;