Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class TheGarden {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int rows = Integer.parseInt(reader.readLine());
- List<List<String>> garden = new ArrayList<>();
- for (int i = 0; i < rows; i++) {
- String[] input = reader.readLine().split("\\s+");
- List<String> tokens = Arrays.asList(input);
- garden.add(tokens);
- }
- String input = reader.readLine();
- int cCounter = 0;
- int pCounter = 0;
- int lCounter = 0;
- int harmedCounter = 0;
- while (!input.equalsIgnoreCase("End of Harvest")) {
- String[] tokens = input.split("\\s+");
- String command = tokens[0];
- int row = Integer.parseInt(tokens[1]);
- int col = Integer.parseInt(tokens[2]);
- if (command.equalsIgnoreCase("Harvest")) {
- if (checkIfExist(garden, row, col)) {
- if (garden.get(row).get(col).equalsIgnoreCase("L")) {
- lCounter++;
- } else if (garden.get(row).get(col).equalsIgnoreCase("C")) {
- cCounter++;
- } else if (garden.get(row).get(col).equalsIgnoreCase("P")) {
- pCounter++;
- }
- garden.get(row).set(col, " ");
- }
- } else if (command.equalsIgnoreCase("Mole")) {
- String direction = tokens[3];
- if (direction.equalsIgnoreCase("up")) {
- for (int i = row; i >= 0; i -= 2) {
- if (checkIfExist(garden, i, col)) {
- if (garden.get(i).get(col).equalsIgnoreCase("L")
- || garden.get(i).get(col).equalsIgnoreCase("C")
- || garden.get(i).get(col).equalsIgnoreCase("P")) {
- harmedCounter++;
- garden.get(i).set(col, " ");
- }
- }
- }
- } else if (direction.equalsIgnoreCase("down")) {
- for (int i = row; i < garden.size(); i += 2) {
- if (checkIfExist(garden, i, col)) {
- if (garden.get(i).get(col).equalsIgnoreCase("L")
- || garden.get(i).get(col).equalsIgnoreCase("C")
- || garden.get(i).get(col).equalsIgnoreCase("P")) {
- harmedCounter++;
- garden.get(i).set(col, " ");
- }
- }
- }
- } else if (direction.equalsIgnoreCase("left")) {
- for (int i = col; i >= 0; i -= 2) {
- if (checkIfExist(garden, row, i)) {
- if (garden.get(row).get(i).equalsIgnoreCase("L")
- || garden.get(row).get(i).equalsIgnoreCase("C")
- || garden.get(row).get(i).equalsIgnoreCase("P")) {
- harmedCounter++;
- garden.get(row).set(i, " ");
- }
- }
- }
- } else if (direction.equalsIgnoreCase("right")) {
- for (int i = col; i < garden.get(row).size(); i += 2) {
- if (checkIfExist(garden, row, i)) {
- if (garden.get(row).get(i).equalsIgnoreCase("L")
- || garden.get(row).get(i).equalsIgnoreCase("C")
- || garden.get(row).get(i).equalsIgnoreCase("P")) {
- harmedCounter++;
- garden.get(row).set(i, " ");
- }
- }
- }
- }
- }
- input = reader.readLine();
- }
- for (List<String> strings : garden) {
- System.out.println(String.join(" ", strings));
- }
- System.out.println("Carrots: " + cCounter);
- System.out.println("Potatoes: " + pCounter);
- System.out.println("Lettuce: " + lCounter);
- System.out.println("Harmed vegetables: " + harmedCounter);
- }
- private static boolean checkIfExist(List<List<String>> garden, int row, int col) {
- return garden.size() > row && garden.get(row).size() > col;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement