From dadc6cc6231fae4edfc305ff205c0580c8caebec Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Sun, 13 Jun 2021 13:20:47 +0200 Subject: [PATCH] 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> --- .../project-euler/problem-96-su-doku.md | 129 +++++++++++++++--- 1 file changed, 109 insertions(+), 20 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-96-su-doku.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-96-su-doku.md index 22442b67c2..10509108d2 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-96-su-doku.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-96-su-doku.md @@ -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. -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-- -`suDoku(testPuzzles)` should return a number. +`suDoku(testPuzzles1)` should return a number. ```js -assert(typeof suDoku(testPuzzles) === 'number'); +assert(typeof suDoku(_testPuzzles1) === 'number'); ``` -`suDoku(testPuzzles)` should return 1190. +`suDoku(testPuzzles1)` should return `1190`. ```js -assert.strictEqual(suDoku(testPuzzles), 1190); +assert.strictEqual(suDoku(_testPuzzles1), 1190); ``` -`suDoku(puzzlesArr)` should return 24702. +`suDoku(testPuzzles2)` should return `24702`. ```js -assert.strictEqual(suDoku(puzzlesArr), 24702); +assert.strictEqual(suDoku(_testPuzzles2), 24702); ``` # --seed-- -## --seed-contents-- +## --after-user-code-- ```js -function suDoku(arr) { - - return true; -} - -// Only change code above this line - -const testPuzzles = [ +const _testPuzzles1 = [ '003020600900305001001806400008102900700000008006708200002609500800203009005010300', '200080300060070084030500209000105408000000000402706000301007040720040060004010003', '000000907000420180000705026100904000050000040000507009920108000034059000507000000' ]; -const puzzlesArr = [ +const _testPuzzles2 = [ '003020600900305001001806400008102900700000008006708200002609500800203009005010300', '200080300060070084030500209000105408000000000402706000301007040720040060004010003', '000000907000420180000705026100904000050000040000507009920108000034059000507000000', @@ -196,12 +189,108 @@ const puzzlesArr = [ '000003017015009008060000000100007000009000200000500004000000020500600340340200000', '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-- ```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; +} ```