fix(curriculum): rework Project Euler 96 (#42376)

* fix: use in tests arrays defined in background

* feat: add solution

* fix: improve wording

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
This commit is contained in:
gikf
2021-06-13 13:20:47 +02:00
committed by GitHub
parent ce3ccc47d4
commit dadc6cc623

View File

@ -102,49 +102,42 @@ Su Doku (Japanese meaning *number place*) is the name given to a popular puzzle
A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction. A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.
The `puzzlesArr` array contains fifty different Su Doku puzzle strings ranging in difficulty, but all with unique solutions (the first puzzle in the array is the example in the challenge description). The `puzzlesArr` array contains different Su Doku puzzle strings ranging in difficulty, but all with unique solutions.
By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above. By solving all puzzles in `puzzlesArr`, find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.
# --hints-- # --hints--
`suDoku(testPuzzles)` should return a number. `suDoku(testPuzzles1)` should return a number.
```js ```js
assert(typeof suDoku(testPuzzles) === 'number'); assert(typeof suDoku(_testPuzzles1) === 'number');
``` ```
`suDoku(testPuzzles)` should return 1190. `suDoku(testPuzzles1)` should return `1190`.
```js ```js
assert.strictEqual(suDoku(testPuzzles), 1190); assert.strictEqual(suDoku(_testPuzzles1), 1190);
``` ```
`suDoku(puzzlesArr)` should return 24702. `suDoku(testPuzzles2)` should return `24702`.
```js ```js
assert.strictEqual(suDoku(puzzlesArr), 24702); assert.strictEqual(suDoku(_testPuzzles2), 24702);
``` ```
# --seed-- # --seed--
## --seed-contents-- ## --after-user-code--
```js ```js
function suDoku(arr) { const _testPuzzles1 = [
return true;
}
// Only change code above this line
const testPuzzles = [
'003020600900305001001806400008102900700000008006708200002609500800203009005010300', '003020600900305001001806400008102900700000008006708200002609500800203009005010300',
'200080300060070084030500209000105408000000000402706000301007040720040060004010003', '200080300060070084030500209000105408000000000402706000301007040720040060004010003',
'000000907000420180000705026100904000050000040000507009920108000034059000507000000' '000000907000420180000705026100904000050000040000507009920108000034059000507000000'
]; ];
const puzzlesArr = [ const _testPuzzles2 = [
'003020600900305001001806400008102900700000008006708200002609500800203009005010300', '003020600900305001001806400008102900700000008006708200002609500800203009005010300',
'200080300060070084030500209000105408000000000402706000301007040720040060004010003', '200080300060070084030500209000105408000000000402706000301007040720040060004010003',
'000000907000420180000705026100904000050000040000507009920108000034059000507000000', '000000907000420180000705026100904000050000040000507009920108000034059000507000000',
@ -196,12 +189,108 @@ const puzzlesArr = [
'000003017015009008060000000100007000009000200000500004000000020500600340340200000', '000003017015009008060000000100007000009000200000500004000000020500600340340200000',
'300200000000107000706030500070009080900020004010800050009040301000702000000008006' '300200000000107000706030500070009080900020004010800050009040301000702000000008006'
]; ];
```
suDoku(testPuzzles); ## --seed-contents--
```js
function suDoku(puzzlesArr) {
return true;
}
// Only change code above this line
const testPuzzles1 = [
'003020600900305001001806400008102900700000008006708200002609500800203009005010300',
'200080300060070084030500209000105408000000000402706000301007040720040060004010003',
'000000907000420180000705026100904000050000040000507009920108000034059000507000000'
];
suDoku(testPuzzles1);
``` ```
# --solutions-- # --solutions--
```js ```js
// solution required function suDoku(puzzlesArr) {
function solve(puzzle) {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (puzzle[row][col] > 0) {
continue;
}
const allowedNumbers = getAllowedNumbers(puzzle, row, col);
for (let number = 1; number <= 9; number++) {
if (allowedNumbers[number]) {
puzzle[row][col] = number;
if (solve(puzzle)) {
return true;
}
}
}
puzzle[row][col] = 0;
return false;
}
}
return true;
}
function getAllowedNumbers(puzzle, row, col) {
const allowed = new Array(10).fill(true);
allowed[0] = false;
for (let i = 0; i < 9; i++) {
const numberInSameRow = puzzle[row][i];
if (numberInSameRow > 0) {
allowed[numberInSameRow] = false;
}
const numberInSameCol = puzzle[i][col];
if (numberInSameCol > 0) {
allowed[numberInSameCol] = false;
}
}
const rowOfSubGrid = Math.floor(row / 3) * 3;
const colOfSubGrid = Math.floor(col / 3) * 3;
for (let rowInSubGrid = 0; rowInSubGrid < 3; rowInSubGrid++) {
for (let colInSubGrid = 0; colInSubGrid < 3; colInSubGrid++) {
const numberInSameSubGrid =
puzzle[rowOfSubGrid + rowInSubGrid][colOfSubGrid + colInSubGrid];
if (numberInSameSubGrid > 0) {
allowed[numberInSameSubGrid] = false;
}
}
}
return allowed;
}
function parsePuzzle(string) {
const puzzle = [];
for (let row = 0; row < 9; row++) {
puzzle.push(
string
.slice(row * 9, 9 + row * 9)
.split('')
.map(x => parseInt(x, 10))
);
}
return puzzle;
}
let sum = 0;
for (let i = 0; i < puzzlesArr.length; i++) {
const puzzle = parsePuzzle(puzzlesArr[i]);
if (solve(puzzle)) {
sum += 100 * puzzle[0][0] + 10 * puzzle[0][1] + puzzle[0][2];
}
}
return sum;
}
``` ```