Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell: Get block for Sudoku solver
    text
    copied!<p>I am trying to build a Sudoku solver for a project I am working on. I have a 9x9 grid with each position numbered 0..80, left to right, top to bottom</p> <p>e.g.:</p> <p>0 1 2 3 4 5 6 7 8</p> <p>9 10 ...</p> <p>I am trying to return a list of Int's that represent the 3x3 grid that a position falls in. For example for position 1, which is in grid (0,0) it would return [0,1,2,9,10,11,18,19,20] and for position 8, which is in grid (0,2) it would return [6,7,8,15,16,17,24,25,26].</p> <p>I have written a function which returns the required 3x3 grid, on a 9x9 Sudoku:</p> <pre><code>getBlock :: Int -&gt; Int -&gt; [[Int]] getBlock x y = [[((x * sudokuSizeSq + 0) * sudokuSize) + (y * sudokuSizeSq + 0),((x * sudokuSizeSq + 0) * sudokuSize) + (y * sudokuSizeSq + 1)..((x * sudokuSizeSq + 0) * sudokuSize) + (y * sudokuSizeSq + (sudokuSizeSq-1))],[((x * sudokuSizeSq + 1) * sudokuSize) + (y * sudokuSizeSq + 0),((x * sudokuSizeSq + 1) * sudokuSize) + (y * sudokuSizeSq + 1)..((x * sudokuSizeSq + 1) * sudokuSize) + (y * sudokuSizeSq + (sudokuSizeSq-1))],[((x * sudokuSizeSq + 2) * sudokuSize) + (y * sudokuSizeSq + 0),((x * sudokuSizeSq + 2) * sudokuSize) + (y * sudokuSizeSq + 1)..((x * sudokuSizeSq + 2) * sudokuSize) + (y * sudokuSizeSq + (sudokuSizeSq-1))]] </code></pre> <p>Where: sudokuSizeSq is the square of the width (3) sudokuSize is the width (9)</p> <p>x and y represent the possible grids.</p> <p>This works, however I would like to increase it so it will work for larger grids. I changed my code to the follow but it seems then wont load the function.</p> <pre><code>getBlock :: Int -&gt; Int -&gt; [[Int]] getBlock x y = [[((x * sudokuSizeSq + 0) * sudokuSize) + (y * sudokuSizeSq + 0),((x * sudokuSizeSq + 0) * sudokuSize) + (y * sudokuSizeSq + 1)..((x * sudokuSizeSq + 0) * sudokuSize) + (y * sudokuSizeSq + (sudokuSizeSq-1))],[((x * sudokuSizeSq + 1) * sudokuSize) + (y * sudokuSizeSq + 0),((x * sudokuSizeSq + 1) * sudokuSize) + (y * sudokuSizeSq + 1)..((x * sudokuSizeSq + 1) * sudokuSize) + (y * sudokuSizeSq + (sudokuSizeSq-1))]..[((x * sudokuSizeSq + (sudokuSizeSq-1)) * sudokuSize) + (y * sudokuSizeSq + 0),((x * sudokuSizeSq + (sudokuSizeSq-1)) * sudokuSize) + (y * sudokuSizeSq + 1)..((x * sudokuSizeSq + (sudokuSizeSq-1)) * sudokuSize) + (y * sudokuSizeSq + (sudokuSizeSq-1))]] </code></pre> <p>The way I'm doing it seems very cumbersome, is there a better way to do this or a way to fix the problem I'm having. Thanks. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload