|
| 1 | +import string |
| 2 | +from pprint import pprint |
| 3 | + |
| 4 | + |
| 5 | +EQUATIONSSTR = '''B9 + B8 + C1 + H4 + H4 = 23 |
| 6 | +A5 + D7 + I5 + G8 + B3 + A5 = 19 |
| 7 | +I2 + I3 + F2 + E9 = 15 |
| 8 | +I7 + H8 + C2 + D9 = 26 |
| 9 | +I6 + A5 + I3 + B8 + C3 = 20 |
| 10 | +I7 + D9 + B6 + A8 + A3 + C4 = 27 |
| 11 | +C7 + H9 + I7 + B2 + H8 + G3 = 31 |
| 12 | +D3 + I8 + A4 + I6 = 27 |
| 13 | +F5 + B8 + F8 + I7 + F1 = 33 |
| 14 | +A2 + A8 + D7 + E4 = 21 |
| 15 | +C1 + I4 + C2 + I1 + A4 = 20 |
| 16 | +F8 + C1 + F6 + D3 + B6 = 25''' |
| 17 | + |
| 18 | + |
| 19 | +def gridstring(gridstr): |
| 20 | + gridstr = gridstr.replace('.', '0') |
| 21 | + |
| 22 | + position = 0 |
| 23 | + grid = [] |
| 24 | + for row in range(9): |
| 25 | + grid.append([]) |
| 26 | + # grid[row] = [] |
| 27 | + for column in range(9): |
| 28 | + grid[row].append(int(gridstr[position])) |
| 29 | + position += 1 |
| 30 | + return grid |
| 31 | + |
| 32 | + |
| 33 | +def test_gridstring(): |
| 34 | + pprint(gridstring("123456789123456789123456789123456789123456789123456789123456789123456789123456789")) |
| 35 | + pprint(gridstring("123456789789123456456789123312845967697312845845697312231574698968231574574968231")) |
| 36 | + |
| 37 | + |
| 38 | +def make_equations(equationsstr): |
| 39 | + equationsstr = equationsstr.splitlines() |
| 40 | + for pos, e in enumerate(equationsstr): |
| 41 | + equationsstr[pos] = [x.strip() for x in e.split('=')] |
| 42 | + equationsstr[pos][0] = [x.strip() for x in equationsstr[pos][0].split('+')] |
| 43 | + equationsstr[pos][1] = int(equationsstr[pos][1]) |
| 44 | + items = [] |
| 45 | + for item in equationsstr[pos][0]: |
| 46 | + # print(item[0], string.ascii_uppercase.find(item[0]), item[1]) |
| 47 | + items.append([string.ascii_uppercase.find(item[0]), int(item[1])-1]) |
| 48 | + equationsstr[pos][0] = items |
| 49 | + |
| 50 | + # print(equationsstr[pos][0]) |
| 51 | + # print(equationsstr) |
| 52 | + return equationsstr |
| 53 | + |
| 54 | + |
| 55 | +def test_make_equations(): |
| 56 | + pprint(make_equations(EQUATIONSSTR)) |
| 57 | + |
| 58 | +def checksections(grid): |
| 59 | + # test all the 3x3 sections |
| 60 | + print('Check 3x3') |
| 61 | + for column in range(0, 9, 3): |
| 62 | + for row in range(0, 9, 3): |
| 63 | + # print(column, row) |
| 64 | + section = grid[row][column:column+3] + grid[row+1][column:column+3] + grid[row+2][column:column+3] |
| 65 | + for i in range(1, 9): |
| 66 | + if i not in section: |
| 67 | + print(False, i, column, row, section) |
| 68 | + return False |
| 69 | + return True |
| 70 | + |
| 71 | + |
| 72 | +def checkrows(grid): |
| 73 | + print('Check rows') |
| 74 | + for row in range(9): |
| 75 | + for i in range(1, 9): |
| 76 | + if i not in grid[row]: |
| 77 | + print(False, i, row, grid[row]) |
| 78 | + return False |
| 79 | + return True |
| 80 | + |
| 81 | + |
| 82 | +def checkcolumns(grid): |
| 83 | + print('Check columns') |
| 84 | + |
| 85 | + for column in range(9): |
| 86 | + columndata = [] |
| 87 | + for row in range(9): |
| 88 | + columndata.append(grid[row][column]) |
| 89 | + for i in range(1, 9): |
| 90 | + if i not in columndata: |
| 91 | + print(False, i, row, column, columndata) |
| 92 | + return False |
| 93 | + return True |
| 94 | + |
| 95 | + |
| 96 | +def checkequations(grid, equations=make_equations(EQUATIONSSTR)): |
| 97 | + print("Check equations") |
| 98 | + |
| 99 | + for e in equations: |
| 100 | + # print(e) |
| 101 | + # print(e[0]) |
| 102 | + sum = 0 |
| 103 | + for coord in e[0]: |
| 104 | + # print(coord, grid[coord[0]][coord[1]]) |
| 105 | + sum += grid[coord[0]][coord[1]] |
| 106 | + if sum != e[1]: |
| 107 | + return False |
| 108 | + return True |
| 109 | + |
| 110 | + |
| 111 | +def checkgrid(grid): |
| 112 | + result = checksections(grid) and checkrows(grid) and checkcolumns(grid) and checkequations(grid) |
| 113 | + return result |
| 114 | + |
| 115 | + |
| 116 | +def solve(grid): |
| 117 | + # add a bool to see whether we increment that position |
| 118 | + givengrid = [] |
| 119 | + for row in range(9): |
| 120 | + givengrid.append([]) |
| 121 | + for column in range(9): |
| 122 | + if grid[row][column] == 0: |
| 123 | + givengrid[row].append(False) # False means we don't know this location |
| 124 | + grid[row][column] = 1 # change all 0 to 1 |
| 125 | + |
| 126 | + else: |
| 127 | + givengrid[row].append(True) # True means this location was given |
| 128 | + |
| 129 | + |
| 130 | + ## TODO ## resume working solver here |
| 131 | + # increment all the 0 positions, one by one and check solution |
| 132 | + checkgrid(grid) |
| 133 | + for row in range(9): |
| 134 | + for column in range(9): |
| 135 | + |
| 136 | + if givengrid[row][column]: |
| 137 | + # This position must not change |
| 138 | + continue |
| 139 | + |
| 140 | + next = grid[row][column] + 1 |
| 141 | + for i in range(grid[row][column], 10): |
| 142 | + |
| 143 | + |
| 144 | + grid[row][column] += 1 |
| 145 | + if checkgrid(grid): |
| 146 | + return(grid) |
| 147 | + return None |
| 148 | + |
| 149 | + |
| 150 | +# grid = gridstring("123456789789123456456789123312845967697312845845697312231574698968231574574968231") |
| 151 | +# print(checkgrid(grid)) |
| 152 | + |
| 153 | +grid = gridstring("........1.12............2..........2.2......................12......1......1.....") |
| 154 | +pprint(grid) |
| 155 | +print(solve(grid)) |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +# sudoku grid formats |
| 160 | +''' |
| 161 | +
|
| 162 | +......... |
| 163 | +......... |
| 164 | +......... |
| 165 | +......... |
| 166 | +......... |
| 167 | +......... |
| 168 | +......... |
| 169 | +......... |
| 170 | +......... |
| 171 | +
|
| 172 | +........1 |
| 173 | +.12...... |
| 174 | +......2.. |
| 175 | +........2 |
| 176 | +.2....... |
| 177 | +......... |
| 178 | +......12. |
| 179 | +.....1... |
| 180 | +...1..... |
| 181 | +
|
| 182 | +
|
| 183 | +''' |
| 184 | + |
0 commit comments