diff --git a/src/main/java/Solution.java b/src/main/java/Solution.java index 5615be4..2311c7b 100644 --- a/src/main/java/Solution.java +++ b/src/main/java/Solution.java @@ -1,6 +1,4 @@ import java.util.*; -import java.io.*; -import java.math.*; /** * Template code to help you parse the standard input @@ -10,11 +8,64 @@ class Solution { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); - // read values with in.next...() methods - // code your solution here + int[][] grid = new int[9][9]; + for ( int index = 0; in.hasNext(); index++ ) { + int row = index / 9; + int col = index % 9; + grid[row][col] = in.nextShort(); + } - // Write result with System.out.println() - System.out.println( "value" ); + System.out.println( checkHorizontally( grid ) + && checkVertically( grid ) + && checkAllBlocks( grid ) + ); } + + private static boolean checkHorizontally( int[][] grid ) { + //noinspection ForLoopReplaceableByForEach + for ( int r = 0; r < grid.length; r++ ) { + BitSet bs = new BitSet( 9 ); + for ( int c = 0; c < grid[r].length; c++ ) { + bs.set( grid[r][c] ); + } + if ( bs.cardinality() != 9 ) return false; + } + return true; + } + + private static boolean checkVertically( int[][] grid ) { + for ( int c = 0; c < 9; c++ ) { + BitSet bs = new BitSet( 9 ); + //noinspection ForLoopReplaceableByForEach + for ( int r = 0; r < grid.length; r++ ) { + bs.set( grid[r][c] ); + } + if ( bs.cardinality() != 9 ) return false; + } + return true; + } + + private static boolean checkAllBlocks( int[][] grid ) { + return checkBlock( grid, 0, 0 ) + && checkBlock( grid, 0, 3 ) + && checkBlock( grid, 0, 6 ) + && checkBlock( grid, 3, 0 ) + && checkBlock( grid, 3, 3 ) + && checkBlock( grid, 3, 6 ) + && checkBlock( grid, 6, 0 ) + && checkBlock( grid, 6, 3 ) + && checkBlock( grid, 6, 6 ); + } + + private static boolean checkBlock( int[][] block, int indexTop, int indexLeft ) { + BitSet bs = new BitSet( 9 ); + for ( int r = indexTop; r < indexTop + 3; r++ ) { + for ( int c = indexLeft; c < indexLeft + 3; c++ ) { + bs.set( block[r][c] ); + } + } + return ( bs.cardinality() == 9 ); + } + }