This repository was archived by the owner on Jan 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Sudoku problem #25
Open
grantg012
wants to merge
2
commits into
exercism:main
Choose a base branch
from
grantg012:sudoku
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sudoku problem #25
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Instructions | ||
|
||
Sudoku is the popular number puzzle of fitting the numbers 1-9 into each column, row, | ||
and 3x3 block using each exactly once. Write a z3 function that takes a 9x9 list of lists | ||
containing the sudoku puzzle (row major order), and returns the solution as a 9x9 list of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that "row major order" may be a little confusing to readers. Is there a simpler way to explain this? |
||
list. Empty cells will be denoted by 0. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from z3 import * | ||
|
||
def soduku(puzzle: list) -> list: | ||
"""Solve the sudoku puzzle and return the solution""" | ||
|
||
# TODO: Write your code here | ||
|
||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from z3 import * | ||
|
||
def sudoku(puzzle: list) -> list: | ||
"""Solve the sudoku puzzle and return the solution""" | ||
X = [[Int("x:{},{}".format(row + 1, col + 1)) for col in range(9)] for row in range(9)] | ||
|
||
# each cell contains a value in {1, ..., 9} | ||
range_constraint = [And(1 <= X[col][row], X[col][row] <= 9) for col in range(9) for row in range(9)] | ||
|
||
# each row contains a digit at most once | ||
rows_constraint = [Distinct(X[row]) for row in range(9)] | ||
|
||
# each column contains a digit at most once | ||
columns_constraint = [Distinct([X[row][col] for row in range(9)]) for col in range(9)] | ||
|
||
# each 3x3 square contains a digit at most once | ||
square_constraint = [Distinct([X[3 * majRow + minRow][3 * majCol + minCol] | ||
for minRow in range(3) for minCol in range(3)]) | ||
for majRow in range(3) for majCol in range(3)] | ||
|
||
sudoku_constraints = range_constraint + rows_constraint + columns_constraint + square_constraint | ||
|
||
""" | ||
((0, 0, 0, 0, 9, 4, 0, 3, 0), | ||
(0, 0, 0, 5, 1, 0, 0, 0, 7), | ||
(0, 8, 9, 0, 0, 0, 0, 4, 0), | ||
(0, 0, 0, 0, 0, 0, 2, 0, 8), | ||
(0, 6, 0, 2, 0, 1, 0, 5, 0), | ||
(1, 0, 2, 0, 0, 0, 0, 0, 0), | ||
(0, 7, 0, 0, 0, 0, 5, 2, 0), | ||
(9, 0, 0, 0, 6, 5, 0, 0, 0), | ||
(0, 4, 0, 9, 7, 0, 0, 0, 0)) | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this comment can be removed since it won't be the only solution (since you will also be testing the puzzles in the .txt file) |
||
|
||
# Ensure that the generated sudoku solution works with the fits the given puzzle. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "works with the fits the" should probably be "works with the" OR "fits the", not both |
||
puzzle_constraint = [If(puzzle[row][col] == 0, | ||
True, | ||
X[row][col] == puzzle[row][col]) | ||
for row in range(9) for col in range(9)] | ||
|
||
s = Solver() | ||
s.add(sudoku_constraints + puzzle_constraint) | ||
if(s.check() == sat): | ||
m = s.model() | ||
r = [[m.evaluate(X[row][col]).as_long() for col in range(9)] for row in range(9)] | ||
return r | ||
else: | ||
return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that you need to say "Write a Z3 function ..." since this will be under the Z3 track. Maybe just start the sentence with "Write a function ..."