23
Young Won Lim 12/23/16 A Sudoku Solver – Rules (2A) Richard Bird Implementation

A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Young Won Lim12/23/16

A Sudoku Solver – Rules (2A)

● Richard Bird Implementation

Page 2: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Young Won Lim12/23/16

Copyright (c) 2016 Young W. Lim.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

Please send corrections (or suggestions) to [email protected].

This document was produced by using OpenOffice.

Page 3: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 3 Young Won Lim

12/23/16

Based on

Thinking Functionally with Haskell, R. Bird

https://wiki.haskell.org/Sudoku

http://cdsoft.fr/haskell/sudoku.html

https://gist.github.com/wvandyk/3638996

http://www.cse.chalmers.se/edu/year/2015/course/TDA555/lab3.html

Page 4: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 4 Young Won Lim

12/23/16

Lawful laws (1)

rows . rows = idcols . cols = idboxs . boxs = id

ungroup. group = idgroup . ungroup = id

map ungroup . ungroup . map cols . group . ungroup . map cols . group . map group

map ungroup . ungroup . map cols .map cols . group . map group

map ungroup . ungroup . group . map group

Page 5: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 5 Young Won Lim

12/23/16

Lawful laws (2)

map rows . expand = expand . rowsmap cols . expand = expand . colsmap boxs . expand = expand . boxes

map (map f) . cp = cp . map (map f)filter (all p) . cp = cp . map (filter p)

Page 6: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 6 Young Won Lim

12/23/16

Lawful laws (1)

rows . rows = idcols . cols = idboxs . boxs = id

rows :: Matrix a -> [Row a]rows = id

cols :: Matrix a -> [Row a]cols [xs] = [[x] | x <- xs]cols (xs:xss) = zipWith (:) xs (cols xss)

boxs :: Matrix a -> [Row a]boxs = map ungroup . ungroup .

map cols . group . map group

Page 7: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 7 Young Won Lim

12/23/16

rows.rows, cols.cols, boxs.boxs

a b c d

e f g h

i j k l

m n o p

a b e f

c d g h

i j m n

k l o p

boxs

colsa b c d

e f g h

i j k l

m n o p

a e i m

b f j n

c g k o

d h l p

a b c d

e f g h

i j k l

m n o p

a b c d

e f g h

i j k l

m n o p

rows

boxs

cols

a b c d

e f g h

i j k l

m n o p

rows

a b c d

e f g h

i j k l

m n o p

a b c d

e f g h

i j k l

m n o p

Page 8: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 8 Young Won Lim

12/23/16

group and ungroup

ungroup. group = idgroup . ungroup = id

Page 9: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 9 Young Won Lim

12/23/16

group . ungroup

[ [ [ ‘5’, ‘3’, ‘4’, ‘6’, ‘7’, ‘8’, ‘9’, ‘1’, ‘2’ ],

[ ‘6’, ‘7’, ‘2’, ‘1’, ‘9’, ‘5’, ‘3’, ‘4’, ‘8’ ],

[ ‘1’, ‘9’, ‘8’, ‘3’, ‘4’, ‘2’, ‘5’, ‘6’, ‘7’ ] ],

[ [ ‘8’, ‘5’, ‘9’, ‘7’, ‘6’, ‘1’, ‘4’, ‘2’, ‘3’ ],

[ ‘4’, ‘2’, ‘6’, ‘8’, ‘5’, ‘3’, ‘7’, ‘9’, ‘1’ ],

[ ‘7’, ‘1’, ‘3’, ‘9’, ‘2’, ‘4’, ‘8’, ‘5’, ‘6’ ] ],

[ [ ‘9’, ‘6’, ‘1’, ‘5’, ‘3’, ‘7’, ‘2’, ‘8’, ‘4’ ],

[ ‘2’, ‘8’, ‘7’, ‘4’, ‘1’, ‘9’, ‘6’, ‘3’, ‘5’ ],

[ ‘3’, ‘4’, ‘5’, ‘2’, ‘8’, ‘6’, ‘1’, ‘7’, ‘9’ ] ] ]

[ [ ‘5’, ‘3’, ‘4’, ‘6’, ‘7’, ‘8’, ‘9’, ‘1’, ‘2’ ],

[ ‘6’, ‘7’, ‘2’, ‘1’, ‘9’, ‘5’, ‘3’, ‘4’, ‘8’ ],

[ ‘1’, ‘9’, ‘8’, ‘3’, ‘4’, ‘2’, ‘5’, ‘6’, ‘7’ ],

[ ‘8’, ‘5’, ‘9’, ‘7’, ‘6’, ‘1’, ‘4’, ‘2’, ‘3’ ],

[ ‘4’, ‘2’, ‘6’, ‘8’, ‘5’, ‘3’, ‘7’, ‘9’, ‘1’ ],

[ ‘7’, ‘1’, ‘3’, ‘9’, ‘2’, ‘4’, ‘8’, ‘5’, ‘6’ ],

[ ‘9’, ‘6’, ‘1’, ‘5’, ‘3’, ‘7’, ‘2’, ‘8’, ‘4’ ],

[ ‘2’, ‘8’, ‘7’, ‘4’, ‘1’, ‘9’, ‘6’, ‘3’, ‘5’ ],

[ ‘3’, ‘4’, ‘5’, ‘2’, ‘8’, ‘6’, ‘1’, ‘7’, ‘9’ ] ]

ungroup

group

Page 10: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 10 Young Won Lim

12/23/16

map ungroup . map group

[ [ ‘5’, ‘3’, ‘4’, ‘6’, ‘7’, ‘8’, ‘9’, ‘1’, ‘2’ ],

[ ‘6’, ‘7’, ‘2’, ‘1’, ‘9’, ‘5’, ‘3’, ‘4’, ‘8’ ],

[ ‘1’, ‘9’, ‘8’, ‘3’, ‘4’, ‘2’, ‘5’, ‘6’, ‘7’ ],

[ ‘8’, ‘5’, ‘9’, ‘7’, ‘6’, ‘1’, ‘4’, ‘2’, ‘3’ ],

[ ‘4’, ‘2’, ‘6’, ‘8’, ‘5’, ‘3’, ‘7’, ‘9’, ‘1’ ],

[ ‘7’, ‘1’, ‘3’, ‘9’, ‘2’, ‘4’, ‘8’, ‘5’, ‘6’ ],

[ ‘9’, ‘6’, ‘1’, ‘5’, ‘3’, ‘7’, ‘2’, ‘8’, ‘4’ ],

[ ‘2’, ‘8’, ‘7’, ‘4’, ‘1’, ‘9’, ‘6’, ‘3’, ‘5’ ],

[ ‘3’, ‘4’, ‘5’, ‘2’, ‘8’, ‘6’, ‘1’, ‘7’, ‘9’ ] ]

[ [ [‘5’, ‘3’, ‘4]’, [‘6’, ‘7’, ‘8’], [‘9’, ‘1’, ‘2’] ],

[ [‘6’, ‘7’, ‘2’], [‘1’, ‘9’, ‘5’], [‘3’, ‘4’, ‘8’] ],

[ [‘1’, ‘9’, ‘8’], [‘3’, ‘4’, ‘2’], [‘5’, ‘6’, ‘7’] ],

[ [‘8’, ‘5’, ‘9’], [‘7’, ‘6’, ‘1’], [‘4’, ‘2’, ‘3’] ],

[ [‘4’, ‘2’, ‘6’], [‘8’, ‘5’, ‘3’], [‘7’, ‘9’, ‘1’] ],

[ [‘7’, ‘1’, ‘3’], [‘9’, ‘2’, ‘4’], [‘8’, ‘5’, ‘6’] ],

[ [‘9’, ‘6’, ‘1’], [‘5’, ‘3’, ‘7’], [‘2’, ‘8’, ‘4’] ],

[ [‘2’, ‘8’, ‘7’], [‘4’, ‘1’, ‘9’], [‘6’, ‘3’, ‘5’] ],

[ [‘3’, ‘4’, ‘5’], [‘2’, ‘8’, ‘6’], [‘1’, ‘7’, ‘9’] ] ]

map group

map ungroup

Page 11: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 11 Young Won Lim

12/23/16

map group and map ungroup

map ungroup . ungroup . group . map group

map ungroup . ungroup . group . map group

Page 12: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 12 Young Won Lim

12/23/16

map ungroup . ungroup . group . map group

[ [ ‘5’, ‘3’, ‘4’, ‘6’, ‘7’, ‘8’, ‘9’, ‘1’, ‘2’ ],

[ ‘6’, ‘7’, ‘2’, ‘1’, ‘9’, ‘5’, ‘3’, ‘4’, ‘8’ ],

[ ‘1’, ‘9’, ‘8’, ‘3’, ‘4’, ‘2’, ‘5’, ‘6’, ‘7’ ],

[ ‘8’, ‘5’, ‘9’, ‘7’, ‘6’, ‘1’, ‘4’, ‘2’, ‘3’ ],

[ ‘4’, ‘2’, ‘6’, ‘8’, ‘5’, ‘3’, ‘7’, ‘9’, ‘1’ ],

[ ‘7’, ‘1’, ‘3’, ‘9’, ‘2’, ‘4’, ‘8’, ‘5’, ‘6’ ],

[ ‘9’, ‘6’, ‘1’, ‘5’, ‘3’, ‘7’, ‘2’, ‘8’, ‘4’ ],

[ ‘2’, ‘8’, ‘7’, ‘4’, ‘1’, ‘9’, ‘6’, ‘3’, ‘5’ ],

[ ‘3’, ‘4’, ‘5’, ‘2’, ‘8’, ‘6’, ‘1’, ‘7’, ‘9’ ] ]

[ [ [‘5’, ‘3’, ‘4]’, [‘6’, ‘7’, ‘8’], [‘9’, ‘1’, ‘2’] ],

[ [‘6’, ‘7’, ‘2’], [‘1’, ‘9’, ‘5’], [‘3’, ‘4’, ‘8’] ],

[ [‘1’, ‘9’, ‘8’], [‘3’, ‘4’, ‘2’], [‘5’, ‘6’, ‘7’] ],

[ [‘8’, ‘5’, ‘9’], [‘7’, ‘6’, ‘1’], [‘4’, ‘2’, ‘3’] ],

[ [‘4’, ‘2’, ‘6’], [‘8’, ‘5’, ‘3’], [‘7’, ‘9’, ‘1’] ],

[ [‘7’, ‘1’, ‘3’], [‘9’, ‘2’, ‘4’], [‘8’, ‘5’, ‘6’] ],

[ [‘9’, ‘6’, ‘1’], [‘5’, ‘3’, ‘7’], [‘2’, ‘8’, ‘4’] ],

[ [‘2’, ‘8’, ‘7’], [‘4’, ‘1’, ‘9’], [‘6’, ‘3’, ‘5’] ],

[ [‘3’, ‘4’, ‘5’], [‘2’, ‘8’, ‘6’], [‘1’, ‘7’, ‘9’] ] ]

[ [ [ [‘5’, ‘3’, ‘4]’, [‘6’, ‘7’, ‘8’], [‘9’, ‘1’, ‘2’] ],

[ [‘6’, ‘7’, ‘2’], [‘1’, ‘9’, ‘5’], [‘3’, ‘4’, ‘8’] ],

[ [‘1’, ‘9’, ‘8’], [‘3’, ‘4’, ‘2’], [‘5’, ‘6’, ‘7’] ] ],

[ [ [‘8’, ‘5’, ‘9’], [‘7’, ‘6’, ‘1’], [‘4’, ‘2’, ‘3’] ],

[ [‘4’, ‘2’, ‘6’], [‘8’, ‘5’, ‘3’], [‘7’, ‘9’, ‘1’] ],

[ [‘7’, ‘1’, ‘3’], [‘9’, ‘2’, ‘4’], [‘8’, ‘5’, ‘6’] ] ],

[ [ [‘9’, ‘6’, ‘1’], [‘5’, ‘3’, ‘7’], [‘2’, ‘8’, ‘4’] ],

[ [‘2’, ‘8’, ‘7’], [‘4’, ‘1’, ‘9’], [‘6’, ‘3’, ‘5’] ],

[ [‘3’, ‘4’, ‘5’], [‘2’, ‘8’, ‘6’], [‘1’, ‘7’, ‘9’] ] ] ]

map group group

map ungroup ungroup

group . map group

map ungroup . ungroup .

Page 13: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 13 Young Won Lim

12/23/16

boxs.boxs = id

boxs . boxs = id

boxs :: Matrix a -> [Row a]boxs = map ungroup . ungroup .

map cols . group . map group

map ungroup . ungroup . map cols . group . map group .map ungroup . ungroup . map cols . group . map group = id

Page 14: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 14 Young Won Lim

12/23/16

boxs.boxs = id

map ungroup . ungroup . map cols . group . map group .map ungroup . ungroup . map cols . group . map group =

map ungroup . ungroup . map cols . group . ungroup . map cols . group . map group = .map ungroup . ungroup . map cols . map cols . group . map group =

map ungroup . ungroup . group . map group = id

Page 15: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 15 Young Won Lim

12/23/16

Lawful laws (2)

map rows . expand = expand . rowsmap cols . expand = expand . colsmap boxs . expand = expand . boxes

expand :: Matrix Choices -> [Grid]expand = cp . map cp

cp . map cp = [ [[a]] ] -> [ [[a]] ]

Matrix [Digit][Matrix Digit]expand

Matrix Choices[Grid]

Page 16: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 16 Young Won Lim

12/23/16

Lawful laws (2)

map rows . expand = expand . rowsmap cols . expand = expand . colsmap boxs . expand = expand . boxes

Matrix [Digit][Matrix Digit]expand

Matrix Choices[Grid]row

expandmap rowMatrix [Digit][Matrix Digit]

Matrix Choices[Grid]

Page 17: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 17 Young Won Lim

12/23/16

Lawful laws (2)

[ [ [‘1’..’9’], [‘1’..’9’], [‘4’], [‘1’..’9’], [‘1’..’9’], [‘5’], [‘7’], [‘1’..’9’], [‘1’..’9’] ],

[ [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘9’], [‘4’], [‘1’..’9’], [‘1’..’9’] ],

[ [‘3’], [‘6’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘8’] ],

[ [‘7’], [‘2’], [‘4’], [‘1’..’9’], [‘6’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’] ],

[ [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘4’], [‘1’..’9’], [‘2’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’] ],

[ [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘8’], [‘1’..’9’], [‘1’..’9’], [‘9’], [‘3’] ],

[ [‘4’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘5’], [‘6’] ],

[ [‘1’..’9’], [‘1’..’9’], [‘5’], [‘3’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’] ],

[ [‘1’..’9’], [‘1’..’9’], [‘6’], [‘1’], [‘1’..’9’], [‘1’..’9’], [‘9’], [‘1’..’9’], [‘1’..’9’] ] ]

map cp

cp (

map

cp

)

[ cp [ [‘1’..’9’], [‘1’..’9’], [‘4’], [‘1’..’9’], [‘1’..’9’], [‘5’], [‘7’], [‘1’..’9’], [‘1’..’9’] ],

cp [ [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘9’], [‘4’], [‘1’..’9’], [‘1’..’9’] ],

cp [ [‘3’], [‘6’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘8’] ],

cp [ [‘7’], [‘2’], [‘4’], [‘1’..’9’], [‘6’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’] ],

cp [ [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘4’], [‘1’..’9’], [‘2’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’] ],

cp [ [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘8’], [‘1’..’9’], [‘1’..’9’], [‘9’], [‘3’] ],

cp [ [‘4’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘5’], [‘6’] ],

cp [ [‘1’..’9’], [‘1’..’9’], [‘5’], [‘3’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’], [‘1’..’9’] ],

cp [ [‘1’..’9’], [‘1’..’9’], [‘6’], [‘1’], [‘1’..’9’], [‘1’..’9’], [‘9’], [‘1’..’9’], [‘1’..’9’] ] ]

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ ], [ ], [ ],

[ [ ],

[ ],

[ ],

[ ],

[ ],

[ ],

[ ],

[ ],

[ ] ]

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

Page 18: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 18 Young Won Lim

12/23/16

map row . expand

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,map row .

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],

,

row row row

Page 19: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 19 Young Won Lim

12/23/16

Single-Cell Expansion

> prune :: Matrix Choices -> Matrix Choices> prune => pruneBy boxs . pruneBy cols . pruneBy rows> where pruneBy f = f . map pruneRow . f

> pruneRow :: Row Choices -> Row Choices> pruneRow row = map (remove ones) row> where ones = [d | [d] <- row]

Page 20: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 20 Young Won Lim

12/23/16

Single-Cell Expansion

expand1 :: Matrix Choices -> [Matrix Choices]expand1 rows = [rows1 ++ [row1 ++ [c]:row2] ++ rows2 | c <- cs] where (rows1,row:rows2) = break (any smallest) rows (row1,cs:row2) = break smallest row smallest cs = length cs == n n = minimum (counts rows)

counts = filter (/=1) . map length . concat

Page 21: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 21 Young Won Lim

12/23/16

Single-Cell Expansion

> solve2 :: Grid -> [Grid]> solve2 = search . choices

> search :: Matrix Choices -> [Grid]> search cm> |not (safe pm) = []> |complete pm = [map (map head) pm]> |otherwise = (concat . map search . expand1) pm> where pm = prune cm

> complete :: Matrix Choices -> Bool> complete = all (all single)

> single [_] = True> single _ = False

Page 22: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Bird’s Sudoku Rules (2A) 22 Young Won Lim

12/23/16

Single-Cell Expansion

> safe :: Matrix Choices -> Bool> safe cm = all ok (rows cm) &&> all ok (cols cm) &&> all ok (boxs cm)

> ok row = nodups [d | [d] <- row]

Page 23: A Sudoku Solver – Rules (2A)...Dec 23, 2016  · Bird’s Sudoku Rules (2A) 5 Young Won Lim 12/23/16 Lawful laws (2) map rows . expand = expand . rows map cols . expand = expand

Young Won Lim12/23/16

References

[1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf[2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf