GithubHelp home page GithubHelp logo

udacity-sudoku's Introduction

Diagonal Sudoku Solver with Naked Twins

This repository contains the solution to the first project of the Udacity Artificial Intelligence Nanodegree.

Naked Twins

Constraint propagation is a search strategy for the solution of a constrained optimization problem. During search we simply remove from the solution space possibilities which do not respect all constraints. In general, the more constraints we identify the faster this strategy will work because the constraints rule out
unfeasible solutions from the search space. Naked Twins is a constraint on the peers of tuples of boxes in the sudoku (reference here http://www.sudokudragon.com/tutorialnakedtwins.htm).

It was implemented in the following function:

def naked_twins(values):
    """Eliminate values using the naked twins strategy.
    Args:
        values(dict): a dictionary of the form {'box_name': '123456789', ...}

    Returns:
        the values dictionary with the naked twins eliminated from peers.
    """

    # Find boxes with 2 entries
    candidates = [box for box in values.keys() if len(values[box]) == 2]

    # Collect boxes that have the same elements
    twins = [[box1,box2] for box1 in candidates for box2 in peers[box1] if set(values[box1]) == set(values[box2])]

    for b1,b2 in twins:
        print(b1, b2, values[b1])

    for box1, box2 in twins:

        peers1 = set(peers[box1])
        peers2 = set(peers[box2])

        peers_int = peers1.intersection(peers2)

        # delete the two digits from all common peers
        for peer_box in peers_int:
            for rm_val in values[box1]:
                values = assign_value(values, peer_box, values[peer_box].replace(rm_val,''))

    return values

Diagonal Sudoku

Modifing the original sudoku problem to add additional constraints on the diagonals is easy, because we simply add the diagonals in the set of possible peers of a box.

# Add diagonal units among the peers
diagonal_units = [[r+c for r,c in zip(rows,cols)] , [r+c for r,c in zip(rows[::-1],cols)]]
unitlist = row_units + column_units + square_units + diagonal_units
units = dict((s, [u for u in unitlist if s in u]) for s in boxes)
peers = dict((s, set(sum(units[s], [])) - set([s])) for s in boxes)

Code

  • solution.py - My coded solution in the function naked_twins
  • solution_test.py - some test python solution_test.py.
  • PySudoku.py - Visuzalizes the solution
  • visualize.py - Visualizes the solution

udacity-sudoku's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.