Problem p02592 (python)

Fixed submission:
def output(S):
  s = S.split()
  i, j, k = map(int, s[1:])
  if s[0] == '<':
    state[k] = 1 if state[i] < state[j] else 0
  else:
    state[k] = state[i] + state[j]
  #print(state)
  #print(S)

outputs = []
output = outputs.append
def print_outputs():
  print(len(outputs))
  for x in outputs:
    print(x)

def solve(x, y):
  LIM = 32
  state = [0] * 5 * 10 * LIM
  state[0] = x
  state[1] = y

  usable = 3  # reset to 0 after use
  usable1 = 4  # reset to 0 after use
  usable2 = 5  # reset to 0 after use
  usable3 = 6  # reset to 0 after use
  zero = 7
  one = 8
  base = one  # base, ... base + LIM-1, (2**X)

  output(f'< {zero} 0 {base}')
  output(f'< {zero} 1 {usable}')
  output(f'+ {base} {usable} {base}')  # 2 is 0, 3 is 1 or 2
  output(f'< {zero} {base} {base}')  # 2 is 0, 3 is 1
  output(f'+ {zero} {zero} {usable}')
  
  for i in range(LIM-1):
    output(f'+ {base+i} {base+i} {base+i+1}') 

  def print_or(i, j, k):
    output(f'+ {i} {j} {k}')
    output(f'< {zero} {k} {k}')

  def print_not(i, k):
    output(f'< {i} {one} {k}')

  def print_zero(i):
    output(f'+ {zero} {zero} {i}')

  def print_and(i, j, k):
    print_not(i, k)
    print_not(j, usable)
    print_or(k, usable, k)
    print_not(k, k)
  
  def print_mult_pow2(n, i, k):
    # set i*2**n to k
    output(f'+ {zero} {i} {k}')
    for _ in range(n):
      output(f'+ {k} {k} {k}')

  def print_binarize(i, k): # binarize i and output in k, k+1, .. k+LIM-1
    output(f'+ {one} {i} {usable1}')  # usable1 = I + 1   # usable2 added so far

    for p in range(LIM-1, -1, -1):
      # 2 ** i
      #print(i, p, k)
      output(f'+ {base+p} {usable2} {usable3}')
      output(f'< {usable3} {usable1} {k+p}')
      print_mult_pow2(p, k+p, usable3)
      output(f'+ {usable3} {usable2} {usable2}')
    print_zero(usable1)
    print_zero(usable2)
    print_zero(usable3)

  A = base + LIM
  B = base + 2 * LIM
  print_binarize(0, A)  
  print_binarize(1, B)  

  for i in range(LIM):
    for j in range(LIM):
      print_and(A+i, B+j, usable1)
      print_mult_pow2(i+j, usable1, usable2)
      output(f'+ 2 {usable2} 2')


  return state[2]

solve(8, 7)
print_outputs()
# for x in range(11):
#   for y in range(11):
#     ans = solve(x, y)
#     if ans != x* y:
#       print(x, y, ans)

Buggy submission (verdict: Runtime Error):
# def output(S):
#   s = S.split()
#   i, j, k = map(int, s[1:])
#   if s[0] == '<':
#     state[k] = 1 if state[i] < state[j] else 0
#   else:
#     state[k] = state[i] + state[j]
#   #print(state)
#   #print(S)

outputs = []
output = outputs.append
def print_outputs():
  print(len(outputs))
  for x in outputs:
    print(x)

def solve(x, y):
  LIM = 32
  # state = [0] * 5 * 10 * LIM
  # state[0] = x
  # state[1] = y

  usable = 3  # reset to 0 after use
  usable1 = 4  # reset to 0 after use
  usable2 = 5  # reset to 0 after use
  usable3 = 6  # reset to 0 after use
  zero = 7
  one = 8
  base = one  # base, ... base + LIM-1, (2**X)

  output(f'< {zero} 0 {base}')
  output(f'< {zero} 1 {usable}')
  output(f'+ {base} {usable} {base}')  # 2 is 0, 3 is 1 or 2
  output(f'< {zero} {base} {base}')  # 2 is 0, 3 is 1
  output(f'+ {zero} {zero} {usable}')
  
  for i in range(LIM-1):
    output(f'+ {base+i} {base+i} {base+i+1}') 

  def print_or(i, j, k):
    output(f'+ {i} {j} {k}')
    output(f'< {zero} {k} {k}')

  def print_not(i, k):
    output(f'< {i} {one} {k}')

  def print_zero(i):
    output(f'+ {zero} {zero} {i}')

  def print_and(i, j, k):
    print_not(i, k)
    print_not(j, usable)
    print_or(k, usable, k)
    print_not(k, k)
  
  def print_mult_pow2(n, i, k):
    # set i*2**n to k
    output(f'+ {zero} {i} {k}')
    for _ in range(n):
      output(f'+ {k} {k} {k}')

  def print_binarize(i, k): # binarize i and output in k, k+1, .. k+LIM-1
    output(f'+ {one} {i} {usable1}')  # usable1 = I + 1   # usable2 added so far

    for p in range(LIM-1, -1, -1):
      # 2 ** i
      #print(i, p, k)
      output(f'+ {base+p} {usable2} {usable3}')
      output(f'< {usable3} {usable1} {k+p}')
      print_mult_pow2(p, k+p, usable3)
      output(f'+ {usable3} {usable2} {usable2}')
    print_zero(usable1)
    print_zero(usable2)
    print_zero(usable3)

  A = base + LIM
  B = base + 2 * LIM
  print_binarize(0, A)  
  print_binarize(1, B)  

  for i in range(LIM):
    for j in range(LIM):
      print_and(A+i, B+j, usable1)
      print_mult_pow2(i+j, usable1, usable2)
      output(f'+ 2 {usable2} 2')


  return state[2]

solve(8, 7)
print_outputs()
# for x in range(11):
#   for y in range(11):
#     ans = solve(x, y)
#     if ans != x* y:
#       print(x, y, ans)