Advertisement
wwww

Untitled

May 4th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3. grid = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
  4.  
  5. def parse_grid(grid):
  6.     _grid = []
  7.     for line in (grid[0+i:9+i] for i in range(0, len(grid), 9)):
  8.         row = []
  9.         _grid.append(row)
  10.         for char in line:
  11.             if char == '0':
  12.                 row.append([1,2,3,4,5,6,7,8,9])
  13.             else:
  14.                 row.append(int(char))
  15.     return _grid
  16.  
  17. def is_in_square(n, grid, square):
  18.     for x in range(square[0], square[0]+3):
  19.         for y in range(square[1], square[1]+3):
  20.             if type(grid[y][x]) == int and grid[y][x] == n:
  21.                 return True
  22.     return False
  23.  
  24. def is_in_row(n, grid, row):
  25.     for x in range(0, 9):
  26.         if type(grid[row][x]) == int and grid[row][x] == n:
  27.             return True
  28.     return False
  29.  
  30. def is_in_column(n, grid, column):
  31.     for y in range(0, 9):
  32.         if type(grid[y][column]) == int and grid[y][column] == n:
  33.             return True
  34.     return False
  35.  
  36. def find_pair(grid):
  37.     return ((x, y) for y, row in enumerate(grid) for x, tile in enumerate(row) if type(tile) != int and len(tile) == 2)
  38.  
  39. def sudoku(grid, _tried={}):
  40.     incomplete = False
  41.     changed = False
  42.     for y, row in enumerate(grid):
  43.         for x, tile in enumerate(row):
  44.             if type(tile) == int:
  45.                 continue
  46.             tile = [n for n in tile if not (is_in_column(n, grid, x) or is_in_row(n, grid, y) or is_in_square(n, grid, (x//3*3, y//3*3)))]
  47.             if len(tile) == 1:
  48.                 grid[y][x] = tile[0]
  49.                 changed = True
  50.             elif len(tile) == 0:
  51.                 grid[y][x] = tile
  52.                 raise Exception
  53.             else:
  54.                 grid[y][x] = tile
  55.                 incomplete = True
  56.  
  57.     if not changed:
  58.         for pair in find_pair(grid):
  59.             if pair in _tried:
  60.                 continue
  61.             else:
  62.                 _tried[pair] = True
  63.             x, y = None, None
  64.             (x, y) = pair
  65.             choices = grid[y][x]
  66.             try:
  67.                 attempt = deepcopy(grid)
  68.                 attempt[y][x] = choices[0]
  69.                 return sudoku(attempt, _tried)
  70.             except Exception:
  71.                 attempt = deepcopy(grid)
  72.                 attempt[y][x] = choices[1]
  73.                 try:
  74.                     return sudoku(attempt, _tried)
  75.                 except Exception:
  76.                     continue
  77.             raise Exception
  78.  
  79.     if incomplete:
  80.         return sudoku(grid, _tried)
  81.     return grid
  82.  
  83. total = 0
  84. with open('files/96.txt', 'r') as f:
  85.     for line in f:
  86.         result = sudoku(parse_grid(line[:-1]))
  87.         total += result[0][0]*100 + result[0][1]*10 + result[0][2]
  88.         print(result[0][0]*100 + result[0][1]*10 + result[0][2])
  89. print(total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement