Advertisement
Guest User

Untitled

a guest
Jun 26th, 2020
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. package ExamPractice.Exam2019Dec17;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.nio.charset.StandardCharsets;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Pr02PresentDelivery {
  10.  
  11.     public static void main(String[] args) {
  12.         final BufferedReader reader = new BufferedReader(
  13.                 new InputStreamReader(System.in, StandardCharsets.UTF_8));
  14.  
  15.         Engine engine = Engine.init(reader);
  16.         engine.run();
  17.     }
  18.  
  19.     private static final class Engine {
  20.         private static final String SANTA = "S";
  21.         private static final String NICE_KID = "V";
  22.         private static final String NAUGHTY_KID = "X";
  23.         private static final String COOKIE = "C";
  24.         private static final String EMPTY = "-";
  25.  
  26.         private static final String SEPARATOR = " ";
  27.         private static final Pattern SPLITTER = Pattern.compile(SEPARATOR);
  28.  
  29.  
  30.         private final BufferedReader reader;
  31.         private String[][] map = null;
  32.         private int santaRow = -1;
  33.         private int santaCol = -1;
  34.         private int niceKidsCount = 0;
  35.         private int niceKidPresentsCount = 0;
  36.         private int presentsCount = 0;
  37.         private boolean theHack = false;
  38.  
  39.         private Engine(final BufferedReader reader) {
  40.             this.reader = reader;
  41.         }
  42.  
  43.         public static Engine init(final BufferedReader reader) {
  44.             Engine engine = new Engine(reader);
  45.  
  46.             engine.presentsCount = Integer.parseInt(engine.readLine());
  47.             engine.map = reader.lines()
  48.                     .limit(Long.parseLong(engine.readLine()))
  49.                     .map(SPLITTER::split)
  50.                     .toArray(String[][]::new);
  51.  
  52.             for (int row = 0; row < engine.map.length; row++) {
  53.                 for (int col = 0; col < engine.map[row].length; col++) {
  54.                     String symbol = engine.map[row][col];
  55.                     switch (symbol) {
  56.                     case SANTA:
  57.                         engine.santaRow = row;
  58.                         engine.santaCol = col;
  59.                         break;
  60.                     case NICE_KID:
  61.                         engine.niceKidsCount++;
  62.                         break;
  63.                     case COOKIE:
  64.                     case NAUGHTY_KID:
  65.                     case EMPTY:
  66.                         break;
  67.                     default:
  68.                         throw new IllegalStateException(
  69.                                 "Unknown symbol on map: " + symbol);
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             return engine;
  75.         }
  76.  
  77.         private String readLine() {
  78.             try {
  79.                 return reader.readLine();
  80.             } catch (IOException e) {
  81.                 throw new IllegalArgumentException(e);
  82.             }
  83.         }
  84.  
  85.         public void run() {
  86.             String command;
  87.             while (hasPresentsLeft() && !"Christmas morning".equals(command = readLine())) {
  88.                 if (!moveTo(toDirection(command))) {
  89.                     break;
  90.                 }
  91.                 process(false);
  92.             }
  93.  
  94.             if (!theHack && !hasPresentsLeft()) {
  95.                 System.out.println("Santa ran out of presents!");
  96.             }
  97.  
  98.             for (String[] row : map) {
  99.                 System.out.println(String.join(SEPARATOR, row));
  100.             }
  101.  
  102.             if (niceKidsCount == niceKidPresentsCount) {
  103.                 System.out.printf("Good job, Santa! %d happy nice kid/s.%n", niceKidsCount);
  104.             } else {
  105.                 System.out.printf("No presents for %d nice kid/s.%n", niceKidsCount - niceKidPresentsCount);
  106.             }
  107.         }
  108.  
  109.         private void process(boolean isHappy) {
  110.             switch (map[santaRow][santaCol]) {
  111.             case NICE_KID:
  112.                 givePresent(true);
  113.                 break;
  114.             case NAUGHTY_KID:
  115.                 if (isHappy) {
  116.                     givePresent(false);
  117.                 }
  118.                 break;
  119.             case COOKIE:
  120.                 int cookieRow = santaRow;
  121.                 int cookieCol = santaCol;
  122.                 for (Direction direction : Direction.values()) {
  123.                     santaRow = cookieRow;
  124.                     santaCol = cookieCol;
  125.                     if (moveTo(direction)) {
  126.                         process(true);
  127.                     }
  128.                 }
  129.                 santaRow = cookieRow;
  130.                 santaCol = cookieCol;
  131.                 theHack = !hasPresentsLeft(); // A hack for Judge
  132.                 break;
  133.             default:
  134.                 break;
  135.             }
  136.  
  137.             if (isHappy) {
  138.                 map[santaRow][santaCol] = EMPTY;
  139.             } else {
  140.                 map[santaRow][santaCol] = SANTA;
  141.             }
  142.         }
  143.  
  144.         private void givePresent(boolean isNiceKid) {
  145.             if (!hasPresentsLeft()) {
  146.                 return;
  147.             }
  148.  
  149.             presentsCount--;
  150.  
  151.             if (isNiceKid) {
  152.                 niceKidPresentsCount++;
  153.             }
  154.         }
  155.  
  156.         private boolean hasPresentsLeft() {
  157.             return presentsCount > 0;
  158.         }
  159.  
  160.         private Direction toDirection(String directionStr) {
  161.             return Direction.valueOf(directionStr.toUpperCase());
  162.         }
  163.  
  164.         private boolean moveTo(Direction direction) {
  165.             int newRow = santaRow;
  166.             int newCol = santaCol;
  167.             switch (direction) {
  168.             case UP:
  169.                 newRow--;
  170.                 break;
  171.             case RIGHT:
  172.                 newCol++;
  173.                 break;
  174.             case DOWN:
  175.                 newRow++;
  176.                 break;
  177.             case LEFT:
  178.                 newCol--;
  179.                 break;
  180.             }
  181.  
  182.             if (newRow < 0 || newRow >= map.length ||
  183.                     newCol < 0 || newCol >= map[santaRow].length) {
  184.                 return false;
  185.             }
  186.            
  187.             map[santaRow][santaCol] = EMPTY;
  188.            
  189.             santaRow = newRow;
  190.             santaCol = newCol;
  191.            
  192.             return true;
  193.         }
  194.  
  195.         private enum Direction {
  196.             UP,
  197.             RIGHT,
  198.             DOWN,
  199.             LEFT;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement