-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.java
38 lines (33 loc) · 1.14 KB
/
Day11.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
public class Day11 {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int[][] matrix = getMatrix();
System.out.println(maximumHourglassSum(matrix));
}
private static int[][] getMatrix() {
int[][] matrix = new int[6][6];
for (int i = 0 ; i < 6 ; i++) {
for (int j = 0 ; j < 6 ; j++) {
matrix[i][j] = SCANNER.nextInt();
}
}
return matrix;
}
private static int maximumHourglassSum(int[][] matrix) {
int maximumSum = -Integer.MAX_VALUE;
for (int row = 0; row < 4 ; row++) {
for (int column = 0 ; column < 4 ; column++) {
maximumSum = Math.max(maximumSum, hourglassSum(matrix, row, column));
}
}
return maximumSum;
}
private static int hourglassSum(int[][] matrix, int row, int column) {
int sum = 0;
for (int i = 0 ; i < 3 ; i++) {
sum += matrix[row][column + i] + matrix[row + 2][column + i];
}
return sum + matrix[row + 1][column + 1];
}
}