aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Brattlof <hello@bryanbrattlof.com>2020-08-09 08:59:34 -0400
committerBryan Brattlof <hello@bryanbrattlof.com>2020-08-09 08:59:34 -0400
commit79ddf104c674fcef4f90119372e838d0a270a7d5 (patch)
tree560fea89acaa7ec7d2fa6d209676e2af4a1003ad
parent789c5ff3f7abfd78558347627b1c7360f28eb911 (diff)
downloadsudoku-79ddf104c674fcef4f90119372e838d0a270a7d5.tar.gz
sudoku-79ddf104c674fcef4f90119372e838d0a270a7d5.tar.bz2
c: move int->char cast outside 'for' loops
Instead of casting the 'n' integer into a "int" equivalent every time we iterate over a new peer index, store the new "int" into 'inum' and use that.
-rw-r--r--c/sudoku.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/c/sudoku.c b/c/sudoku.c
index 3d5b2d9..8d36d92 100644
--- a/c/sudoku.c
+++ b/c/sudoku.c
@@ -8,11 +8,13 @@ char PUZZLE[81];
bool inBox(int i, int n)
{
+ int inum = n + '0';
int boxRow = (i / 9 / 3) * 27;
int boxCol = ((i % 9) / 3) * 3;
+
for(int j = boxRow; j < boxRow + 27; j += 9) {
for(int k = boxCol; k < boxCol + 3; k++){
- if(PUZZLE[j + k] - '0' == n && i != j + k)
+ if(PUZZLE[j + k] == inum && i != j + k)
return true;
}
}
@@ -22,9 +24,11 @@ bool inBox(int i, int n)
bool inCol(int i, int n)
{
+ int inum = n + '0';
int col = i % 9;
+
for(int j = col; j < 81; j += 9) {
- if(PUZZLE[j] - '0' == n && i != j )
+ if(PUZZLE[j] == inum && i != j )
return true;
}
return false;
@@ -33,9 +37,11 @@ bool inCol(int i, int n)
bool inRow(int i, int n)
{
+ int inum = n + '0';
int row = (i/9) * 9;
+
for(int j = row; j < row + 9; j++) {
- if(PUZZLE[j] - '0' == n && i != j)
+ if(PUZZLE[j] == inum && i != j)
return true;
}
return false;