RGAlex

O1.The Garden.java/2019

Jun 20th, 2021
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class TheGarden {
  4.     private static char[][] matrix;
  5.     private static Map<String, Integer> vegetables;
  6.     private static int countHarmed = 0;
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         readMatrix(scan, Integer.parseInt(scan.nextLine()));
  11.         vegetables = new LinkedHashMap<>() {{ put("Carrots", 0); put("Potatoes",0); put("Lettuce", 0);}};
  12.         String input;
  13.         int row, col;
  14.  
  15.         while (!"End of Harvest".equals(input = scan.nextLine())) {
  16.             String[] command = input.split("\\s+");
  17.             row = Integer.parseInt(command[1]);
  18.             col = Integer.parseInt(command[2]);
  19.  
  20.             if (command[0].equals("Harvest")) {
  21.                 checkVegetable(row, col);
  22.             } else {
  23.                 if (isValidIndexes(row, col)) {
  24.                     if ("up".equals(command[3])) {
  25.                         moveToNext(row, col, -2, "rows");
  26.                     } else if ("down".equals(command[3])) {
  27.                         moveToNext(row, col, 2, "rows");
  28.                     } else if ("left".equals(command[3])) {
  29.                         moveToNext(row, col, -2, "cols");
  30.                     } else if ("right".equals(command[3])) {
  31.                         moveToNext(row, col, 2, "cols");
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         printMatrix();
  37.         vegetables.forEach((key, value) -> System.out.printf("%s: %d%n", key, value));
  38.         System.out.println("Harmed vegetables: " + countHarmed);
  39.  
  40.     }
  41.  
  42.     private static void moveToNext(int row, int col, int index, String direction) {
  43.         while (isValidIndexes(row, col)) {
  44.             checkCell(row, col);
  45.             if (direction.equals("rows")) {
  46.                 row = Math.abs(row) + index;
  47.             } else {
  48.                 col = Math.abs(col) + index;
  49.             }
  50.         }
  51.     }
  52.  
  53.     private static void checkCell(int row, int col) {
  54.         if (matrix[row][col] != ' ') {
  55.             countHarmed++;
  56.             matrix[row][col] = ' ';
  57.         }
  58.     }
  59.  
  60.     private static void checkVegetable(int row, int col) {
  61.         if (isValidIndexes(row, col)) {
  62.             char current = matrix[row][col];
  63.             if (current == 'L') {
  64.                 vegetables.put("Lettuce", vegetables.get("Lettuce") + 1);
  65.             } else if (current == 'P') {
  66.                 vegetables.put("Potatoes", vegetables.get("Potatoes") + 1);
  67.             } else if (current == 'C') {
  68.                 vegetables.put("Carrots", vegetables.get("Carrots") + 1);
  69.             }
  70.             matrix[row][col] = ' ';
  71.         }
  72.     }
  73.  
  74.     private static boolean isValidIndexes(int row, int col) {
  75.         return (row >= 0 && row < matrix.length) && (col >= 0 && col < matrix[row].length);
  76.     }
  77.  
  78.     private static void readMatrix(Scanner scan, int n) {
  79.         matrix = new char[n][];
  80.         for (int row = 0; row < n; row++) {
  81.             matrix[row] = scan.nextLine().replaceAll("\\s+", "").toCharArray();
  82.         }
  83.     }
  84.  
  85.     private static void printMatrix() {
  86.         Arrays.stream(matrix).forEach(e -> System.out.println(Arrays.toString(e)
  87.                 .replaceAll("[\\[\\]]", "").replaceAll(", ", " ")));
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment