Created
February 18, 2023 17:58
-
-
Save xeoncross/f2c7557cf99b171a1f188126e0f83d6d to your computer and use it in GitHub Desktop.
My first attempt at validating a Sukoku board. (Also my first introduction to Sudoku at all!)
This file contains hidden or 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
// https://leetcode.com/problems/valid-sudoku/submissions/900490403/ | |
func isValidSudoku(board [][]byte) bool { | |
// 9 spaces is > 8 bits so we use 2 bytes (an int16) | |
rowsMap := [9]int16{} | |
colsMap := [9]int16{} | |
gridMap := [9]int16{} | |
for row:=0; row<9; row++ { | |
for col:=0; col<9; col++ { | |
if board[row][col] == 46 { | |
continue | |
} | |
// ASCII 48 == 0 | |
val := board[row][col] - 48 | |
gridIndex := col/3 + (row/3) * 3 | |
// We set bits on/off instead of using a map with 1-2 bytes per answer stored | |
var flag int16 = 1<<val | |
if rowsMap[row]&flag == flag || colsMap[col]&flag == flag || gridMap[gridIndex]&flag == flag { | |
return false | |
} | |
rowsMap[row] |= flag | |
colsMap[col] |= flag | |
gridMap[gridIndex] |= flag | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment