Little Puzzlers–Validate a Sudoku Board

I like to keep my brain sharp by working on programming puzzlers. On off weeks I'm going to start posting programming puzzlers I've collected over the years. Hopefully you'll find them as entertaining as I do.

This one is a more straight-forward puzzler.  There’s no gotcha or “ah-hah” moment in solving this, it’s just an exercise in coding.  I’ll try to vary up the difficulty level of problems so they’re not all easy and not all brain-busters…

The Problem

Given a 2d 9 x 9 array of char representing an in-progress Sudoku board, validate that the board currently has no errors.  The​ board may be partially filled and may be unsolvable, the challenge is to simply determine if there are any errors with the solution so far.​

A Sudoku board has no errors if there are no numbers duplicated in the same row, or in the same column, or in the same 3x3 cube (as shown below by the heavy lines dividing the board into 9 equal 3x3 cubes).

Empty cells will be represented by a space (' ') otherwise, the cells will have the character representation of the numbers '1' thru '9'.

For example, this board:

250px-Sudoku-by-L2G-20050714_svg

Would be represented by the 2d array:

1: var board = new char[9,9]

2: {

3: {'5', '3', ' ', ' ', '7', ' ', ' ', ' ', ' '},

4: {'6', ' ', ' ', '1', '9', '5', ' ', ' ', ' '},

5: {' ', '9', '8', ' ', ' ', ' ', ' ', '6', ' '},

6: {'8', ' ', ' ', ' ', '6', ' ', ' ', ' ', '3'},

7: {'4', ' ', ' ', '8', ' ', '3', ' ', ' ', '1'},

8: {'7', ' ', ' ', ' ', '2', ' ', ' ', ' ', '6'},

9: {' ', '6', ' ', ' ', ' ', ' ', '2', '8', ' '},

10: {' ', ' ', ' ', '4', '1', '9', ' ', ' ', '5'},

11: {' ', ' ', ' ', ' ', '8', ' ', ' ', '7', '9'},

12: };

Again, the goal is to simply state if there are any errors in the Sudoku board as it stands, not to determine if the puzzle has a solution or is solvable.

Spoiler Alert!

Fair Warning: there may be discussion of the problem and potential solutions posted in the comments below. Read at your own risk.

This article is part of the GWB Archives. Original Author: James Michael Hare

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.