Sudoku puzzle solver using awk

Solving Sudoku with awk

awk is a fun and powerful language available in the command-line of Linux, Mac OS X, and even Windows (with a little help from Cygwin). In fact, our own Dale Dougherty co-authored one of the classic books on awk (and sed, another great Unix power tool) back in 1990 (the second edition was released in 1997), sed & awk.

At The Geek Stuff, Bill Duncan has posted a fun awk program that can solve Sudoku puzzles:

The application I chose to use as an example is "yet another sudoku puzzle solver". I must confess at the outset that I have never sat down to solve one of these puzzles myself, but sketched this out over a few days while commuting on a train and watching other people work on them. It was far more fun I think than actually doing any of the puzzles..


[...]

This program uses a very simple depth-first recursive backtracking algorithm with up-front and ongoing elimination of invalid entries. Awk may not have the expressive power for representing complex data that perl or other languages have, but with care, many moderate sized problems and data sets can be used. This algorithm may not be the best one around, but it is certainly fast enough for most problems and is easy to implement.

When you strip out blank lines and comments it's only 67 lines! Keeping in the awk spirit, that would be:

awk '!/^[ \t]*#/ && !/^$/' solve.awk | wc -l

Yet Another Sudoku Puzzle Solver Using AWK

If you need to generate some puzzles to throw at it, try this Sudoku Generator written in Python.


Recent Entries

Comments

Oldest comments listed first.

Posted by: charlie on January 24, 2010 at 10:47 AM

awk without cygwin

you don't need to clutter up windows by adding cygwin, there are a few native ones, gnuish, unxutils, even a native port of the one true awk, as well as lookalikes like gawk.

Features vary.


Posted by: Brian Jepson on January 24, 2010 at 11:12 AM

Thanks, Charlie; I usually install cygwin on many of my machines, but true, it's not the simplest way to get your hands on awk.


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television




Check out more videos from MAKE.

MZ_DIYMovieMaking-RR.gif
Maker SHED

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter



    MAKE Archives

    Make: Money

    Make: Science Room
    Subscribe to MAKE Magazine!

    Make: Online editors and authors!

    Gareth BranwynGareth Branwyn
    Editor-in-Chief


    Phillip TorronePhillip Torrone
    Senior Editor
    | Web | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter


    John BaichtalJohn Baichtal
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Current Podcast

    itunesdl.gif Weekend Project: Gourd Lanterns Turn hard-shell gourds into decorative party lights. Thanks go to Diane Gilleland for the original article in MAKE Volume 21. To download The Gourd Lanterns video click here and subscribe in iTunes. Check out the complete Gourd Lanterns article... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    Sign up for the Make: Newsletter

    Our Make: Newsletter covers news from maker Media, has original columns, Shed deals, and more! You can also read the archives of past issues.


     



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en EspaƱol MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog