Advertisement
tolem

hourglass java

Mar 23rd, 2022
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HourGlass {
  4.  
  5. public static int length = 6;
  6.  
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. int[] hourGlassValues = new int[(int) Math.pow(length - 2, 2)];
  10.  
  11. for (int row = 0, column; row < length; row++) {
  12. String userInputLine = scanner.nextLine();
  13. String[] numberArray = userInputLine.split(" ");
  14. column = 0;
  15. for (String numberString : numberArray) {
  16. int number = Integer.parseInt(numberString);
  17. for (int hourGlass = 0; hourGlass < (length-2)*(length-2); hourGlass++) {
  18. if (coordinateChecker(hourGlass, row, column)) {
  19. hourGlassValues[hourGlass] += number;
  20. }
  21. }
  22. column++;
  23. }
  24. }
  25. int maxSum = Integer.MIN_VALUE;
  26. for (int i = 0; i < hourGlassValues.length; i++) {
  27. if(hourGlassValues[i] > maxSum) maxSum = hourGlassValues[i];
  28. }
  29. System.out.println(maxSum);
  30. }
  31.  
  32. public static boolean coordinateChecker(int hourglassNumber, int row, int column) {
  33. int coordinateX = hourglassNumber % (length - 2);
  34. int coordinateY = hourglassNumber / (length - 2);
  35.  
  36. return (column >= coordinateX && column <= coordinateX + 2) && (row == coordinateY || row == coordinateY + 2) ||
  37. row == coordinateY+1 && column == coordinateX+1;
  38. }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement