Last active
March 19, 2017 14:23
-
-
Save ddsilva/4f191a17582ae91514f113baa64efdf2 to your computer and use it in GitHub Desktop.
Did I finish my sudoku?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isValidList = (list) => { | |
return list.every(Boolean) && | |
list | |
.slice() | |
.sort((a, b) => a-b) | |
.every((item, index, list) => item !== list[index + 1]) | |
} | |
const invertBoard = (board) => { | |
return board.reduce((prev, curr) => { | |
curr.forEach((value, index) => { | |
prev[index] = prev[index] || []; | |
prev[index].push(value); | |
}); | |
return prev; | |
}, []); | |
}; | |
const boardRegions = (board) => { | |
return board.reduce((prev, curr, index) => { | |
const lastItem = prev.slice(-1)[0], | |
regionsSlice = (lastItem && lastItem.length !== 9) ? | |
prev.splice(-3) : | |
[ [], [], [] ]; | |
return prev.concat(regionsSlice.map((item, i) => { | |
const start = i * 3; | |
return item.concat(curr.slice(start, start + 3)) | |
})) | |
}, []); | |
}; | |
const doneOrNot = (board) => { | |
return ( | |
board.every(isValidList) && | |
invertBoard(board).every(isValidList) && | |
boardRegions(board).every(isValidList) | |
) ? 'Finished!' : 'Try again!'; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment