Commits

Josh VanderLinden committed dd8c173

Added HyperSudoku.

Comments (0)

Files changed (2)

 variations of Sudoku.  This class will occasionally generate solvable grids 
 within a second or two, but it takes much longer more often than not.
 
+Another variation of Sudoku, called Hyper Sudoku, can be found in hyper.py. 
+This one involves four additional regions inside the grid.  See the entry 
+for Sudoku on Wikipedia for an image of Hyper Sudoku.
+

sudokulib/hyper.py

+from decorators import requires_solution
+from sudoku import Sudoku
+
+class HyperSudoku(Sudoku):
+    VALID_SIZES = (3,)
+    EXTRA_REGIONS = (
+        (10,11,12,19,20,21,28,29,30),
+        (14,15,16,23,24,25,32,33,34),
+        (46,47,48,55,56,57,64,65,66),
+        (50,51,52,59,60,61,68,69,70),
+    )
+
+    def get_region(self, row, col):
+        """Returns values used in the region at the specified row, col"""
+
+        index = self.row_col_to_index(row, col)
+        used = super(HyperSudoku, self).get_region(row, col)
+        return used + self.get_region_by_index(index)
+
+    def get_region_by_index(self, index):
+        """Returns values used in the region at the specified index"""
+
+        for region in HyperSudoku.EXTRA_REGIONS:
+            if index in region:
+                return [self.solution[i] for i in region]
+
+        return []
+
+def main():
+    s = HyperSudoku()
+    s.print_masked()
+    print '=' * 50
+    s.print_solution()
+
+if __name__ == '__main__':
+    main()
+