Task

Solve a partially filled-in normal   9x9   Sudoku grid   and display the result in a human-readable format.

references

 Algorithmics of Sudoku   may help implement this.
 Python Sudoku Solver Computerphile video.


T Sudoku
   solved = 0B
   grid = [0] * 81
 
   F (rows)
      assert(rows.len == 9 & all(rows.map(row -> row.len == 9)), ‘Grid must be 9 x 9’)
      L(i) 9
         L(j) 9
            .grid[9 * i + j] = Int(rows[i][j])
 
   F solve()
      print("Starting grid:\n\n"(.))
      .placeNumber(0)
      print(I .solved {"Solution:\n\n"(.)} E ‘Unsolvable!’)
 
   F placeNumber(pos)
      I .solved
         R
      I pos == 81
         .solved = 1B
         R
 
      I .grid[pos] > 0
         .placeNumber(pos + 1)
         R
 
      L(n) 1..9
         I .checkValidity(n, pos % 9, pos I/ 9)
            .grid[pos] = n
            .placeNumber(pos + 1)
            I .solved
               R
            .grid[pos] = 0
 
   F checkValidity(v, x, y)
      L(i) 9
         I .grid[y * 9 + i] == v |
           .grid[i * 9 + x] == v
            R 0B
 
      V startX = (x I/ 3) * 3
      V startY = (y I/ 3) * 3
      L(i) startY .< startY + 3
         L(j) startX .< startX + 3
            I .grid[i * 9 + j] == v
               R 0B
 
      R 1B
 
   F String()
      V s = ‘’
      L(i) 9
         L(j) 9
            s ‘’= .grid[i * 9 + j]‘ ’
            I j C (2, 5)
               s ‘’= ‘| ’
         s ‘’= "\n"
         I i C (2, 5)
            s ‘’= "------+-------+------\n"
      R s
 
V rows = [‘850002400’,
          ‘720000009’,
          ‘004000000’,
          ‘000107002’,
          ‘305000900’,
          ‘040000000’,
          ‘000080070’,
          ‘017000000’,
          ‘000036040’]
 
Sudoku(rows).solve()