As Promised

SUDOKU!

Several days ago I posted about an R package that I had randomly stumbled across while I was lecturing as part of the week-long graduate course known as Geographical Epidemiology. That package was the Sudoku package, and it allowed me to generate some Sudoku puzzles on a whim.

Being the Sudoku-loving fellow that I am, I of course sat down and started generating a bunch of puzzles. I also whipped up a short script to pretty up the output, because well, why not?

Anyway, I promised a few days ago that I would release the solutions to those puzzles. You will find those below. And for any of you who might want to generate your own Sudoku puzzles, I’ve also included the short R script that I wrote.

Enjoy!


Solutions


R Script

##################
# Sudoku
# D. Gillis
# June 2012
##################

##################
# Clear Memory
##################
remove(list=ls())

##################
# Libraries
##################
library(sudoku)

##################
# Constants
##################
setwd(Enter your working directory here)
numBoards<-2 # Will produce numBoards*numBoards Sudoku boards

##################
# Functions
##################
generateBoard<-function(sudoku.values)
{
plot(NULL, xlim=c(0, 9), ylim=c(0, 9), xlab=””, ylab=””, xaxt=”n”, yaxt=”n”)
for (lr in 0:9)
{
linewidth<-1
if(lr==3 | lr==6){linewidth<-3}
lines(c(lr, lr), c(0, 9), lwd=linewidth)
lines(c(0, 9), c(lr, lr), lwd=linewidth)
}
for (r in 1:9)
{
for (c in 1:9)
{
if (sudoku.values[r, c]!=0)
{
text(r-0.5, c-0.5, sudoku.values[r, c], cex=2)
}
}
}
}

##################
# Make Sudoku
##################
for (i in 1:numBoards)
{
for (j in 1:numBoards)
{
sudoku.board<-generateSudoku()
sudoku.solution<-solveSudoku(sudoku.board)

# Generate jpegs of puzzle
jpeg(paste(“Sudoku”, 2*(i-1)+j ,”.jpg”, sep=””), width=960, height=960)
generateBoard(sudoku.board)
dev.off()

# Generate jpegs of solutions
jpeg(paste(“Sudoku”, 2*(i-1)+j ,”_Solution.jpg”, sep=””), width=960, height=960)
generateBoard(sudoku.solution)
dev.off()
}
}


Advertisements

One thought on “As Promised

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s