11"""
2+ Project Euler Problem 96: https://projecteuler.net/problem=96
3+
24Problem Statement:
35Su Doku (Japanese meaning number place) is the name given to a popular puzzle
46concept. Its origin is unclear, but credit must be attributed to Leonhard
4143
4244By solving all fifty puzzles find the sum of the 3-digit numbers found in
4345the top left corner of each solution grid; for example, 483 is the 3-digit
44- number found in the top left corner of the solution grid above."""
46+ number found in the top left corner of the solution grid above.
47+
48+ Solution:
49+ We keep a track of the digits that are already present in each row,
50+ column and box, and use that to check which digits can be used to fill an unfilled
51+ cell. This process is then repeated recursively until the puzzle is solved, after
52+ which the 3 digit numbers formed by the top left corner of each puzzle are added.
53+
54+ References:
55+ https://en.wikipedia.org/wiki/Sudoku
56+ https://en.wikipedia.org/wiki/Backtracking
57+ """
4558
4659import os
4760
@@ -57,6 +70,27 @@ def solve(
5770) -> bool :
5871 """
5972 Recursive backtracking function to solve the sudoku
73+
74+ >>> solve(
75+ ... [(0, 0)],
76+ ... [0b111110111, 0b111111111, 0b111111111, 0b111111111,
77+ ... 0b111111111, 0b111111111, 0b111111111, 0b111111111, 0b111111111],
78+ ... [0b111110111, 0b111111111, 0b111111111, 0b111111111,
79+ ... 0b111111111, 0b111111111, 0b111111111, 0b111111111, 0b111111111],
80+ ... [0b111110111, 0b111111111, 0b111111111, 0b111111111,
81+ ... 0b111111111, 0b111111111, 0b111111111, 0b111111111, 0b111111111],
82+ ... [["0","8","3","9","2","1","6","5","7"],
83+ ... ["9","6","7","3","4","5","8","2","1"],
84+ ... ["2","5","1","8","7","6","4","9","3"],
85+ ... ["5","4","8","1","3","2","9","7","6"],
86+ ... ["7","2","9","5","6","4","1","3","8"],
87+ ... ["1","3","6","7","9","8","2","4","5"],
88+ ... ["3","7","2","6","8","9","5","1","4"],
89+ ... ["8","1","4","2","5","3","7","6","9"],
90+ ... ["6","9","5","4","1","7","3","8","2"]],
91+ ... 0,
92+ ... 1)
93+ True
6094 """
6195 if i == n :
6296 return True
@@ -93,6 +127,18 @@ def solve(
93127def solve_sudoku (board : list [list [str ]]) -> int :
94128 """
95129 Solve a single sudoku puzzle and return the first 3 digits
130+
131+ >>> solve_sudoku(
132+ ... [["0","0","3","0","2","0","6","0","0"],
133+ ... ["9","0","0","3","0","5","0","0","1"],
134+ ... ["0","0","1","8","0","6","4","0","0"],
135+ ... ["0","0","8","1","0","2","9","0","0"],
136+ ... ["7","0","0","0","0","0","0","0","8"],
137+ ... ["0","0","6","7","0","8","2","0","0"],
138+ ... ["0","0","2","6","0","9","5","0","0"],
139+ ... ["8","0","0","2","0","3","0","0","9"],
140+ ... ["0","0","5","0","1","0","3","0","0"]])
141+ 483
96142 """
97143 unfilled = []
98144 row = [0 ] * 9
0 commit comments