len row[] 90
len col[] 90
len box[] 90
len grid[] 82
# 
proc init . .
  for pos = 1 to 81
    if pos mod 9 = 1
      s$ = input
      if s$ = ""
        s$ = input
      .
      len inp[] 0
      for i = 1 to len s$
        if substr s$ i 1 <> " "
          inp[] &= number substr s$ i 1
        .
      .
    .
    dig = number inp[(pos - 1) mod 9 + 1]
    if dig > 0
      grid[pos] = dig
      r = (pos - 1) div 9
      c = (pos - 1) mod 9
      b = r div 3 * 3 + c div 3
      row[r * 10 + dig] = 1
      col[c * 10 + dig] = 1
      box[b * 10 + dig] = 1
    .
  .
.
init
# 
proc display . .
  for i = 1 to 81
    write grid[i] & " "
    if i mod 3 = 0
      write " "
    .
    if i mod 9 = 0
      print ""
    .
    if i mod 27 = 0
      print ""
    .
  .
.
# 
proc solve pos . .
  while grid[pos] <> 0
    pos += 1
  .
  if pos > 81
    # solved
    display
    break 1
  .
  r = (pos - 1) div 9
  c = (pos - 1) mod 9
  b = r div 3 * 3 + c div 3
  r *= 10
  c *= 10
  b *= 10
  for d = 1 to 9
    if row[r + d] = 0 and col[c + d] = 0 and box[b + d] = 0
      grid[pos] = d
      row[r + d] = 1
      col[c + d] = 1
      box[b + d] = 1
      solve pos + 1
      row[r + d] = 0
      col[c + d] = 0
      box[b + d] = 0
    .
  .
  grid[pos] = 0
.
solve 1
# 
input_data
5 3 0  0 2 4  7 0 0
0 0 2  0 0 0  8 0 0
1 0 0  7 0 3  9 0 2

0 0 8  0 7 2  0 4 9
0 2 0  9 8 0  0 7 0
7 9 0  0 0 0  0 8 0

0 0 0  0 3 0  5 0 6
9 6 0  0 1 0  3 0 0
0 5 0  6 9 0  0 1 0

