From c120ddc4fa472b2430a431695e3ef58e12cb223b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 28 Aug 2013 23:09:55 -0700 Subject: [PATCH 01/22] Add sudoku module -- work thus far --- sudoku.py | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 sudoku.py diff --git a/sudoku.py b/sudoku.py new file mode 100644 index 0000000..5540925 --- /dev/null +++ b/sudoku.py @@ -0,0 +1,166 @@ +#!/usr/bin/env python3 +# Eryn Wells + +''' +A Sudoku solver. +''' + +import logging +import math + +LOG = logging.getLogger(__name__) + + +class Board(dict): + def __init__(self, size=9, **kwargs): + self.size = size + + # Verify size is a perfect square. The sqrt(size) is also the dimension of each box on the + # Sudoku grid. + self.box_size = int(math.sqrt(size)) + assert self.box_size == int(self.box_size), 'Invalid size; value must be a perfect square' + + # The range of possible values for a square. + possible_values = range(1, self.size + 1) + + def kwget(x, y): + ''' + Try to get an initial value for square ({x}, {y}) from the given kwargs. If none exists, return a list of + all possible values for the square. If an initial value was given, make sure it is one of the valid initial + values. Raise a ValueError if not. + ''' + initial_value = kwargs.get('x{}y{}'.format(x, y)) + if initial_value is None: + return list(possible_values) + if initial_value not in possible_values: + raise ValueError('Invalid initial value for square ({}, {}): {}'.format(x, y, initial_value)) + return [initial_value] + + # Make the grid. + super(Board, self).__init__([(self._xy_key(x, y), kwget(x, y)) + for x in range(self.size) + for y in range(self.size)]) + + def _xy_key(self, x, y): + ''' + Given {x} and {y}, generate a key to refer to the square at coordinate (x, y) in the grid. + ''' + return (int(x), int(y)) + + def row(self, idx): + ''' + Return a dict of all squares in the {idx}'th row. + ''' + assert idx >= 0 and idx < self.size, 'Invalid row index; value must be >= 0 and < {}'.format(self.size) + # key[1] is Y coordinate in the grid. + return {k: v for k, v in self.items() if k[1] == idx} + + def col(self, idx): + ''' + Return a dict of all squares in the {idx}'th column. + ''' + assert idx >= 0 and idx < self.size, 'Invalid column index; value must be >= 0 and < {}'.format(self.size) + # key[0] is X coordinate in the grid. + return {k: v for k, v in self.items() if k[0] == idx} + + def box(self, x, y): + ''' + Given a square at coordinates ({x}, {y}), return a dictionary containing the squares that make up the box that + contains the point. + ''' + bx = int(x / self.box_size) * self.box_size + by = int(y / self.box_size) * self.box_size + bx_range = range(bx, bx + self.box_size) + by_range = range(by, by + self.box_size) + return {k: v for k, v in self.items() if k[0] in bx_range and k[1] in by_range} + + def peers(self, x, y): + # Generate a dictionary of all the squares in the row, column, and box containing the given square. + peers = dict(list(self.row(y).items()) + list(self.col(x).items()) + list(self.box(x, y).items())) + # Remove the given square. + del peers[self._xy_key(x, y)] + return peers + + def clear(self): + ''' + Clear the board. + ''' + for square in self: + self[square] = list(range(1, self.size + 1)) + + def eliminate(self, square, value): + ''' + Eliminate {value} from square at ({x}, {y}). + ''' + if value not in self[square]: + LOG.debug('Value {} not in square {}; skipping'.format(value, square)) + return + + # Update peer value list. + super(Board, self).__setitem__(square, [v for v in self[square] if v != value]) + LOG.debug('Eliminating {} from square {}'.format(value, square)) + + # (1) If a square is reduced to one value, eliminate that value from its peers. + if len(self[square]) == 0: + # Whoops. Removed the last value... We have a contradiction now. + LOG.error('Removed last value from square {}'.format(square)) + raise ValueError('Removed last value from square {}; board is now invalid'.format(square)) + elif len(self[square]) == 1: + # One value left in this square. Propagate changes to its peers. + LOG.debug('One value left in square {}; eliminating {} from its peers'.format(square, self[square][0])) + try: + for peer in self.peers(*square): + self.eliminate(peer, self[square][0]) + except ValueError: + raise + + # (2) If a unit has only one square for a value, put it there. + for unit in (self.row(square[1]), self.col(square[0]), self.box(*square)): + places = [sq for sq in unit if value in unit[sq]] + if len(places) == 0: + LOG.error('No place for value {} to go in unit {}'.format(value, unit)) + raise ValueError('No place for value {} to go in unit {}; board is now invalid'.format(value, unit)) + elif len(places) == 1: + LOG.debug('One place for value {} to be in unit {}; setting'.format(value, unit)) + self[places[0]] = [value] + return True + + def __delitem__(self, key): + # Don't allow deleting keys from self. + pass + + def __setitem__(self, key, value): + #if key not in self: + # Don't allow adding new keys, only changes to existing ones. + #return + LOG.debug('Setting value {} at {}.'.format(value, key)) + removed_values = set(self[key]) - set(value) + for v in removed_values: + self.eliminate(key, v) + + def __str__(self): + lines = [] + box_lines = [] + for x in range(self.size): + row_squares = [] + box_squares = [] + for y in range(self.size): + square = self.get(self._xy_key(x, y)) + if len(square) == 1: + box_squares.append(str(square[0])) + else: + box_squares.append('.') + if len(box_squares) == self.box_size: + row_squares.append(' '.join(box_squares)) + box_squares = [] + # Print a divider between boxes. + box_lines.append(' | '.join(row_squares)) + if len(box_lines) == self.box_size: + lines.append('\n'.join(box_lines)) + box_lines = [] + if x < self.size - 1: + box_dividers = ['-' * (2 * self.box_size - 1) for box in range(self.box_size)] + lines.append('\n{}\n'.format('-+-'.join(box_dividers))) + return ''.join(lines) + + From af92bb89f82591359be1ec6d97fac10aa465aed4 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 28 Aug 2013 23:10:15 -0700 Subject: [PATCH 02/22] Ignore *.pyc --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc From 31415f81754b466055161927372562aeeacb3f4b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 28 Aug 2013 23:10:27 -0700 Subject: [PATCH 03/22] Ignore __pycache__/ --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0d20b64..d646835 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +__pycache__/ From 7fd051802c0719dfb56c240e3bd59404eda6035e Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 09:42:06 -0700 Subject: [PATCH 04/22] Implement the solved property --- sudoku.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/sudoku.py b/sudoku.py index 5540925..e810099 100644 --- a/sudoku.py +++ b/sudoku.py @@ -21,7 +21,7 @@ class Board(dict): assert self.box_size == int(self.box_size), 'Invalid size; value must be a perfect square' # The range of possible values for a square. - possible_values = range(1, self.size + 1) + self.possible_values = range(1, self.size + 1) def kwget(x, y): ''' @@ -31,8 +31,8 @@ class Board(dict): ''' initial_value = kwargs.get('x{}y{}'.format(x, y)) if initial_value is None: - return list(possible_values) - if initial_value not in possible_values: + return list(self.possible_values) + if initial_value not in self.possible_values: raise ValueError('Invalid initial value for square ({}, {}): {}'.format(x, y, initial_value)) return [initial_value] @@ -42,11 +42,41 @@ class Board(dict): for y in range(self.size)]) def _xy_key(self, x, y): - ''' - Given {x} and {y}, generate a key to refer to the square at coordinate (x, y) in the grid. - ''' + '''Given {x} and {y}, generate a key to refer to the square at coordinate (x, y) in the grid.''' return (int(x), int(y)) + @property + def solved(self): + ''' + Determines if the board has been solved. First, determine if all squares have no more than one value (i.e. they + have had values assigned to them). If not, return False. If so, check each unit (rows, columns, and boxes) to + make sure that each has one and only one of each value in the range of possible values for a unit. If not, + return False; otherwise, return True. + ''' + if not all(len(s) == 1 for s in self.values()): + return False + + def validate_unit(unit): + ''' + Validate {unit} by ensuring it has exactly 1 of each value in the range of possible values. + ''' + necessary_values = list(self.possible_values) + for square, values in unit.items(): + v = values[0] + if v in necessary_values: + necessary_values.remove(v) + else: + return False + if len(necessary_values) != 0: + return False + return True + + return ( all(validate_unit(self.row(r)) for r in range(self.size)) + and all(validate_unit(self.col(c)) for c in range(self.size)) + and all(validate_unit(self.box(x, y)) + for x in range(0, self.size, self.box_size) + for y in range(0, self.size, self.box_size))) + def row(self, idx): ''' Return a dict of all squares in the {idx}'th row. From 6a753a8dacc27ed0fcb566531242b1f69cf64fa3 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 09:52:52 -0700 Subject: [PATCH 05/22] Docstring updates --- sudoku.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/sudoku.py b/sudoku.py index e810099..0eb3b34 100644 --- a/sudoku.py +++ b/sudoku.py @@ -105,6 +105,10 @@ class Board(dict): return {k: v for k, v in self.items() if k[0] in bx_range and k[1] in by_range} def peers(self, x, y): + ''' + Generate and return a list of peers for the square at coordinates ({x}, {y}). Peers are defined as all the + squares in the same row, column, and box as the given square. + ''' # Generate a dictionary of all the squares in the row, column, and box containing the given square. peers = dict(list(self.row(y).items()) + list(self.col(x).items()) + list(self.box(x, y).items())) # Remove the given square. @@ -113,14 +117,17 @@ class Board(dict): def clear(self): ''' - Clear the board. + Clear the board. Resets each square's possible value list to the full range of possible values for this board. ''' for square in self: self[square] = list(range(1, self.size + 1)) def eliminate(self, square, value): ''' - Eliminate {value} from square at ({x}, {y}). + Eliminate {value} from {square}, propagating changes to the squares peers as necessary. This process involves + two logical rules of Sudoku. First, if a square is reduced to a single possible value, assign that value to the + square and remove the value from the possible value lists of its peers. Second, if a unit has only one square + that can hold a value, put the value in that square and propagate that change to its peers. ''' if value not in self[square]: LOG.debug('Value {} not in square {}; skipping'.format(value, square)) @@ -160,9 +167,15 @@ class Board(dict): pass def __setitem__(self, key, value): - #if key not in self: + ''' + Set the square defined by {key}'s value to {value}. {value} is expected to be an iterable (a list). {key} should + be a valid (x, y) coordinate pair for referencing a square on the board. + ''' + if key not in self: # Don't allow adding new keys, only changes to existing ones. - #return + raise KeyError('Key {} is not a valid coordinate pair.'.format(key)) + + # Remove all values other than the one given from the square. LOG.debug('Setting value {} at {}.'.format(value, key)) removed_values = set(self[key]) - set(value) for v in removed_values: From 8540f60381042265dc883e9e5e9f5efdbbb04586 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 11:49:34 -0700 Subject: [PATCH 06/22] Use length of str(self.size) to compute size of each square in the printed grid --- sudoku.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sudoku.py b/sudoku.py index 0eb3b34..6c4ec6c 100644 --- a/sudoku.py +++ b/sudoku.py @@ -184,15 +184,16 @@ class Board(dict): def __str__(self): lines = [] box_lines = [] + square_size = len(str(self.size)) for x in range(self.size): row_squares = [] box_squares = [] for y in range(self.size): square = self.get(self._xy_key(x, y)) if len(square) == 1: - box_squares.append(str(square[0])) + box_squares.append(str(square[0]).center(square_size)) else: - box_squares.append('.') + box_squares.append('.'.center(square_size)) if len(box_squares) == self.box_size: row_squares.append(' '.join(box_squares)) box_squares = [] @@ -202,7 +203,7 @@ class Board(dict): lines.append('\n'.join(box_lines)) box_lines = [] if x < self.size - 1: - box_dividers = ['-' * (2 * self.box_size - 1) for box in range(self.box_size)] + box_dividers = ['-' * (square_size * (2 * self.box_size - 1)) for box in range(self.box_size)] lines.append('\n{}\n'.format('-+-'.join(box_dividers))) return ''.join(lines) From cac812e46f2c0c097199f87a95fc0a7d5e60dd9d Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 11:57:31 -0700 Subject: [PATCH 07/22] Fix divider length computation --- sudoku.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoku.py b/sudoku.py index 6c4ec6c..4376269 100644 --- a/sudoku.py +++ b/sudoku.py @@ -203,7 +203,7 @@ class Board(dict): lines.append('\n'.join(box_lines)) box_lines = [] if x < self.size - 1: - box_dividers = ['-' * (square_size * (2 * self.box_size - 1)) for box in range(self.box_size)] + box_dividers = ['-' * (square_size * self.box_size + square_size)) for box in range(self.box_size)] lines.append('\n{}\n'.format('-+-'.join(box_dividers))) return ''.join(lines) From d20a0bbdf74a2dea0601c5de116b93be9efaaa80 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 12:05:34 -0700 Subject: [PATCH 08/22] Fix the divider printing logic (again) --- sudoku.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sudoku.py b/sudoku.py index 4376269..5dfd47d 100644 --- a/sudoku.py +++ b/sudoku.py @@ -203,7 +203,8 @@ class Board(dict): lines.append('\n'.join(box_lines)) box_lines = [] if x < self.size - 1: - box_dividers = ['-' * (square_size * self.box_size + square_size)) for box in range(self.box_size)] + # Don't print a divider on the last line. + box_dividers = ['-' * ((square_size + 1) * self.box_size - 1) for box in range(self.box_size)] lines.append('\n{}\n'.format('-+-'.join(box_dividers))) return ''.join(lines) From a926bc5a99e0d69a485897ea70b50d5e9e636fca Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 15:05:30 -0700 Subject: [PATCH 09/22] Board.solve() and Board.assign() --- sudoku.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/sudoku.py b/sudoku.py index 5dfd47d..9da9aa5 100644 --- a/sudoku.py +++ b/sudoku.py @@ -122,6 +122,29 @@ class Board(dict): for square in self: self[square] = list(range(1, self.size + 1)) + def solve(self): + for square, values in self.items(): + if len(values) == 1: + try: + self.assign(square, values) + except ValueError: + raise + return self.solved + + def assign(self, square, value): + ''' + Assign {value} to {square}. {value} is expected to be an iterable (a list). {key} should be a valid (x, y) + coordinate pair for referencing a square on the board. + ''' + LOG.debug('Assigning values {} to square {}.'.format(value, square)) + removed_values = set(self[square]) - set(value) + for v in removed_values: + try: + self.eliminate(square, v) + except ValueError: + raise + return True + def eliminate(self, square, value): ''' Eliminate {value} from {square}, propagating changes to the squares peers as necessary. This process involves @@ -131,7 +154,7 @@ class Board(dict): ''' if value not in self[square]: LOG.debug('Value {} not in square {}; skipping'.format(value, square)) - return + return True # Update peer value list. super(Board, self).__setitem__(square, [v for v in self[square] if v != value]) @@ -167,19 +190,10 @@ class Board(dict): pass def __setitem__(self, key, value): - ''' - Set the square defined by {key}'s value to {value}. {value} is expected to be an iterable (a list). {key} should - be a valid (x, y) coordinate pair for referencing a square on the board. - ''' if key not in self: # Don't allow adding new keys, only changes to existing ones. raise KeyError('Key {} is not a valid coordinate pair.'.format(key)) - - # Remove all values other than the one given from the square. - LOG.debug('Setting value {} at {}.'.format(value, key)) - removed_values = set(self[key]) - set(value) - for v in removed_values: - self.eliminate(key, v) + self.assign(key, value) def __str__(self): lines = [] From 3c21549ee93d89b5b0c626249b3fa28a863459ac Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 15:05:38 -0700 Subject: [PATCH 10/22] Bare bones tests module --- tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests.py diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..cd64084 --- /dev/null +++ b/tests.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# Eryn Wells + +''' +Tests for sudoku. +''' + +import nose +import sudoku +import unittest + + +def main(): + nose.main() + + +if __name__ == '__main__': + main() From 7e43451eb5037a1bfdec1b1272c23b71cfa19831 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 15:58:43 -0700 Subject: [PATCH 11/22] Tests yay! --- tests.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests.py b/tests.py index cd64084..d413bb1 100644 --- a/tests.py +++ b/tests.py @@ -10,6 +10,97 @@ import sudoku import unittest +def test_9x9_dimensions(): + ''' + Test dimenions of a 9x9 Sudoku board. + ''' + b = sudoku.Board() + assert len(b.keys()) == 81 + assert b.size == 9 + assert b.box_size == 3 + for square, values in b.items(): + assert len(values) == 9 + + +def test_9x9_units(): + ''' + Test units of a 9x9 Sudoku board. + ''' + b = sudoku.Board() + for r in range(b.size): + row = b.row(r) + assert len(row) == 9 + for sq in row: + assert sq[1] == r + + for c in range(b.size): + col = b.col(c) + assert len(col) == 9 + for sq in col: + assert sq[0] == c + + for x in range(0, b.size, b.box_size): + for y in range(0, b.size, b.box_size): + box = b.box(x, y) + assert len(box) == 9 + # TODO: Finish this test + +def test_9x9_row(): + ''' + A few tests on rows of a 9x9 Sudoku board. + ''' + b = sudoku.Board() + row = b.row(1) + expected_keys = ((x, 1) for x in range(b.size)) + for ekey in expected_keys: + assert ekey in row + # No negative numbers + assert (-1, 1) not in row + # Only squares with the right y-coordinate + assert (0, 0) not in row + # No keys above the size of the board + assert (b.size, 1) not in row + + +def test_9x9_col(): + ''' + A few tests on rows of a 9x9 Sudoku board. + ''' + b = sudoku.Board() + col = b.col(3) + expected_keys = ((3, y) for y in range(b.size)) + for ekey in expected_keys: + assert ekey in col + # No negative numbers + assert (3, -1) not in col + # Only squares with the right x-coordinate + assert (0, 0) not in col + # No keys above the size of the board + assert (3, b.size) not in col + + +def test_9x9_box(): + ''' + A few tests on boxes of a 9x9 Sudoku board. + ''' + b = sudoku.Board() + assert True + + +def test_9x9_peers(): + ''' + Test peers. + ''' + b = sudoku.Board() + peers = b.peers(3, 3) + expected_peers = set( [(x, 3) for x in range(b.size)] + + [(3, y) for y in range(b.size)] + + [(3, 3), (3, 4), (3, 5), (4, 3), (4, 4), (4, 5), (5, 3), (5, 4), (5, 5)]) + expected_peers.remove((3, 3)) + for epeer in expected_peers: + assert epeer in peers, '{} not in peers of (3, 3)'.format(epeer) + + def main(): nose.main() From 5a2df50e02b239e637c83c45eb1c4c2a59d62ddb Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 16:14:35 -0700 Subject: [PATCH 12/22] Add string test --- tests.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests.py b/tests.py index d413bb1..32dabd1 100644 --- a/tests.py +++ b/tests.py @@ -101,6 +101,25 @@ def test_9x9_peers(): assert epeer in peers, '{} not in peers of (3, 3)'.format(epeer) +def test_9x9_str(): + ''' + Test string generation/printing. + ''' + b = sudoku.Board() + expected_str = '\n'.join(['. . . | . . . | . . .', + '. . . | . . . | . . .', + '. . . | . . . | . . .', + '------+-------+------', + '. . . | . . . | . . .', + '. . . | . . . | . . .', + '. . . | . . . | . . .', + '------+-------+------', + '. . . | . . . | . . .', + '. . . | . . . | . . .', + '. . . | . . . | . . .']) + assert str(b) == expected_str + + def main(): nose.main() From 9f4a775472ebb7143c4c263171588d60ba52e0fc Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 16:32:24 -0700 Subject: [PATCH 13/22] Add puzzles Both via http://norvig.com/sudoku.html - euler.txt: http://projecteuler.net/project/sudoku.txt - norvig.txt: http://magictour.free.fr/top95 --- puzzles/euler.txt | 500 +++++++++++++++++++++++++++++++++++++++++++++ puzzles/norvig.txt | 96 +++++++++ 2 files changed, 596 insertions(+) create mode 100644 puzzles/euler.txt create mode 100644 puzzles/norvig.txt diff --git a/puzzles/euler.txt b/puzzles/euler.txt new file mode 100644 index 0000000..be23f6a --- /dev/null +++ b/puzzles/euler.txt @@ -0,0 +1,500 @@ +Grid 01 +003020600 +900305001 +001806400 +008102900 +700000008 +006708200 +002609500 +800203009 +005010300 +Grid 02 +200080300 +060070084 +030500209 +000105408 +000000000 +402706000 +301007040 +720040060 +004010003 +Grid 03 +000000907 +000420180 +000705026 +100904000 +050000040 +000507009 +920108000 +034059000 +507000000 +Grid 04 +030050040 +008010500 +460000012 +070502080 +000603000 +040109030 +250000098 +001020600 +080060020 +Grid 05 +020810740 +700003100 +090002805 +009040087 +400208003 +160030200 +302700060 +005600008 +076051090 +Grid 06 +100920000 +524010000 +000000070 +050008102 +000000000 +402700090 +060000000 +000030945 +000071006 +Grid 07 +043080250 +600000000 +000001094 +900004070 +000608000 +010200003 +820500000 +000000005 +034090710 +Grid 08 +480006902 +002008001 +900370060 +840010200 +003704100 +001060049 +020085007 +700900600 +609200018 +Grid 09 +000900002 +050123400 +030000160 +908000000 +070000090 +000000205 +091000050 +007439020 +400007000 +Grid 10 +001900003 +900700160 +030005007 +050000009 +004302600 +200000070 +600100030 +042007006 +500006800 +Grid 11 +000125400 +008400000 +420800000 +030000095 +060902010 +510000060 +000003049 +000007200 +001298000 +Grid 12 +062340750 +100005600 +570000040 +000094800 +400000006 +005830000 +030000091 +006400007 +059083260 +Grid 13 +300000000 +005009000 +200504000 +020000700 +160000058 +704310600 +000890100 +000067080 +000005437 +Grid 14 +630000000 +000500008 +005674000 +000020000 +003401020 +000000345 +000007004 +080300902 +947100080 +Grid 15 +000020040 +008035000 +000070602 +031046970 +200000000 +000501203 +049000730 +000000010 +800004000 +Grid 16 +361025900 +080960010 +400000057 +008000471 +000603000 +259000800 +740000005 +020018060 +005470329 +Grid 17 +050807020 +600010090 +702540006 +070020301 +504000908 +103080070 +900076205 +060090003 +080103040 +Grid 18 +080005000 +000003457 +000070809 +060400903 +007010500 +408007020 +901020000 +842300000 +000100080 +Grid 19 +003502900 +000040000 +106000305 +900251008 +070408030 +800763001 +308000104 +000020000 +005104800 +Grid 20 +000000000 +009805100 +051907420 +290401065 +000000000 +140508093 +026709580 +005103600 +000000000 +Grid 21 +020030090 +000907000 +900208005 +004806500 +607000208 +003102900 +800605007 +000309000 +030020050 +Grid 22 +005000006 +070009020 +000500107 +804150000 +000803000 +000092805 +907006000 +030400010 +200000600 +Grid 23 +040000050 +001943600 +009000300 +600050002 +103000506 +800020007 +005000200 +002436700 +030000040 +Grid 24 +004000000 +000030002 +390700080 +400009001 +209801307 +600200008 +010008053 +900040000 +000000800 +Grid 25 +360020089 +000361000 +000000000 +803000602 +400603007 +607000108 +000000000 +000418000 +970030014 +Grid 26 +500400060 +009000800 +640020000 +000001008 +208000501 +700500000 +000090084 +003000600 +060003002 +Grid 27 +007256400 +400000005 +010030060 +000508000 +008060200 +000107000 +030070090 +200000004 +006312700 +Grid 28 +000000000 +079050180 +800000007 +007306800 +450708096 +003502700 +700000005 +016030420 +000000000 +Grid 29 +030000080 +009000500 +007509200 +700105008 +020090030 +900402001 +004207100 +002000800 +070000090 +Grid 30 +200170603 +050000100 +000006079 +000040700 +000801000 +009050000 +310400000 +005000060 +906037002 +Grid 31 +000000080 +800701040 +040020030 +374000900 +000030000 +005000321 +010060050 +050802006 +080000000 +Grid 32 +000000085 +000210009 +960080100 +500800016 +000000000 +890006007 +009070052 +300054000 +480000000 +Grid 33 +608070502 +050608070 +002000300 +500090006 +040302050 +800050003 +005000200 +010704090 +409060701 +Grid 34 +050010040 +107000602 +000905000 +208030501 +040070020 +901080406 +000401000 +304000709 +020060010 +Grid 35 +053000790 +009753400 +100000002 +090080010 +000907000 +080030070 +500000003 +007641200 +061000940 +Grid 36 +006080300 +049070250 +000405000 +600317004 +007000800 +100826009 +000702000 +075040190 +003090600 +Grid 37 +005080700 +700204005 +320000084 +060105040 +008000500 +070803010 +450000091 +600508007 +003010600 +Grid 38 +000900800 +128006400 +070800060 +800430007 +500000009 +600079008 +090004010 +003600284 +001007000 +Grid 39 +000080000 +270000054 +095000810 +009806400 +020403060 +006905100 +017000620 +460000038 +000090000 +Grid 40 +000602000 +400050001 +085010620 +038206710 +000000000 +019407350 +026040530 +900020007 +000809000 +Grid 41 +000900002 +050123400 +030000160 +908000000 +070000090 +000000205 +091000050 +007439020 +400007000 +Grid 42 +380000000 +000400785 +009020300 +060090000 +800302009 +000040070 +001070500 +495006000 +000000092 +Grid 43 +000158000 +002060800 +030000040 +027030510 +000000000 +046080790 +050000080 +004070100 +000325000 +Grid 44 +010500200 +900001000 +002008030 +500030007 +008000500 +600080004 +040100700 +000700006 +003004050 +Grid 45 +080000040 +000469000 +400000007 +005904600 +070608030 +008502100 +900000005 +000781000 +060000010 +Grid 46 +904200007 +010000000 +000706500 +000800090 +020904060 +040002000 +001607000 +000000030 +300005702 +Grid 47 +000700800 +006000031 +040002000 +024070000 +010030080 +000060290 +000800070 +860000500 +002006000 +Grid 48 +001007090 +590080001 +030000080 +000005800 +050060020 +004100000 +080000030 +100020079 +020700400 +Grid 49 +000003017 +015009008 +060000000 +100007000 +009000200 +000500004 +000000020 +500600340 +340200000 +Grid 50 +300200000 +000107000 +706030500 +070009080 +900020004 +010800050 +009040301 +000702000 +000008006 \ No newline at end of file diff --git a/puzzles/norvig.txt b/puzzles/norvig.txt new file mode 100644 index 0000000..c8429cc --- /dev/null +++ b/puzzles/norvig.txt @@ -0,0 +1,96 @@ +4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4...... +52...6.........7.13...........4..8..6......5...........418.........3..2...87..... +6.....8.3.4.7.................5.4.7.3..2.....1.6.......2.....5.....8.6......1.... +48.3............71.2.......7.5....6....2..8.............1.76...3.....4......5.... +....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8... +......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3. +6.2.5.........3.4..........43...8....1....2........7..5..27...........81...6..... +.524.........7.1..............8.2...3.....6...9.5.....1.6.3...........897........ +6.2.5.........4.3..........43...8....1....2........7..5..27...........81...6..... +.923.........8.1...........1.7.4...........658.........6.5.2...4.....7.....9..... +6..3.2....5.....1..........7.26............543.........8.15........4.2........7.. +.6.5.1.9.1...9..539....7....4.8...7.......5.8.817.5.3.....5.2............76..8... +..5...987.4..5...1..7......2...48....9.1.....6..2.....3..6..2.......9.7.......5.. +3.6.7...........518.........1.4.5...7.....6.....2......2.....4.....8.3.....5..... +1.....3.8.7.4..............2.3.1...........958.........5.6...7.....8.2...4....... +6..3.2....4.....1..........7.26............543.........8.15........4.2........7.. +....3..9....2....1.5.9..............1.2.8.4.6.8.5...2..75......4.1..6..3.....4.6. +45.....3....8.1....9...........5..9.2..7.....8.........1..4..........7.2...6..8.. +.237....68...6.59.9.....7......4.97.3.7.96..2.........5..47.........2....8....... +..84...3....3.....9....157479...8........7..514.....2...9.6...2.5....4......9..56 +.98.1....2......6.............3.2.5..84.........6.........4.8.93..5...........1.. +..247..58..............1.4.....2...9528.9.4....9...1.........3.3....75..685..2... +4.....8.5.3..........7......2.....6.....5.4......1.......6.3.7.5..2.....1.9...... +.2.3......63.....58.......15....9.3....7........1....8.879..26......6.7...6..7..4 +1.....7.9.4...72..8.........7..1..6.3.......5.6..4..2.........8..53...7.7.2....46 +4.....3.....8.2......7........1...8734.......6........5...6........1.4...82...... +.......71.2.8........4.3...7...6..5....2..3..9........6...7.....8....4......5.... +6..3.2....4.....8..........7.26............543.........8.15........8.2........7.. +.47.8...1............6..7..6....357......5....1..6....28..4.....9.1...4.....2.69. +......8.17..2........5.6......7...5..1....3...8.......5......2..4..8....6...3.... +38.6.......9.......2..3.51......5....3..1..6....4......17.5..8.......9.......7.32 +...5...........5.697.....2...48.2...25.1...3..8..3.........4.7..13.5..9..2...31.. +.2.......3.5.62..9.68...3...5..........64.8.2..47..9....3.....1.....6...17.43.... +.8..4....3......1........2...5...4.69..1..8..2...........3.9....6....5.....2..... +..8.9.1...6.5...2......6....3.1.7.5.........9..4...3...5....2...7...3.8.2..7....4 +4.....5.8.3..........7......2.....6.....5.8......1.......6.3.7.5..2.....1.8...... +1.....3.8.6.4..............2.3.1...........958.........5.6...7.....8.2...4....... +1....6.8..64..........4...7....9.6...7.4..5..5...7.1...5....32.3....8...4........ +249.6...3.3....2..8.......5.....6......2......1..4.82..9.5..7....4.....1.7...3... +...8....9.873...4.6..7.......85..97...........43..75.......3....3...145.4....2..1 +...5.1....9....8...6.......4.1..........7..9........3.8.....1.5...2..4.....36.... +......8.16..2........7.5......6...2..1....3...8.......2......7..3..8....5...4.... +.476...5.8.3.....2.....9......8.5..6...1.....6.24......78...51...6....4..9...4..7 +.....7.95.....1...86..2.....2..73..85......6...3..49..3.5...41724................ +.4.5.....8...9..3..76.2.....146..........9..7.....36....1..4.5..6......3..71..2.. +.834.........7..5...........4.1.8..........27...3.....2.6.5....5.....8........1.. +..9.....3.....9...7.....5.6..65..4.....3......28......3..75.6..6...........12.3.8 +.26.39......6....19.....7.......4..9.5....2....85.....3..2..9..4....762.........4 +2.3.8....8..7...........1...6.5.7...4......3....1............82.5....6...1....... +6..3.2....1.....5..........7.26............843.........8.15........8.2........7.. +1.....9...64..1.7..7..4.......3.....3.89..5....7....2.....6.7.9.....4.1....129.3. +.........9......84.623...5....6...453...1...6...9...7....1.....4.5..2....3.8....9 +.2....5938..5..46.94..6...8..2.3.....6..8.73.7..2.........4.38..7....6..........5 +9.4..5...25.6..1..31......8.7...9...4..26......147....7.......2...3..8.6.4.....9. +...52.....9...3..4......7...1.....4..8..453..6...1...87.2........8....32.4..8..1. +53..2.9...24.3..5...9..........1.827...7.........981.............64....91.2.5.43. +1....786...7..8.1.8..2....9........24...1......9..5...6.8..........5.9.......93.4 +....5...11......7..6.....8......4.....9.1.3.....596.2..8..62..7..7......3.5.7.2.. +.47.2....8....1....3....9.2.....5...6..81..5.....4.....7....3.4...9...1.4..27.8.. +......94.....9...53....5.7..8.4..1..463...........7.8.8..7.....7......28.5.26.... +.2......6....41.....78....1......7....37.....6..412....1..74..5..8.5..7......39.. +1.....3.8.6.4..............2.3.1...........758.........7.5...6.....8.2...4....... +2....1.9..1..3.7..9..8...2.......85..6.4.........7...3.2.3...6....5.....1.9...2.5 +..7..8.....6.2.3...3......9.1..5..6.....1.....7.9....2........4.83..4...26....51. +...36....85.......9.4..8........68.........17..9..45...1.5...6.4....9..2.....3... +34.6.......7.......2..8.57......5....7..1..2....4......36.2..1.......9.......7.82 +......4.18..2........6.7......8...6..4....3...1.......6......2..5..1....7...3.... +.4..5..67...1...4....2.....1..8..3........2...6...........4..5.3.....8..2........ +.......4...2..4..1.7..5..9...3..7....4..6....6..1..8...2....1..85.9...6.....8...3 +8..7....4.5....6............3.97...8....43..5....2.9....6......2...6...7.71..83.2 +.8...4.5....7..3............1..85...6.....2......4....3.26............417........ +....7..8...6...5...2...3.61.1...7..2..8..534.2..9.......2......58...6.3.4...1.... +......8.16..2........7.5......6...2..1....3...8.......2......7..4..8....5...3.... +.2..........6....3.74.8.........3..2.8..4..1.6..5.........1.78.5....9..........4. +.52..68.......7.2.......6....48..9..2..41......1.....8..61..38.....9...63..6..1.9 +....1.78.5....9..........4..2..........6....3.74.8.........3..2.8..4..1.6..5..... +1.......3.6.3..7...7...5..121.7...9...7........8.1..2....8.64....9.2..6....4..... +4...7.1....19.46.5.....1......7....2..2.3....847..6....14...8.6.2....3..6...9.... +......8.17..2........5.6......7...5..1....3...8.......5......2..3..8....6...4.... +963......1....8......2.5....4.8......1....7......3..257......3...9.2.4.7......9.. +15.3......7..4.2....4.72.....8.........9..1.8.1..8.79......38...........6....7423 +..........5724...98....947...9..3...5..9..12...3.1.9...6....25....56.....7......6 +....75....1..2.....4...3...5.....3.2...8...1.......6.....1..48.2........7........ +6.....7.3.4.8.................5.4.8.7..2.....1.3.......2.....5.....7.9......1.... +....6...4..6.3....1..4..5.77.....8.5...8.....6.8....9...2.9....4....32....97..1.. +.32.....58..3.....9.428...1...4...39...6...5.....1.....2...67.8.....4....95....6. +...5.3.......6.7..5.8....1636..2.......4.1.......3...567....2.8..4.7.......2..5.. +.5.3.7.4.1.........3.......5.8.3.61....8..5.9.6..1........4...6...6927....2...9.. +..5..8..18......9.......78....4.....64....9......53..2.6.........138..5....9.714. +..........72.6.1....51...82.8...13..4.........37.9..1.....238..5.4..9.........79. +...658.....4......12............96.7...3..5....2.8...3..19..8..3.6.....4....473.. +.2.3.......6..8.9.83.5........2...8.7.9..5........6..4.......1...1...4.22..7..8.9 +.5..9....1.....6.....3.8.....8.4...9514.......3....2..........4.8...6..77..15..6. +.....2.......7...17..3...9.8..7......2.89.6...13..6....9..5.824.....891.......... +3...8.......7....51..............36...2..4....7...........6.13..452...........8.. + \ No newline at end of file From 05976b51fbd1a53047d5eaa53790e268b55b051d Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 21:44:19 -0700 Subject: [PATCH 14/22] TODO and use self.possible_values instead of explicit range --- sudoku.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sudoku.py b/sudoku.py index 9da9aa5..b5ca47a 100644 --- a/sudoku.py +++ b/sudoku.py @@ -120,7 +120,7 @@ class Board(dict): Clear the board. Resets each square's possible value list to the full range of possible values for this board. ''' for square in self: - self[square] = list(range(1, self.size + 1)) + self[square] = list(self.possible_values) def solve(self): for square, values in self.items(): @@ -143,6 +143,8 @@ class Board(dict): self.eliminate(square, v) except ValueError: raise + # TODO: To make this work right, I should probably also do the same as above for the values added *back* into + # the values for {square}. return True def eliminate(self, square, value): From 3d69b671851c12a9b2b16f82f1a41dca51acb102 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 22:05:16 -0700 Subject: [PATCH 15/22] Fix printing -- had the x and y coordinates flipped --- sudoku.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sudoku.py b/sudoku.py index b5ca47a..e47228d 100644 --- a/sudoku.py +++ b/sudoku.py @@ -201,10 +201,10 @@ class Board(dict): lines = [] box_lines = [] square_size = len(str(self.size)) - for x in range(self.size): + for y in range(self.size): row_squares = [] box_squares = [] - for y in range(self.size): + for x in range(self.size): square = self.get(self._xy_key(x, y)) if len(square) == 1: box_squares.append(str(square[0]).center(square_size)) @@ -218,7 +218,7 @@ class Board(dict): if len(box_lines) == self.box_size: lines.append('\n'.join(box_lines)) box_lines = [] - if x < self.size - 1: + if y < self.size - 1: # Don't print a divider on the last line. box_dividers = ['-' * ((square_size + 1) * self.box_size - 1) for box in range(self.box_size)] lines.append('\n{}\n'.format('-+-'.join(box_dividers))) From 6a5e13e345454af84b4ab92b870ab3c542914d4d Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 22:31:49 -0700 Subject: [PATCH 16/22] Add quick routines for parsing the puzzles in puzzles/ --- puzzles.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 puzzles.py diff --git a/puzzles.py b/puzzles.py new file mode 100644 index 0000000..62f5199 --- /dev/null +++ b/puzzles.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# Eryn Wells + +''' +Parser for puzzles in the ./puzzles directory. +''' + +import sudoku + + +euler = [] +norvig = [] + + +def parse_euler(filename='./puzzles/euler.txt'): + with open(filename, 'r') as f: + puzzle_lines = f.readlines() + + for puzzle in puzzle_lines: + # Chop the newline + if puzzle[-1] == '\n': + puzzle = puzzle[:-1] + print('Parsing puzzle: {}'.format(puzzle)) + if len(puzzle) != 81: + continue + kwargs = {} + for idx in range(len(puzzle)): + sq = puzzle[idx] + if sq not in '1234567890.': + continue + sq_int = 0 if sq == '.' else int(sq) + x, y = int(idx % 9), int(idx / 9) + if sq_int != 0: + kwargs[sudoku.Board.xy_kwargs_key(x, y)] = sq_int + euler.append(sudoku.Board(**kwargs)) + + +def parse_norvig(filename='./puzzles/norvig.txt'): + with open(filename, 'r') as f: + puzzle_lines = f.readlines() + + for puzzle in puzzle_lines: + # Chop the newline + if puzzle[-1] == '\n': + puzzle = puzzle[:-1] + print('Parsing puzzle: {}'.format(puzzle)) + if len(puzzle) != 81: + continue + kwargs = {} + for idx in range(len(puzzle)): + sq = puzzle[idx] + if sq not in '1234567890.': + continue + sq_int = 0 if sq == '.' else int(sq) + x, y = int(idx % 9), int(idx / 9) + if sq_int != 0: + kwargs[sudoku.Board.xy_kwargs_key(x, y)] = sq_int + norvig.append(sudoku.Board(**kwargs)) From 09c6c4287f3be99a7bef1f94d6da50b6f865adf8 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 22:32:05 -0700 Subject: [PATCH 17/22] Convert Euler puzzles into Norvig format --- puzzles/euler.txt | 550 +++++----------------------------------------- 1 file changed, 50 insertions(+), 500 deletions(-) diff --git a/puzzles/euler.txt b/puzzles/euler.txt index be23f6a..b0bb576 100644 --- a/puzzles/euler.txt +++ b/puzzles/euler.txt @@ -1,500 +1,50 @@ -Grid 01 -003020600 -900305001 -001806400 -008102900 -700000008 -006708200 -002609500 -800203009 -005010300 -Grid 02 -200080300 -060070084 -030500209 -000105408 -000000000 -402706000 -301007040 -720040060 -004010003 -Grid 03 -000000907 -000420180 -000705026 -100904000 -050000040 -000507009 -920108000 -034059000 -507000000 -Grid 04 -030050040 -008010500 -460000012 -070502080 -000603000 -040109030 -250000098 -001020600 -080060020 -Grid 05 -020810740 -700003100 -090002805 -009040087 -400208003 -160030200 -302700060 -005600008 -076051090 -Grid 06 -100920000 -524010000 -000000070 -050008102 -000000000 -402700090 -060000000 -000030945 -000071006 -Grid 07 -043080250 -600000000 -000001094 -900004070 -000608000 -010200003 -820500000 -000000005 -034090710 -Grid 08 -480006902 -002008001 -900370060 -840010200 -003704100 -001060049 -020085007 -700900600 -609200018 -Grid 09 -000900002 -050123400 -030000160 -908000000 -070000090 -000000205 -091000050 -007439020 -400007000 -Grid 10 -001900003 -900700160 -030005007 -050000009 -004302600 -200000070 -600100030 -042007006 -500006800 -Grid 11 -000125400 -008400000 -420800000 -030000095 -060902010 -510000060 -000003049 -000007200 -001298000 -Grid 12 -062340750 -100005600 -570000040 -000094800 -400000006 -005830000 -030000091 -006400007 -059083260 -Grid 13 -300000000 -005009000 -200504000 -020000700 -160000058 -704310600 -000890100 -000067080 -000005437 -Grid 14 -630000000 -000500008 -005674000 -000020000 -003401020 -000000345 -000007004 -080300902 -947100080 -Grid 15 -000020040 -008035000 -000070602 -031046970 -200000000 -000501203 -049000730 -000000010 -800004000 -Grid 16 -361025900 -080960010 -400000057 -008000471 -000603000 -259000800 -740000005 -020018060 -005470329 -Grid 17 -050807020 -600010090 -702540006 -070020301 -504000908 -103080070 -900076205 -060090003 -080103040 -Grid 18 -080005000 -000003457 -000070809 -060400903 -007010500 -408007020 -901020000 -842300000 -000100080 -Grid 19 -003502900 -000040000 -106000305 -900251008 -070408030 -800763001 -308000104 -000020000 -005104800 -Grid 20 -000000000 -009805100 -051907420 -290401065 -000000000 -140508093 -026709580 -005103600 -000000000 -Grid 21 -020030090 -000907000 -900208005 -004806500 -607000208 -003102900 -800605007 -000309000 -030020050 -Grid 22 -005000006 -070009020 -000500107 -804150000 -000803000 -000092805 -907006000 -030400010 -200000600 -Grid 23 -040000050 -001943600 -009000300 -600050002 -103000506 -800020007 -005000200 -002436700 -030000040 -Grid 24 -004000000 -000030002 -390700080 -400009001 -209801307 -600200008 -010008053 -900040000 -000000800 -Grid 25 -360020089 -000361000 -000000000 -803000602 -400603007 -607000108 -000000000 -000418000 -970030014 -Grid 26 -500400060 -009000800 -640020000 -000001008 -208000501 -700500000 -000090084 -003000600 -060003002 -Grid 27 -007256400 -400000005 -010030060 -000508000 -008060200 -000107000 -030070090 -200000004 -006312700 -Grid 28 -000000000 -079050180 -800000007 -007306800 -450708096 -003502700 -700000005 -016030420 -000000000 -Grid 29 -030000080 -009000500 -007509200 -700105008 -020090030 -900402001 -004207100 -002000800 -070000090 -Grid 30 -200170603 -050000100 -000006079 -000040700 -000801000 -009050000 -310400000 -005000060 -906037002 -Grid 31 -000000080 -800701040 -040020030 -374000900 -000030000 -005000321 -010060050 -050802006 -080000000 -Grid 32 -000000085 -000210009 -960080100 -500800016 -000000000 -890006007 -009070052 -300054000 -480000000 -Grid 33 -608070502 -050608070 -002000300 -500090006 -040302050 -800050003 -005000200 -010704090 -409060701 -Grid 34 -050010040 -107000602 -000905000 -208030501 -040070020 -901080406 -000401000 -304000709 -020060010 -Grid 35 -053000790 -009753400 -100000002 -090080010 -000907000 -080030070 -500000003 -007641200 -061000940 -Grid 36 -006080300 -049070250 -000405000 -600317004 -007000800 -100826009 -000702000 -075040190 -003090600 -Grid 37 -005080700 -700204005 -320000084 -060105040 -008000500 -070803010 -450000091 -600508007 -003010600 -Grid 38 -000900800 -128006400 -070800060 -800430007 -500000009 -600079008 -090004010 -003600284 -001007000 -Grid 39 -000080000 -270000054 -095000810 -009806400 -020403060 -006905100 -017000620 -460000038 -000090000 -Grid 40 -000602000 -400050001 -085010620 -038206710 -000000000 -019407350 -026040530 -900020007 -000809000 -Grid 41 -000900002 -050123400 -030000160 -908000000 -070000090 -000000205 -091000050 -007439020 -400007000 -Grid 42 -380000000 -000400785 -009020300 -060090000 -800302009 -000040070 -001070500 -495006000 -000000092 -Grid 43 -000158000 -002060800 -030000040 -027030510 -000000000 -046080790 -050000080 -004070100 -000325000 -Grid 44 -010500200 -900001000 -002008030 -500030007 -008000500 -600080004 -040100700 -000700006 -003004050 -Grid 45 -080000040 -000469000 -400000007 -005904600 -070608030 -008502100 -900000005 -000781000 -060000010 -Grid 46 -904200007 -010000000 -000706500 -000800090 -020904060 -040002000 -001607000 -000000030 -300005702 -Grid 47 -000700800 -006000031 -040002000 -024070000 -010030080 -000060290 -000800070 -860000500 -002006000 -Grid 48 -001007090 -590080001 -030000080 -000005800 -050060020 -004100000 -080000030 -100020079 -020700400 -Grid 49 -000003017 -015009008 -060000000 -100007000 -009000200 -000500004 -000000020 -500600340 -340200000 -Grid 50 -300200000 -000107000 -706030500 -070009080 -900020004 -010800050 -009040301 -000702000 -000008006 \ No newline at end of file +003020600900305001001806400008102900700000008006708200002609500800203009005010300 +200080300060070084030500209000105408000000000402706000301007040720040060004010003 +000000907000420180000705026100904000050000040000507009920108000034059000507000000 +030050040008010500460000012070502080000603000040109030250000098001020600080060020 +020810740700003100090002805009040087400208003160030200302700060005600008076051090 +100920000524010000000000070050008102000000000402700090060000000000030945000071006 +043080250600000000000001094900004070000608000010200003820500000000000005034090710 +480006902002008001900370060840010200003704100001060049020085007700900600609200018 +000900002050123400030000160908000000070000090000000205091000050007439020400007000 +001900003900700160030005007050000009004302600200000070600100030042007006500006800 +000125400008400000420800000030000095060902010510000060000003049000007200001298000 +062340750100005600570000040000094800400000006005830000030000091006400007059083260 +300000000005009000200504000020000700160000058704310600000890100000067080000005437 +630000000000500008005674000000020000003401020000000345000007004080300902947100080 +000020040008035000000070602031046970200000000000501203049000730000000010800004000 +361025900080960010400000057008000471000603000259000800740000005020018060005470329 +050807020600010090702540006070020301504000908103080070900076205060090003080103040 +080005000000003457000070809060400903007010500408007020901020000842300000000100080 +003502900000040000106000305900251008070408030800763001308000104000020000005104800 +000000000009805100051907420290401065000000000140508093026709580005103600000000000 +020030090000907000900208005004806500607000208003102900800605007000309000030020050 +005000006070009020000500107804150000000803000000092805907006000030400010200000600 +040000050001943600009000300600050002103000506800020007005000200002436700030000040 +004000000000030002390700080400009001209801307600200008010008053900040000000000800 +360020089000361000000000000803000602400603007607000108000000000000418000970030014 +500400060009000800640020000000001008208000501700500000000090084003000600060003002 +007256400400000005010030060000508000008060200000107000030070090200000004006312700 +000000000079050180800000007007306800450708096003502700700000005016030420000000000 +030000080009000500007509200700105008020090030900402001004207100002000800070000090 +200170603050000100000006079000040700000801000009050000310400000005000060906037002 +000000080800701040040020030374000900000030000005000321010060050050802006080000000 +000000085000210009960080100500800016000000000890006007009070052300054000480000000 +608070502050608070002000300500090006040302050800050003005000200010704090409060701 +050010040107000602000905000208030501040070020901080406000401000304000709020060010 +053000790009753400100000002090080010000907000080030070500000003007641200061000940 +006080300049070250000405000600317004007000800100826009000702000075040190003090600 +005080700700204005320000084060105040008000500070803010450000091600508007003010600 +000900800128006400070800060800430007500000009600079008090004010003600284001007000 +000080000270000054095000810009806400020403060006905100017000620460000038000090000 +000602000400050001085010620038206710000000000019407350026040530900020007000809000 +000900002050123400030000160908000000070000090000000205091000050007439020400007000 +380000000000400785009020300060090000800302009000040070001070500495006000000000092 +000158000002060800030000040027030510000000000046080790050000080004070100000325000 +010500200900001000002008030500030007008000500600080004040100700000700006003004050 +080000040000469000400000007005904600070608030008502100900000005000781000060000010 +904200007010000000000706500000800090020904060040002000001607000000000030300005702 +000700800006000031040002000024070000010030080000060290000800070860000500002006000 +001007090590080001030000080000005800050060020004100000080000030100020079020700400 +000003017015009008060000000100007000009000200000500004000000020500600340340200000 +300200000000107000706030500070009080900020004010800050009040301000702000000008006 From fdee9ed716f8bfbbfcf833fa41e7849be7ce6d83 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 22:32:15 -0700 Subject: [PATCH 18/22] Couple tweaks to help with parsing Euler and Norvig puzzles - Make xy_key and xy_kwargs_key classmethods --- sudoku.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sudoku.py b/sudoku.py index e47228d..95dee07 100644 --- a/sudoku.py +++ b/sudoku.py @@ -29,7 +29,7 @@ class Board(dict): all possible values for the square. If an initial value was given, make sure it is one of the valid initial values. Raise a ValueError if not. ''' - initial_value = kwargs.get('x{}y{}'.format(x, y)) + initial_value = kwargs.get(Board.xy_kwargs_key(x, y)) if initial_value is None: return list(self.possible_values) if initial_value not in self.possible_values: @@ -37,14 +37,23 @@ class Board(dict): return [initial_value] # Make the grid. - super(Board, self).__init__([(self._xy_key(x, y), kwget(x, y)) + super(Board, self).__init__([(Board.xy_key(x, y), kwget(x, y)) for x in range(self.size) for y in range(self.size)]) - def _xy_key(self, x, y): + @classmethod + def xy_key(self, x, y): '''Given {x} and {y}, generate a key to refer to the square at coordinate (x, y) in the grid.''' return (int(x), int(y)) + @classmethod + def xy_kwargs_key(self, x, y): + ''' + Given {x} and {y}, generate a key to refer to the square in the __init__ kwargs at coordinate (x, y) in the + grid. + ''' + return 'x{}y{}'.format(x, y) + @property def solved(self): ''' @@ -205,7 +214,7 @@ class Board(dict): row_squares = [] box_squares = [] for x in range(self.size): - square = self.get(self._xy_key(x, y)) + square = self.get(Board.xy_key(x, y)) if len(square) == 1: box_squares.append(str(square[0]).center(square_size)) else: @@ -223,5 +232,3 @@ class Board(dict): box_dividers = ['-' * ((square_size + 1) * self.box_size - 1) for box in range(self.box_size)] lines.append('\n{}\n'.format('-+-'.join(box_dividers))) return ''.join(lines) - - From 81e3296f6ff4302f3e2ea1c6aade7c5812d1630e Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Aug 2013 22:46:38 -0700 Subject: [PATCH 19/22] Create keys then assign values in __init__ --- sudoku.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/sudoku.py b/sudoku.py index 95dee07..ea78cc2 100644 --- a/sudoku.py +++ b/sudoku.py @@ -23,23 +23,18 @@ class Board(dict): # The range of possible values for a square. self.possible_values = range(1, self.size + 1) - def kwget(x, y): - ''' - Try to get an initial value for square ({x}, {y}) from the given kwargs. If none exists, return a list of - all possible values for the square. If an initial value was given, make sure it is one of the valid initial - values. Raise a ValueError if not. - ''' - initial_value = kwargs.get(Board.xy_kwargs_key(x, y)) - if initial_value is None: - return list(self.possible_values) - if initial_value not in self.possible_values: - raise ValueError('Invalid initial value for square ({}, {}): {}'.format(x, y, initial_value)) - return [initial_value] - # Make the grid. - super(Board, self).__init__([(Board.xy_key(x, y), kwget(x, y)) + super(Board, self).__init__([(Board.xy_key(x, y), list(self.possible_values)) for x in range(self.size) for y in range(self.size)]) + for square in self: + initial_value = kwargs.get(Board.xy_kwargs_key(*square)) + if initial_value is None: + continue + if initial_value not in self.possible_values: + raise ValueError('Invalid initial value for square ({}, {}): {}'.format(x, y, initial_value)) + LOG.debug('Setting initial value of {} to {}'.format(square, initial_value)) + self[square] = [initial_value] @classmethod def xy_key(self, x, y): @@ -121,7 +116,7 @@ class Board(dict): # Generate a dictionary of all the squares in the row, column, and box containing the given square. peers = dict(list(self.row(y).items()) + list(self.col(x).items()) + list(self.box(x, y).items())) # Remove the given square. - del peers[self._xy_key(x, y)] + del peers[Board.xy_key(x, y)] return peers def clear(self): From cc5e35ff8194ee29390b86a14255b95a11348f5b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 30 Aug 2013 10:11:44 -0700 Subject: [PATCH 20/22] Working on search() --- sudoku.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sudoku.py b/sudoku.py index ea78cc2..ea1bbd8 100644 --- a/sudoku.py +++ b/sudoku.py @@ -135,6 +135,14 @@ class Board(dict): raise return self.solved + def search(self): + if self.solved: + return True + + # Chose the square with the fewest possible values. + _, smallest = min((len(self[sq]), sq) for sq in self if len(self[sq]) > 1) + # Deepcopy the board. + def assign(self, square, value): ''' Assign {value} to {square}. {value} is expected to be an iterable (a list). {key} should be a valid (x, y) From 6cc3fed4b3e4c6d60ed3153d42ae1ceb6612f931 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 30 Aug 2013 17:09:41 -0700 Subject: [PATCH 21/22] Implement search - Remove __setitem__; it was getting in the way... - deepcopy the board for each trial in search() --- sudoku.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/sudoku.py b/sudoku.py index ea1bbd8..efdabc3 100644 --- a/sudoku.py +++ b/sudoku.py @@ -5,6 +5,7 @@ A Sudoku solver. ''' +import copy import logging import math @@ -34,7 +35,7 @@ class Board(dict): if initial_value not in self.possible_values: raise ValueError('Invalid initial value for square ({}, {}): {}'.format(x, y, initial_value)) LOG.debug('Setting initial value of {} to {}'.format(square, initial_value)) - self[square] = [initial_value] + self.assign(square, [initial_value]) @classmethod def xy_key(self, x, y): @@ -119,29 +120,36 @@ class Board(dict): del peers[Board.xy_key(x, y)] return peers - def clear(self): + def clear(self, squares=None): ''' Clear the board. Resets each square's possible value list to the full range of possible values for this board. ''' - for square in self: - self[square] = list(self.possible_values) + for square in (self if squares is None else squares): + self.assign(square, list(self.possible_values)) def solve(self): - for square, values in self.items(): - if len(values) == 1: - try: - self.assign(square, values) - except ValueError: - raise - return self.solved + return self.search() def search(self): + ''' + Search for a solution, depth-first. Return the solved Board as a new instance of this class. + ''' if self.solved: - return True + return self # Chose the square with the fewest possible values. _, smallest = min((len(self[sq]), sq) for sq in self if len(self[sq]) > 1) # Deepcopy the board. + for v in self[smallest]: + trial_board = copy.deepcopy(self) + try: + trial_board.assign(smallest, [v]) + if trial_board.search(): + return trial_board + except ValueError: + continue + raise ValueError('No possible solution found.') + def assign(self, square, value): ''' @@ -196,19 +204,13 @@ class Board(dict): raise ValueError('No place for value {} to go in unit {}; board is now invalid'.format(value, unit)) elif len(places) == 1: LOG.debug('One place for value {} to be in unit {}; setting'.format(value, unit)) - self[places[0]] = [value] + self.assign(places[0], [value]) return True def __delitem__(self, key): # Don't allow deleting keys from self. pass - def __setitem__(self, key, value): - if key not in self: - # Don't allow adding new keys, only changes to existing ones. - raise KeyError('Key {} is not a valid coordinate pair.'.format(key)) - self.assign(key, value) - def __str__(self): lines = [] box_lines = [] From e004d319e31c6f9b642bac5ded4047869e9b19bb Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 6 Oct 2017 20:27:01 -0700 Subject: [PATCH 22/22] IDK what this WIP is --- sudoku.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/sudoku.py b/sudoku.py index efdabc3..964e6be 100644 --- a/sudoku.py +++ b/sudoku.py @@ -140,15 +140,18 @@ class Board(dict): # Chose the square with the fewest possible values. _, smallest = min((len(self[sq]), sq) for sq in self if len(self[sq]) > 1) # Deepcopy the board. + trials = [] for v in self[smallest]: trial_board = copy.deepcopy(self) - try: - trial_board.assign(smallest, [v]) - if trial_board.search(): - return trial_board - except ValueError: - continue - raise ValueError('No possible solution found.') + if trial_board.assign(smallest, [v]): + trials.append(trial_board) + trial_board.search() + else: + trials.append(None) + for t in trials: + if t is not None: + return t + return False def assign(self, square, value): @@ -159,12 +162,8 @@ class Board(dict): LOG.debug('Assigning values {} to square {}.'.format(value, square)) removed_values = set(self[square]) - set(value) for v in removed_values: - try: - self.eliminate(square, v) - except ValueError: - raise - # TODO: To make this work right, I should probably also do the same as above for the values added *back* into - # the values for {square}. + if not self.eliminate(square, v): + return False return True def eliminate(self, square, value): @@ -186,25 +185,24 @@ class Board(dict): if len(self[square]) == 0: # Whoops. Removed the last value... We have a contradiction now. LOG.error('Removed last value from square {}'.format(square)) - raise ValueError('Removed last value from square {}; board is now invalid'.format(square)) + return False elif len(self[square]) == 1: # One value left in this square. Propagate changes to its peers. LOG.debug('One value left in square {}; eliminating {} from its peers'.format(square, self[square][0])) - try: - for peer in self.peers(*square): - self.eliminate(peer, self[square][0]) - except ValueError: - raise + for peer in self.peers(*square): + if not self.eliminate(peer, self[square][0]): + return False # (2) If a unit has only one square for a value, put it there. for unit in (self.row(square[1]), self.col(square[0]), self.box(*square)): places = [sq for sq in unit if value in unit[sq]] if len(places) == 0: LOG.error('No place for value {} to go in unit {}'.format(value, unit)) - raise ValueError('No place for value {} to go in unit {}; board is now invalid'.format(value, unit)) + return False elif len(places) == 1: LOG.debug('One place for value {} to be in unit {}; setting'.format(value, unit)) self.assign(places[0], [value]) + return True def __delitem__(self, key):