Вы находитесь на странице: 1из 10

\documentclass[11pt,letterpaper]{article}

\usepackage{graphicx}

\begin{document}

\begin{flushright}
{\large Ben Goodman}
\end{flushright}

\begin{center}
{\Large CS173 Honors Add-on HW 2}
\end{center}
\textbf{\large Problem 1}
\\

In essence, first realize that Sudoku is made up of 81 different boxes, thus I will
refer to each separate box as ``box1" through ``box81". Counting through the rows
left to right, then dropping top to bottom. In other words, I start out at the left
uppermost box and run through 1-9 through the entire first row, then drop to the
second row which is 10-18, etc. etc. until we reach the right lowermost box, box
81. To simplify into a formula basis, we will refer to ``box x" as $box_x$. So box 1
would be called $box_1$.

The Sudoku constraints can be described as follows, now that we have established how
to refer to individual boxes:
\\

We have 3 basic rules, and can establish these in terms of ``formulas" using our
notation.
\\

1) Every column must contain distinct numbers 1-9. So boxes defined
by $box_{n+9p}$ must all be distinct, where $p$ and $n$ are integers and $1 \leq p
\leq 8$ and $1 \leq n \leq 8$. For every $n$, all boxes defined by the
possible $p$ values (1-8) must be distinct numbers 1-9.
\\

2) Every row must contain distinct numbers 1-9. So boxes defined
by $box_{n}$ through $box_{n+8}$ must be distinct, where $n$ is defined by $1 (mod
9)$, but $1 \leq n \leq 73$ as well. This results
in $box_{1}$ through $box_{9}$ being distinct, as well
as $box_{10}$ through $box_{18}$, etc.
\\

3) Every 3-by-3 larger box must contain distinct numbers 1-9. Let's call the 3-by-3
boxes "squares" so we don't confuse them with the smaller cells. The Sudoku puzzle is
split up into 9 of these squares, and each of the squares must contain distinct
numbers 1-9. So squares defined
by $box_{n}$ through $box_{n+2}$, $box_{n+9}$ through $box_{(n+9)+2}$,
and$box_{((n+9)+9)}$ through $box_{((n+9)+9) +2}$ must all be distinct. This tends to
look a little sloppy, but we can simplify. $n$ can only be numbers defined by $1 (mod
9)$, $4 (mod 9)$, and $7 (mod 9)$. So for rule 3, $box_{n}$ must always define a box
that is in the left uppermost corner of a square.
\pagebreak
\\
\textbf{\large Problem 2}
\\

I will first use a for loop to loop through all the boxes, literally printing out
Strings that declare variables for each box in the Z3 format:

``(declare-const box" + i + " Int)"
\\
where $i$ starts at 1 and ends at 81.


Next, I will convert the puzzle file into a 1D String array with length 82 (82 because
I will want to start at cell 1 and end at cell 81, not cell 0 to 80, just to make the
code flow a little nicer with the concept of Sudoku cells starting at 1 and ending at
81). I manually write out statements that follow the pattern:

``(assert (distinct `` + box[1] + " `` + box[2] + " `` + box[3] + " `` + box[4] + " ``
+ box[5] + " `` + box[6] + " `` + box[7] + " ``+ box[8] + " `` + box[9] + "))"

where box[1] is replaced by the name of the actual 1D String array that I will set up,
but box[1] will refer to the \textit{actual integer} in box 1.

For this part, I will manually write out statements that assert that each row, each
column, and each 3-by-3 square contain distinct values. For example, the statement
that I wrote above would simply assert the first row of the puzzle.

Finally, the last part is pretty simple. I will simply write another for loop that
loops from $i=1$ to $i=81$ that checks that each box holds a value between 1 and 9:

``(assert (and($>$ box`` + i + " 0) ($<$ box`` + i + " 10)))"
\\
\\
\textbf{\large Problem 3}
\\

First pick a number $n$ that is 1-9. Now, look at the puzzle given and note all valid
spots for $n$ on that puzzle, and for every valid spot, change the last of
the Z3 statements in the code (the ones that assert whether box i's value is between 1
and 9) so that the statement for that one valid spot is locked into determining that
box's value as $n$.

In other words, if your $n$ is, say, 8, and one of the valid spots for 8 is box 73,
you'd change the Z3 statement:

``(assert (and($>$ box73 0) ($<$ box73 10)))"
\\
to

``(assert (= box73 8))"

After running this in Z3, and checking this for all of the valid boxes for $n$, if any
turn out to be satisfiable, and result in a solution that is different from your first
solution (the one where you didn't lock in any of the boxes as your $n$ value), then
you know the puzzle obviously must have more than one solution.
\\
\\
\textbf{\large Source Code}
\\
\\
See attached files.
\\

Ignore the supposed errors next to BufferedReader statements... I printed out the code
using Eclipse's Java environment but it seems that my JRE was outdated on Eclipse so
it didn't like the BufferedReader -- I wrote the code using compileonline.com which
worked at a different JRE level I suppose.

The honors2puzzle.java file is the code I used to print out the Z3 input, and
the honors2puzzlez3output is the code that converts the Z3 output into a solution in
the puzzle format.
\\
\\
\textbf{\large Solution to puzzle2.txt}
\\
\\
4 1 7 3 6 9 8 2 5
\\
6 3 2 1 5 8 9 4 7
\\
9 5 8 7 2 4 3 1 6
\\
8 2 5 4 3 7 1 6 9
\\
7 9 1 5 8 6 4 3 2
\\
3 4 6 9 1 2 7 5 8
\\
2 8 9 6 4 3 5 7 1
\\
5 7 3 2 9 1 6 8 4
\\
1 6 4 8 7 5 2 9 3
\\
\\
\textbf{\large 2 More Puzzles}
\\
Puzzles from http://www.tbnweekly.com/just4fun/sudoku/
\\
\\
First Puzzle:
\\
7 2 . 5 . . . . .
\\
. 4 1 . . . . . .
\\
9 . 6 7 1 . . . 5
\\
4 . 5 9 . . 6 . 2
\\
. . . . . . . . .
\\
3 . 8 . . 4 1 . 9
\\
1 . . . 8 7 9 . 4
\\
. . . . . . 5 1 .
\\
. . . . . 9 . 2 8
\\
\\
\\
First Puzzle's Z3 Input:
\\
(declare-const box1 Int)
(declare-const box2 Int)
(declare-const box3 Int)
(declare-const box4 Int)
(declare-const box5 Int)
(declare-const box6 Int)
(declare-const box7 Int)
(declare-const box8 Int)
(declare-const box9 Int)
(declare-const box10 Int)
(declare-const box11 Int)
(declare-const box12 Int)
(declare-const box13 Int)
(declare-const box14 Int)
(declare-const box15 Int)
(declare-const box16 Int)
(declare-const box17 Int)
(declare-const box18 Int)
(declare-const box19 Int)
(declare-const box20 Int)
(declare-const box21 Int)
(declare-const box22 Int)
(declare-const box23 Int)
(declare-const box24 Int)
(declare-const box25 Int)
(declare-const box26 Int)
(declare-const box27 Int)
(declare-const box28 Int)
(declare-const box29 Int)
(declare-const box30 Int)
(declare-const box31 Int)
(declare-const box32 Int)
(declare-const box33 Int)
(declare-const box34 Int)
(declare-const box35 Int)
(declare-const box36 Int)
(declare-const box37 Int)
(declare-const box38 Int)
(declare-const box39 Int)
(declare-const box40 Int)
(declare-const box41 Int)
(declare-const box42 Int)
(declare-const box43 Int)
(declare-const box44 Int)
(declare-const box45 Int)
(declare-const box46 Int)
(declare-const box47 Int)
(declare-const box48 Int)
(declare-const box49 Int)
(declare-const box50 Int)
(declare-const box51 Int)
(declare-const box52 Int)
(declare-const box53 Int)
(declare-const box54 Int)
(declare-const box55 Int)
(declare-const box56 Int)
(declare-const box57 Int)
(declare-const box58 Int)
(declare-const box59 Int)
(declare-const box60 Int)
(declare-const box61 Int)
(declare-const box62 Int)
(declare-const box63 Int)
(declare-const box64 Int)
(declare-const box65 Int)
(declare-const box66 Int)
(declare-const box67 Int)
(declare-const box68 Int)
(declare-const box69 Int)
(declare-const box70 Int)
(declare-const box71 Int)
(declare-const box72 Int)
(declare-const box73 Int)
(declare-const box74 Int)
(declare-const box75 Int)
(declare-const box76 Int)
(declare-const box77 Int)
(declare-const box78 Int)
(declare-const box79 Int)
(declare-const box80 Int)
(declare-const box81 Int)
(assert (distinct 7 2 box3 5 box5 box6 box7 box8 box9))
(assert (distinct box10 4 1 box13 box14 box15 box16 box17 box18))
(assert (distinct 9 box20 6 7 1 box24 box25 box26 5))
(assert (distinct 4 box29 5 9 box32 box33 6 box35 2))
(assert (distinct box37 box38 box39 box40 box41 box42 box43 box44 box45))
(assert (distinct 3 box47 8 box49 box50 4 1 box53 9))
(assert (distinct 1 box56 box57 box58 8 7 9 box62 4))
(assert (distinct box64 box65 box66 box67 box68 box69 5 1 box72))
(assert (distinct box73 box74 box75 box76 box77 9 box79 2 8))
(assert (distinct 7 box10 9 4 box37 3 1 box64 box73))
(assert (distinct 2 4 box20 box29 box38 box47 box56 box65 box74))
(assert (distinct box3 1 6 5 box39 8 box57 box66 box75))
(assert (distinct 5 box13 7 9 box40 box49 box58 box67 box76))
(assert (distinct box5 box14 1 box32 box41 box50 8 box68 box77))
(assert (distinct box6 box15 box24 box33 box42 4 7 box69 9))
(assert (distinct box7 box16 box25 6 box43 1 9 5 box79))
(assert (distinct box8 box17 box26 box35 box44 box53 box62 1 2))
(assert (distinct box9 box18 5 2 box45 9 4 box72 8))
(assert (distinct 7 2 box3 box10 4 1 9 box20 6))
(assert (distinct 5 box5 box6 box13 box14 box15 7 1 box24))
(assert (distinct box7 box8 box9 box16 box17 box18 box25 box26 5))
(assert (distinct 4 box29 5 box37 box38 box39 3 box47 8))
(assert (distinct 9 box32 box33 box40 box41 box42 box49 box50 4))
(assert (distinct 6 box35 2 box43 box44 box45 1 box53 9))
(assert (distinct 1 box56 box57 box64 box65 box66 box73 box74 box75))
(assert (distinct box58 8 7 box67 box68 box69 box76 box77 9))
(assert (distinct 9 box62 4 5 1 box72 box79 2 8))
(assert (and($>$ box1 0) ($<$ box1 10)))
(assert (and($>$ box2 0) ($<$ box2 10)))
(assert (and($>$ box3 0) ($<$ box3 10)))
(assert (and($>$ box4 0) ($<$ box4 10)))
(assert (and($>$ box5 0) ($<$ box5 10)))
(assert (and($>$ box6 0) ($<$ box6 10)))
(assert (and($>$ box7 0) ($<$ box7 10)))
(assert (and($>$ box8 0) ($<$ box8 10)))
(assert (and($>$ box9 0) ($<$ box9 10)))
(assert (and($>$ box10 0) ($<$ box10 10)))
(assert (and($>$ box11 0) ($<$ box11 10)))
(assert (and($>$ box12 0) ($<$ box12 10)))
(assert (and($>$ box13 0) ($<$ box13 10)))
(assert (and($>$ box14 0) ($<$ box14 10)))
(assert (and($>$ box15 0) ($<$ box15 10)))
(assert (and($>$ box16 0) ($<$ box16 10)))
(assert (and($>$ box17 0) ($<$ box17 10)))
(assert (and($>$ box18 0) ($<$ box18 10)))
(assert (and($>$ box19 0) ($<$ box19 10)))
(assert (and($>$ box20 0) ($<$ box20 10)))
(assert (and($>$ box21 0) ($<$ box21 10)))
(assert (and($>$ box22 0) ($<$ box22 10)))
(assert (and($>$ box23 0) ($<$ box23 10)))
(assert (and($>$ box24 0) ($<$ box24 10)))
(assert (and($>$ box25 0) ($<$ box25 10)))
(assert (and($>$ box26 0) ($<$ box26 10)))
(assert (and($>$ box27 0) ($<$ box27 10)))
(assert (and($>$ box28 0) ($<$ box28 10)))
(assert (and($>$ box29 0) ($<$ box29 10)))
(assert (and($>$ box30 0) ($<$ box30 10)))
(assert (and($>$ box31 0) ($<$ box31 10)))
(assert (and($>$ box32 0) ($<$ box32 10)))
(assert (and($>$ box33 0) ($<$ box33 10)))
(assert (and($>$ box34 0) ($<$ box34 10)))
(assert (and($>$ box35 0) ($<$ box35 10)))
(assert (and($>$ box36 0) ($<$ box36 10)))
(assert (and($>$ box37 0) ($<$ box37 10)))
(assert (and($>$ box38 0) ($<$ box38 10)))
(assert (and($>$ box39 0) ($<$ box39 10)))
(assert (and($>$ box40 0) ($<$ box40 10)))
(assert (and($>$ box41 0) ($<$ box41 10)))
(assert (and($>$ box42 0) ($<$ box42 10)))
(assert (and($>$ box43 0) ($<$ box43 10)))
(assert (and($>$ box44 0) ($<$ box44 10)))
(assert (and($>$ box45 0) ($<$ box45 10)))
(assert (and($>$ box46 0) ($<$ box46 10)))
(assert (and($>$ box47 0) ($<$ box47 10)))
(assert (and($>$ box48 0) ($<$ box48 10)))
(assert (and($>$ box49 0) ($<$ box49 10)))
(assert (and($>$ box50 0) ($<$ box50 10)))
(assert (and($>$ box51 0) ($<$ box51 10)))
(assert (and($>$ box52 0) ($<$ box52 10)))
(assert (and($>$ box53 0) ($<$ box53 10)))
(assert (and($>$ box54 0) ($<$ box54 10)))
(assert (and($>$ box55 0) ($<$ box55 10)))
(assert (and($>$ box56 0) ($<$ box56 10)))
(assert (and($>$ box57 0) ($<$ box57 10)))
(assert (and($>$ box58 0) ($<$ box58 10)))
(assert (and($>$ box59 0) ($<$ box59 10)))
(assert (and($>$ box60 0) ($<$ box60 10)))
(assert (and($>$ box61 0) ($<$ box61 10)))
(assert (and($>$ box62 0) ($<$ box62 10)))
(assert (and($>$ box63 0) ($<$ box63 10)))
(assert (and($>$ box64 0) ($<$ box64 10)))
(assert (and($>$ box65 0) ($<$ box65 10)))
(assert (and($>$ box66 0) ($<$ box66 10)))
(assert (and($>$ box67 0) ($<$ box67 10)))
(assert (and($>$ box68 0) ($<$ box68 10)))
(assert (and($>$ box69 0) ($<$ box69 10)))
(assert (and($>$ box70 0) ($<$ box70 10)))
(assert (and($>$ box71 0) ($<$ box71 10)))
(assert (and($>$ box72 0) ($<$ box72 10)))
(assert (and($>$ box73 0) ($<$ box73 10)))
(assert (and($>$ box74 0) ($<$ box74 10)))
(assert (and($>$ box75 0) ($<$ box75 10)))
(assert (and($>$ box76 0) ($<$ box76 10)))
(assert (and($>$ box77 0) ($<$ box77 10)))
(assert (and($>$ box78 0) ($<$ box78 10)))
(assert (and($>$ box79 0) ($<$ box79 10)))
(assert (and($>$ box80 0) ($<$ box80 10)))
(assert (and($>$ box81 0) ($<$ box81 10)))
(check-sat)
(get-model)
\\
\\
\\
First Puzzle's Z3 Output:
\\
\\
sat
(model
(define-fun box42 () Int
5)
(define-fun box54 () Int
1)
(define-fun box78 () Int
1)
(define-fun box28 () Int
1)
(define-fun box81 () Int
1)
(define-fun box79 () Int
7)
(define-fun box60 () Int
1)
(define-fun box38 () Int
1)
(define-fun box37 () Int
2)
(define-fun box53 () Int
5)
(define-fun box72 () Int
6)
(define-fun box59 () Int
1)
(define-fun box9 () Int
1)
(define-fun box1 () Int
1)
(define-fun box18 () Int
7)
(define-fun box15 () Int
8)
(define-fun box57 () Int
2)
(define-fun box49 () Int
2)
(define-fun box56 () Int
5)
(define-fun box64 () Int
8)
(define-fun box69 () Int
3)
(define-fun box31 () Int
1)
(define-fun box25 () Int
3)
(define-fun box48 () Int
1)
(define-fun box32 () Int
3)
(define-fun box2 () Int
1)
(define-fun box33 () Int
1)
(define-fun box5 () Int
4)
(define-fun box26 () Int
4)
(define-fun box58 () Int
6)
(define-fun box36 () Int
1)
(define-fun box20 () Int
8)
(define-fun box61 () Int
1)
(define-fun box13 () Int
3)
(define-fun box4 () Int
1)
(define-fun box43 () Int
4)
(define-fun box74 () Int
3)
(define-fun box68 () Int
2)
(define-fun box24 () Int
2)
(define-fun box39 () Int
9)
(define-fun box45 () Int
3)
(define-fun box7 () Int
8)
(define-fun box65 () Int
9)
(define-fun box22 () Int
1)
(define-fun box52 () Int
1)
(define-fun box70 () Int
1)
(define-fun box71 () Int
1)
(define-fun box50 () Int
7)
(define-fun box80 () Int
1)
(define-fun box77 () Int
5)
(define-fun box3 () Int
3)
(define-fun box51 () Int
1)
(define-fun box8 () Int
9)
(define-fun box30 () Int
1)
(define-fun box41 () Int
6)
(define-fun box34 () Int
1)
(define-fun box67 () Int
4)
(define-fun box47 () Int
6)
(define-fun box21 () Int
1)
(define-fun box44 () Int
7)
(define-fun box11 () Int
1)
(define-fun box17 () Int
6)
(define-fun box35 () Int
8)
(define-fun box23 () Int
1)
(define-fun box66 () Int
7)
(define-fun box27 () Int
1)
(define-fun box55 () Int
1)
(define-fun box62 () Int
3)
(define-fun box75 () Int
4)
(define-fun box16 () Int
2)
(define-fun box40 () Int
8)
(define-fun box6 () Int
6)
(define-fun box10 () Int
5)
(define-fun box73 () Int
6)
(define-fun box12 () Int
1)
(define-fun box19 () Int
1)
(define-fun box46 () Int
1)
(define-fun box63 () Int
1)
(define-fun box14 () Int
9)
(define-fun box29 () Int
7)
(define-fun box76 () Int
1)
)
\\
\\
\\
So the first puzzle's solution turns out to be:
\\
\\
7 2 3 5 4 6 8 9 1
\\
5 4 1 3 9 8 2 6 7
\\
9 8 6 7 1 2 3 4 5
\\
4 7 5 9 3 1 6 8 2
\\
2 1 9 8 6 5 4 7 3
\\
3 6 8 2 7 4 1 5 9
\\
1 5 2 6 8 7 9 3 4
\\
8 9 7 4 2 3 5 1 6
\\
6 3 4 1 5 9 7 2 8
\\
\\
Second Puzzle:
\\
. . . . 1 4 . . 8
\\
. . 9 . . . . . .
\\
. 4 . 6 . 3 2 . .
\\
. 7 8 4 3 . 1 . .
\\
. 5 3 . . . 4 7 .
\\
. . 2 . 5 7 3 8 .
\\
. . 4 1 . 5 . 2 .
\\
. . . . . . 5 . .
\\
3 . . 7 8 . . . .
\\
\\
\pagebreak
\\
Second Puzzle's Solution:
\\
5 3 6 2 1 4 7 9 8
\\
1 2 9 5 7 8 6 3 4
\\
8 4 7 6 9 3 2 1 5
\\
9 7 8 4 3 6 1 5 2
\\
6 5 3 8 2 1 4 7 9
\\
4 1 2 9 5 7 3 8 6
\\
7 9 4 1 6 5 8 2 3
\\
2 8 1 3 4 9 5 6 7
\\
3 6 5 7 8 2 9 4 1






\end{document}

Вам также может понравиться