Advertisement
ernstMach

Lift

Jan 1st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Dinglemouse {
  4.     private static Stack<Integer> liftLog;
  5.     private static Floor[] floorList;
  6.     private static boolean travelsUp;
  7.     private static Boolean switched;
  8.     private static List<Integer> lift;
  9.     private static Integer level;
  10.  
  11.     public static int[] theLift(final int[][] queues, final int capacity) {
  12.         initialize(queues);
  13.         transfer(getFloor(), capacity);
  14.         while (switched != null) {
  15.             if (reachingBorder()) switchDirection();
  16.             else if (lift.isEmpty() && oncomingWaiting()) liftToLastOncoming();
  17.             else if (otherWaiting()) liftToNextOther();
  18.             else switchDirection();
  19.             transfer(getFloor(), capacity);
  20.         }
  21.         stopLiftAt(0);
  22.         return liftLog.stream().mapToInt(i -> i).toArray();
  23.     }
  24.  
  25.     private static boolean oncomingWaiting() {
  26.         for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
  27.             if (floorList[i].hasNext(!travelsUp)) return true;
  28.         return false;
  29.     }
  30.  
  31.     private static boolean otherWaiting() {
  32.         for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
  33.             if (floorList[i].hasNext(travelsUp) || lift.contains(i)) return true;
  34.         return false;
  35.     }
  36.  
  37.     private static void liftToLastOncoming() {
  38.         switched = false;
  39.         for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
  40.             if (floorList[i].hasNext(!travelsUp)) level = i;
  41.         switchDirection();
  42.     }
  43.  
  44.     private static void liftToNextOther() {
  45.         switched = false;
  46.         for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
  47.             if (floorList[i].hasNext(travelsUp) || lift.contains(i)) {
  48.                 level = i;
  49.                 return;
  50.             }
  51.     }
  52.  
  53.     private static Floor getFloor() {
  54.         stopLiftAt(level);
  55.         return floorList[level];
  56.     }
  57.  
  58.     private static void transfer(Floor floor, int CAPACITY) {
  59.         Queue<Integer> waitingPeople = floor.getTraveler(travelsUp);
  60.         while (lift.contains(level)) lift.remove(level);
  61.         while (CAPACITY - lift.size() > 0 && !waitingPeople.isEmpty()) lift.add(waitingPeople.poll());
  62.     }
  63.  
  64.     private static void stopLiftAt(int level) {
  65.         if (liftLog.peek() != level) liftLog.push(level);
  66.     }
  67.  
  68.     private static int nextLevel(int i) {
  69.         return i + (travelsUp ? 1 : -1);
  70.     }
  71.  
  72.     private static boolean reachingBorder() {
  73.         return level > floorList.length - 2 && travelsUp || level < 1 && !travelsUp;
  74.     }
  75.  
  76.     private static boolean insideBorders(int i) {
  77.         return i < floorList.length && i >= 0;
  78.     }
  79.  
  80.     private static void switchDirection() {
  81.         if (!switched) switched = true;
  82.         else switched = null;
  83.         travelsUp = !travelsUp;
  84.     }
  85.  
  86.     private static void initialize(int[]... queues) {
  87.         switched = false;
  88.         travelsUp = true;
  89.         lift = new ArrayList<>();
  90.         floorList = new Floor[queues.length];
  91.         Arrays.setAll(floorList, i -> new Floor(queues[i], i));
  92.         level = 0;
  93.         liftLog = new Stack<>();
  94.         liftLog.push(level);
  95.     }
  96. }
  97.  
  98. class Floor {
  99.     private final Queue<Integer> pressingUp = new LinkedList<>();
  100.     private final Queue<Integer> pressingDown = new LinkedList<>();
  101.  
  102.     Floor(int[] waitingPeople, int level) {
  103.         for (int i : waitingPeople) if (i > level) pressingUp.offer(i);
  104.             else pressingDown.offer(i);
  105.     }
  106.  
  107.     public boolean hasNext(boolean travelsUp) {
  108.         return travelsUp ? !pressingUp.isEmpty() : !pressingDown.isEmpty();
  109.     }
  110.  
  111.     public Queue<Integer> getTraveler(boolean travelsUp) {
  112.         return travelsUp ? pressingUp : pressingDown;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement