From 75fd35211130e15e34c230efbc2708ea11f97e9e Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Tue, 25 Feb 2025 21:20:36 -0500 Subject: [PATCH] Idfk. --- arrays/src/arrays/Main.java | 39 +++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/arrays/src/arrays/Main.java b/arrays/src/arrays/Main.java index 8af0809..d29c7a9 100644 --- a/arrays/src/arrays/Main.java +++ b/arrays/src/arrays/Main.java @@ -38,6 +38,8 @@ class Sieve implements Iterator { } class Sudoku { + // Since we know how large the puzzle is, it'd probbaly be more efficient to + // unroll all these loops, though that's no fun. static boolean solved(int[][] puz) { if (puz.length != 9) return false; @@ -51,10 +53,18 @@ class Sudoku { } int[][] transposed = transpose(puz); - // Check that there'ren't any repeats in a column. + // Check there'ren't any repeats in a column. for (int[] col : transposed) if (isRepetitive(col)) return false; + // Check there'ren't any repeats in a square. + for (int i = 0; i < 9; i++) { + int[] square = new int[3][3]; + for (int j = i; j < i + 3; j++) { + for (int k = i; k < i + 3; k++) { square[i][k] } + } + } + return true; } @@ -84,11 +94,15 @@ class Sudoku { class Examples { void testSudoku(Tester t) { int[][] testPuz1 = { - {5, 3, 4, 6, 7, 8, 9, 1, 2}, {6, 7, 2, 1, 9, 5, 3, 4, 8}, - {1, 9, 8, 3, 4, 2, 5, 6, 7}, {8, 5, 9, 7, 6, 1, 4, 2, 3}, - {4, 2, 6, 8, 5, 3, 7, 9, 1}, {7, 1, 3, 9, 2, 4, 8, 5, 6}, - {9, 6, 1, 5, 3, 7, 2, 8, 4}, {2, 8, 7, 4, 1, 9, 6, 3, 5}, - {3, 4, 5, 2, 8, 6, 1, 7, 9} + {5, 3, 4, 6, 7, 8, 9, 1, 2}, // + {6, 7, 2, 1, 9, 5, 3, 4, 8}, // + {1, 9, 8, 3, 4, 2, 5, 6, 7}, // + {8, 5, 9, 7, 6, 1, 4, 2, 3}, // + {4, 2, 6, 8, 5, 3, 7, 9, 1}, // + {7, 1, 3, 9, 2, 4, 8, 5, 6}, // + {9, 6, 1, 5, 3, 7, 2, 8, 4}, // + {2, 8, 7, 4, 1, 9, 6, 3, 5}, // + {3, 4, 5, 2, 8, 6, 1, 7, 9} // }; int[][] testPuz2 = { {5, 3, 4, 6, 7, 8, 9, 1, 2}, {6, 7, 2, 1, 9, 5, 3, 4, 8}, @@ -129,6 +143,18 @@ class Examples { {2, 8, 7, 4, 1, 9, 6, 3, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9} }; + int[][] testPuz7 = { + // No repeats on cols or rows, but many repeats in squares. + {1, 2, 3, 4, 5, 6, 7, 8, 9}, // + {2, 3, 4, 5, 6, 7, 8, 9, 1}, // + {3, 4, 5, 6, 7, 8, 9, 1, 2}, // + {4, 5, 6, 7, 8, 9, 1, 2, 3}, // + {5, 6, 7, 8, 9, 1, 2, 3, 4}, // + {6, 7, 8, 9, 1, 2, 3, 4, 5}, // + {7, 8, 9, 1, 2, 3, 4, 5, 6}, // + {8, 9, 1, 2, 3, 4, 5, 6, 7}, // + {9, 1, 2, 3, 4, 5, 6, 7, 8} // + }; t.checkExpect(Sudoku.solved(testPuz1), true); t.checkExpect(Sudoku.solved(testPuz2), false); @@ -136,6 +162,7 @@ class Examples { t.checkExpect(Sudoku.solved(testPuz4), false); t.checkExpect(Sudoku.solved(testPuz5), false); t.checkExpect(Sudoku.solved(testPuz6), false); + t.checkExpect(Sudoku.solved(testPuz7), false); } void testSieve(Tester t) { Sieve sieve = new Sieve(29);