diff --git a/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_2d/Solution.java b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_2d/Solution.java new file mode 100644 index 0000000..b3c6274 --- /dev/null +++ b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_2d/Solution.java @@ -0,0 +1,55 @@ +package eu.ztsh.training.hackerrank.datastructures.array_2d; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +/** + * Java 2D Array task. + * source: https://www.hackerrank.com/challenges/java-2d-array/problem + */ +public class Solution { + + public static void main(String[] args) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + + List> arr = new ArrayList<>(); + + IntStream.range(0, 6).forEach(i -> { + try { + arr.add( + Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) + .map(Integer::parseInt) + .collect(Collectors.toList()) + ); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + }); + + var array = arr.stream().map(l -> l.stream().mapToInt(i -> i).toArray()).toArray(int[][]::new); + var max = new AtomicInteger(Integer.MIN_VALUE); + IntStream.range(1, 5).forEach(row -> IntStream.range(1, 5).forEach(col -> { + var result = countHourglass(array, row, col); + if (result > max.get()) { + max.set(result); + } + })); + + System.out.println(max.get()); + bufferedReader.close(); + } + + private static int countHourglass(int[][] array, int row, int col) { + return array[row - 1][col - 1] + array[row - 1][col] + array[row - 1][col + 1] + + array[row][col] + + array[row + 1][col - 1] + array[row + 1][col] + array[row + 1][col + 1]; + } + +} diff --git a/src/test/java/eu/ztsh/training/hackerrank/EnvironmentTest.java b/src/test/java/eu/ztsh/training/hackerrank/EnvironmentTest.java index 5fe0e22..8abed7d 100644 --- a/src/test/java/eu/ztsh/training/hackerrank/EnvironmentTest.java +++ b/src/test/java/eu/ztsh/training/hackerrank/EnvironmentTest.java @@ -1,5 +1,8 @@ package eu.ztsh.training.hackerrank; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.List; import java.util.Scanner; import eu.ztsh.training.hackerrank.SolutionClassDescription.FieldModifier; @@ -86,6 +89,17 @@ public class EnvironmentTest { } + @Nested + @DisplayName("Test with BufferedReader created in main(String[]) method") + class EnvironmentReaderTest extends HackerRankEnvironmentTest { + + @Override + protected SolutionClassDescription getSolutionClassDescription() { + return new SolutionClassDescription(SampleSolutionWithInlineBufferedReader.class); + } + + } + } class SampleSolutionWithPrivateStaticScanner { @@ -123,3 +137,16 @@ class SampleSolutionWithInlineScanner { } } + +class SampleSolutionWithInlineBufferedReader { + + public static void main(String... args) throws IOException { + var bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + String line = bufferedReader.readLine(); + do { + System.out.println(line); + line = bufferedReader.readLine(); + } while (line != null); + } + +} diff --git a/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_2d/SolutionTest.java b/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_2d/SolutionTest.java new file mode 100644 index 0000000..75f8030 --- /dev/null +++ b/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_2d/SolutionTest.java @@ -0,0 +1,70 @@ +package eu.ztsh.training.hackerrank.datastructures.array_2d; + +import java.util.List; +import eu.ztsh.training.hackerrank.HackerRankTest; +import eu.ztsh.training.hackerrank.SolutionClassDescription; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("HackerRank challenges: Java 2D Array") +class SolutionTest extends HackerRankTest { + + @Test + public void dummyInitTest() { + assertThat(true).isTrue(); + } + + @Test + public void testCase0() { + simpleAssert( + List.of( + "1 1 1 0 0 0", + "0 1 0 0 0 0", + "1 1 1 0 0 0", + "0 0 2 4 4 0", + "0 0 0 2 0 0", + "0 0 1 2 4 0" + ), + List.of("19") + ); + } + + @Test + public void testCase1() { + simpleAssert( + List.of( + "1 1 1 0 0 0", + "0 1 0 0 0 0", + "1 1 1 0 0 0", + "0 9 2 -4 -4 0", + "0 0 0 -2 0 0", + "0 0 -1 -2 -4 0" + ), + List.of("13") + ); + } + + @Test + public void testCase3() { + simpleAssert( + List.of( + "-1 -1 0 -9 -2 -2", + "-2 -1 -6 -8 -2 -5", + "-1 -1 -1 -2 -3 -4", + "-1 -9 -2 -4 -4 -5", + "-7 -3 -3 -2 -9 -9", + "-1 -3 -1 -2 -4 -5" + ), + List.of("-6") + ); + } + + + @Override + protected SolutionClassDescription getSolutionClassDescription() { + return new SolutionClassDescription(Solution.class); + } + +} \ No newline at end of file