Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. public class TheGarden {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         int rows = Integer.parseInt(reader.readLine());
  13.         List<List<String>> garden = new ArrayList<>();
  14.  
  15.         for (int i = 0; i < rows; i++) {
  16.             String[] input = reader.readLine().split("\\s+");
  17.             List<String> tokens = Arrays.asList(input);
  18.             garden.add(tokens);
  19.         }
  20.  
  21.         String input = reader.readLine();
  22.  
  23.         int cCounter = 0;
  24.         int pCounter = 0;
  25.         int lCounter = 0;
  26.         int harmedCounter = 0;
  27.  
  28.         while (!input.equalsIgnoreCase("End of Harvest")) {
  29.             String[] tokens = input.split("\\s+");
  30.             String command = tokens[0];
  31.             int row = Integer.parseInt(tokens[1]);
  32.             int col = Integer.parseInt(tokens[2]);
  33.  
  34.  
  35.             if (command.equalsIgnoreCase("Harvest")) {
  36.                 if (checkIfExist(garden, row, col)) {
  37.  
  38.                     if (garden.get(row).get(col).equalsIgnoreCase("L")) {
  39.                         lCounter++;
  40.                     } else if (garden.get(row).get(col).equalsIgnoreCase("C")) {
  41.                         cCounter++;
  42.                     } else if (garden.get(row).get(col).equalsIgnoreCase("P")) {
  43.                         pCounter++;
  44.                     }
  45.                     garden.get(row).set(col, " ");
  46.                 }
  47.             } else if (command.equalsIgnoreCase("Mole")) {
  48.                 String direction = tokens[3];
  49.                 if (direction.equalsIgnoreCase("up")) {
  50.                     for (int i = row; i >= 0; i -= 2) {
  51.                         if (checkIfExist(garden, i, col)) {
  52.                             if (garden.get(i).get(col).equalsIgnoreCase("L")
  53.                                     || garden.get(i).get(col).equalsIgnoreCase("C")
  54.                                     || garden.get(i).get(col).equalsIgnoreCase("P")) {
  55.                                 harmedCounter++;
  56.                                 garden.get(i).set(col, " ");
  57.                             }
  58.                         }
  59.                     }
  60.                 } else if (direction.equalsIgnoreCase("down")) {
  61.  
  62.                     for (int i = row; i < garden.size(); i += 2) {
  63.                         if (checkIfExist(garden, i, col)) {
  64.                             if (garden.get(i).get(col).equalsIgnoreCase("L")
  65.                                     || garden.get(i).get(col).equalsIgnoreCase("C")
  66.                                     || garden.get(i).get(col).equalsIgnoreCase("P")) {
  67.                                 harmedCounter++;
  68.                                 garden.get(i).set(col, " ");
  69.                             }
  70.                         }
  71.                     }
  72.                 } else if (direction.equalsIgnoreCase("left")) {
  73.                     for (int i = col; i >= 0; i -= 2) {
  74.                         if (checkIfExist(garden, row, i)) {
  75.                             if (garden.get(row).get(i).equalsIgnoreCase("L")
  76.                                     || garden.get(row).get(i).equalsIgnoreCase("C")
  77.                                     || garden.get(row).get(i).equalsIgnoreCase("P")) {
  78.                                 harmedCounter++;
  79.                                 garden.get(row).set(i, " ");
  80.                             }
  81.                         }
  82.                     }
  83.                 } else if (direction.equalsIgnoreCase("right")) {
  84.                     for (int i = col; i < garden.get(row).size(); i += 2) {
  85.                         if (checkIfExist(garden, row, i)) {
  86.                             if (garden.get(row).get(i).equalsIgnoreCase("L")
  87.                                     || garden.get(row).get(i).equalsIgnoreCase("C")
  88.                                     || garden.get(row).get(i).equalsIgnoreCase("P")) {
  89.                                 harmedCounter++;
  90.                                 garden.get(row).set(i, " ");
  91.                             }
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.             input = reader.readLine();
  97.         }
  98.  
  99.         for (List<String> strings : garden) {
  100.             System.out.println(String.join(" ", strings));
  101.         }
  102.  
  103.         System.out.println("Carrots: " + cCounter);
  104.         System.out.println("Potatoes: " + pCounter);
  105.         System.out.println("Lettuce: " + lCounter);
  106.         System.out.println("Harmed vegetables: " + harmedCounter);
  107.  
  108.     }
  109.  
  110.     private static boolean checkIfExist(List<List<String>> garden, int row, int col) {
  111.         return garden.size() > row && garden.get(row).size() > col;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement