Web Sudoku Solver

In my last post I used Web Sudoku to get a Sudoku as an example for my solver.

After that I wanted to automate the process of looking up a Sudoku, solving it and typing it in. But while trying to get the Sudoku’s numbers, I noticed that the whole, solved Sudoku was stored in plaintext! (Look at this page‘s source code)
So I just needed to get that information, open the Web Sudoku page in a browser and type in the already solved Sudoku.

To accomplish said goal I used the python module urllib to get the Web Sudoku page’s source code and the module webbrowser to open the page in a browser. To type in the Sudoku I used AutoHotkey.

The finished program takes a level (easy, medium, hard or evil) and an id (the Sudoku’s identification number) to get a Sudoku, create an AHK file, execute it and open a web browser.
All you have to do is to click into the first box, press a key (F1 in this case) and the Sudoku gets solved! You then just need to wait a minute, which is the minimum time Web Sudoku wants you to take to solve a Sudoku, and the AHK script hits enter.
You can get really good times with this.

Solved evil puzzle #66 in only 65 seconds!

# Python 2.7.7 Code
# Jonathan Frech  9th of August, 2016
#         edited 10th of August, 2016
#         edited 11th of August, 2016

# NOTE: AutoHotkey needs to be installed for this program to work properly!

# import
import urllib, re, os, webbrowser

# set level and sudoku id
LEVEL = {"easy":1, "medium":2, "hard":3, "evil":4}["evil"]
ID = 66

# set other variables
KEY = "F1"
PATH = os.getcwd() + "/sud.ahk"

# solving function
def solve(level, id):
	# fetch and process webpage
	URL = "http://www.websudoku.com/?level=%d&set_id=%d" % (LEVEL, ID)
	URL_VIEW = "http://view.websudoku.com/?level=%d&set_id=%d" % (LEVEL, ID)
	websudoku = urllib.urlopen(URL_VIEW).read()
	# websudoku can block IPs...
	blockedipmessage = "This IP address has been blocked from accessing Web Sudoku since we received too many requests from it."
	if websudoku[:len(blockedipmessage)] == blockedipmessage:
		return "This IP unfortunately got blocked."

	# split the sudoku!
	solution = re.split("var cheat='", websudoku)[1]
	solution = re.split("';\n\t\t\tvar pre", solution)[0]

	# your cursor needs to be in the first box (even if it is not empty!) 
	keys = ""
	# add keys for solving the sudoku
	for _ in range(0, 9**2):
		keys += solution[_] + ["{TAB}", ""][_ >= 9**2-1]

	# enter to complete input
	keys += ""

	# save to an AHK file
	file = "#SingleInstance force\n" + KEY + "::\n\tSend, " + keys + "\n\tSleep, 60000\n\tSend, {ENTER}\n\tExitApp"
	doc = open(PATH, "w")

	# open webpage
	#os.system("C:/Tor Browser/Browser/firefox.exe " + URL)
	webbrowser.open(URL, 1)

	# open AHK file

	# remove AHK file

# try to execute the solve function
	solve(LEVEL, ID)
	print "An error occurred..."

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.