Mini Sudoku (Code Challenge)

  • Page Owner: Not Set
  • Last Reviewed: 2019-09-20

Mini Sudoku

A Sudoku is a 9x9 grid that is completed when every 3x3 square, row and column consist of the numbers 1-9.

For this task, you will be given a completed 3x3 square, in the form of a two-dimensional array. Create a function that checks to make sure this 3x3 square contains each number from 1-9 exactly once. Make sure there are no duplicates, and no numbers outside this range.

Examples

isMiniSudoku([[1, 3, 2], [9, 7, 8], [4, 5, 6]]) ➞ true

isMiniSudoku([[1, 1, 3], [6, 5, 4], [8, 7, 9]]) ➞ false
// The 1 is repeated twice 

isMiniSudoku([[0, 1, 2], [6, 4, 5], [9, 8, 7]]) ➞ false
// The 0 is included (outside range)

isMiniSudoku([[8, 9, 2], [5, 6, 1], [3, 7, 4]]) ➞ true 

Notes

N/A

Tests

isMiniSudoku(
[[1, 3, 2], 
[9, 7, 8], 
[4, 5, 6]]) ==  true
isMiniSudoku(
[[1, 1, 3], 
[6, 5, 4], 
[8, 7, 9]]) == false // '1 is included twice.'
isMiniSudoku(
[[0, 1, 2], 
[6, 4, 5], 
[9, 8, 7]]) == false // '0 is not in range 1-9.'
isMiniSudoku(
[[8, 9, 2], 
[5, 6, 1], 
[3, 7, 4]]) ==  true);
isMiniSudoku(
[[2, 3, 4], 
[6, 7, 7], 
[8, 9, 1]]) == false // '7 is included twice.'
isMiniSudoku(
[[6, 5, 9], 
[4, 3, 8], 
[2, 1, 7]]) == true;
isMiniSudoku(
[[4, 3, 5], 
[8, 1, 2], 
[9, 6, 7]]) == true;
isMiniSudoku(
[[4, 3, 5], 
[8, 6, 2], 
[9, 6, 7]]) == false // '6 is included twice.'

Additional Posts

Not exactly code-golfed, but nice and compact and impossible to read. Using bits to do the heavy lifting.

let isMiniSudoku = (arrays) => arrays.reduce((acc, row) => row.reduce((acc2, number) => acc2 | (1 << number), acc), 0) == 1022;

This was pretty easy once Bob pointed out I don't know how to follow directions. I've got 86 characters in mine. It might be shorter than Bob's but it doesn't have cool bitwise operators.

isMiniSudoku = x => x.flat().reduce((a, c) => a.add(c % 10 || 1), new Set()).size == 9