diff --git a/README.md b/README.md index e69de29..29d796a 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,37 @@ +# Sudoku verifier + +## Definition +Sudokus are logic number puzzles in which cells in a 9×9 grid must be populated with +the digits 1 to 9 in a way that each digit occurrs only once in each unit (that is column, +row or block = 3×3 sub grid). + +![a solved simple sudoku](sudoku.gif) + +## Goal +Validate if a given solution for a Sudoku is valid, by checking that the digit +1 to 9 appear only once in column, in each row and in each of the 9 sub grids. + +### Input +a sequence of 81 digits, organized in 9 columns and 9 rows + +### Output +A boolean value, if the grid is valid. + +### Constraints +1 <= `digit` <= 9 + +### Examples +**Input:** +``` +9 8 1 5 2 3 6 4 7 +6 3 4 8 7 9 2 5 1 +2 7 5 1 4 6 9 8 3 +1 9 6 4 8 7 5 3 2 +5 4 8 3 1 2 7 6 9 +7 2 3 6 9 5 4 1 8 +3 1 2 7 5 4 8 9 6 +4 6 9 2 3 8 1 7 5 +8 5 7 9 6 1 3 2 4 +``` +**Output:** +`true` diff --git a/settings.gradle b/settings.gradle index d5271ef..bb693f7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -rootProject.name = 'template' +rootProject.name = 'sudoku-verifier' diff --git a/src/test/java/SolutionTest.java b/src/test/java/SolutionTest.java index c8470db..c5912a4 100644 --- a/src/test/java/SolutionTest.java +++ b/src/test/java/SolutionTest.java @@ -1,8 +1,10 @@ import java.io.*; +import java.util.stream.Collectors; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.converter.*; import org.junit.jupiter.params.provider.CsvFileSource; import static java.nio.charset.StandardCharsets.UTF_8; @@ -10,6 +12,15 @@ import static org.junit.jupiter.api.Assertions.*; class SolutionTest { + static class SudokuConverter extends SimpleArgumentConverter { + @Override + protected Object convert( Object source, Class targetType ) throws ArgumentConversionException { + assertEquals(String.class, targetType, "Can only convert to String"); + BufferedReader in = new BufferedReader( new InputStreamReader( SolutionTest.class.getResourceAsStream( String.valueOf( source )))); + return in.lines().collect( Collectors.joining( System.lineSeparator() )); + } + } + static long totalDuration; @BeforeAll @@ -24,7 +35,7 @@ class SolutionTest { @ParameterizedTest @CsvFileSource( resources = "testdata.csv" ) - void main( final String input, final String expected ) throws IOException { + void main( @ConvertWith( SudokuConverter.class ) final String input, final String expected ) throws IOException { // keep original streams InputStream oldIn = System.in; PrintStream oldOut = System.out; diff --git a/src/test/resources/sudoku1.txt b/src/test/resources/sudoku1.txt new file mode 100644 index 0000000..520ae33 --- /dev/null +++ b/src/test/resources/sudoku1.txt @@ -0,0 +1,9 @@ +9 8 1 5 2 3 6 4 7 +6 3 4 8 7 9 2 5 1 +2 7 5 1 4 6 9 8 3 +1 9 6 4 8 7 5 3 2 +5 4 8 3 1 2 7 6 9 +7 2 3 6 9 5 4 1 8 +3 1 2 7 5 4 8 9 6 +4 6 9 2 3 8 1 7 5 +8 5 7 9 6 1 3 2 4 diff --git a/src/test/resources/sudoku2.txt b/src/test/resources/sudoku2.txt new file mode 100644 index 0000000..efa9df5 --- /dev/null +++ b/src/test/resources/sudoku2.txt @@ -0,0 +1,9 @@ +2 4 6 8 9 3 7 5 1 +5 9 8 6 7 1 4 2 3 +1 7 3 5 2 4 6 9 8 +7 5 1 2 3 6 8 4 9 +9 8 2 1 4 5 3 7 6 +6 3 4 7 8 9 5 1 2 +4 1 9 3 5 8 2 6 7 +8 2 5 9 6 7 1 3 4 +3 6 7 4 1 2 9 8 5 diff --git a/src/test/resources/sudoku3.txt b/src/test/resources/sudoku3.txt new file mode 100644 index 0000000..778e7e0 --- /dev/null +++ b/src/test/resources/sudoku3.txt @@ -0,0 +1,9 @@ +8 4 2 9 6 7 5 3 1 +7 9 3 5 1 8 4 6 2 +6 1 5 2 4 3 8 7 9 +2 7 9 8 7 6 3 1 4 +1 6 7 4 3 2 9 5 8 +4 3 8 1 9 5 6 2 7 +9 2 6 3 8 1 7 4 5 +3 8 1 7 5 4 2 9 6 +5 7 4 6 2 9 1 8 3 diff --git a/src/test/resources/sudoku4.txt b/src/test/resources/sudoku4.txt new file mode 100644 index 0000000..18658cf --- /dev/null +++ b/src/test/resources/sudoku4.txt @@ -0,0 +1,9 @@ +8 9 4 7 1 3 5 2 6 +7 1 2 6 5 8 4 3 9 +6 3 5 4 9 2 1 8 7 +2 4 1 5 3 6 7 9 8 +9 8 6 1 2 7 3 5 4 +3 5 7 9 8 4 2 6 1 +1 6 9 3 4 5 8 7 2 +4 2 3 8 7 9 6 1 5 +5 7 8 2 6 1 9 4 3 diff --git a/src/test/resources/sudoku5.txt b/src/test/resources/sudoku5.txt new file mode 100644 index 0000000..355c41a --- /dev/null +++ b/src/test/resources/sudoku5.txt @@ -0,0 +1,9 @@ +3 2 7 6 9 5 1 8 4 +8 1 5 4 7 3 6 2 9 +9 4 6 1 2 8 7 5 3 +6 5 1 8 4 2 3 9 7 +4 3 9 7 5 6 8 1 2 +2 7 8 3 1 9 5 6 4 +5 6 3 2 8 4 9 7 1 +7 8 4 9 3 1 2 6 5 +1 9 2 5 6 7 4 3 8 diff --git a/src/test/resources/testdata.csv b/src/test/resources/testdata.csv index e69de29..788cdbd 100644 --- a/src/test/resources/testdata.csv +++ b/src/test/resources/testdata.csv @@ -0,0 +1,5 @@ +sudoku1.txt, true +sudoku2.txt, true +sudoku3.txt, false +sudoku4.txt, true +sudoku5.txt, false diff --git a/sudoku.gif b/sudoku.gif new file mode 100644 index 0000000..0c45a00 Binary files /dev/null and b/sudoku.gif differ