Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Sudoku
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Chaining style Solver == by jinjing It uses [http://github.com/nfjinjing/projectt/tree/master/T/Snippets.hs some snippets] and the [http://github.com/nfjinjing/projectt/tree/master/T/Hack/Dot.hs dot hack] <haskell> import Prelude hiding ((.)) import T.T import List import Data.Maybe import Data.Char import Data.Map(keys, elems) import qualified Data.Map as Map row i = i `div` 9 col i = i `mod` 9 row_list i positions = positions.select(on_i_row) where on_i_row pos = pos.row == i.row col_list i positions = positions.select(on_i_col) where on_i_col pos = pos.col == i.col grid_list i positions = positions.select(on_same_grid i) on_same_grid i j = on_same_row_grid i j && on_same_col_grid i j on_same_row_grid i j = ( i.row.mod.send_to(3) - j.row.mod.send_to(3) ) == i.row - j.row on_same_col_grid i j = ( i.col.mod.send_to(3) - j.col.mod.send_to(3) ) == i.col - j.col board = 0.upto 80 choices = 1.upto 9 related i positions = positions.row_list(i) ++ positions.col_list(i) ++ positions.grid_list(i) values moves positions = positions.mapMaybe (moves.let_receive Map.lookup) possible_moves i moves = let positions = moves.keys in choices \\ positions.related(i).values(moves) sudoku_move moves = let i = moves.next_pos in moves.possible_moves(i).map(Map.insert i).map_send_to(moves) next_pos moves = (board \\ moves.keys) .label_by(choice_size).sort.first.snd where choice_size i = moves.possible_moves(i).length solve solutions 0 = solutions solve solutions n = solve next_solutions (n-1) where next_solutions = solutions.map(sudoku_move).concat parse_input line = line.words.join("") .map(\c -> if '1' <= c && c <= '9' then c else '0') .map(digitToInt).zip([0..]).reject((==0).snd).Map.fromList pretty_output solution = solution.elems.map(show).in_group_of(9) .map(unwords).unlines sudoku line = solve [given] (81 - given.Map.size).first.pretty_output where given = parse_input line </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width