Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Dinglemouse {
- private static Stack<Integer> liftLog;
- private static Floor[] floorList;
- private static boolean travelsUp;
- private static Boolean switched;
- private static List<Integer> lift;
- private static Integer level;
- public static int[] theLift(final int[][] queues, final int capacity) {
- initialize(queues);
- transfer(getFloor(), capacity);
- while (switched != null) {
- if (reachingBorder()) switchDirection();
- else if (lift.isEmpty() && oncomingWaiting()) liftToLastOncoming();
- else if (otherWaiting()) liftToNextOther();
- else switchDirection();
- transfer(getFloor(), capacity);
- }
- stopLiftAt(0);
- return liftLog.stream().mapToInt(i -> i).toArray();
- }
- private static boolean oncomingWaiting() {
- for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
- if (floorList[i].hasNext(!travelsUp)) return true;
- return false;
- }
- private static boolean otherWaiting() {
- for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
- if (floorList[i].hasNext(travelsUp) || lift.contains(i)) return true;
- return false;
- }
- private static void liftToLastOncoming() {
- switched = false;
- for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
- if (floorList[i].hasNext(!travelsUp)) level = i;
- switchDirection();
- }
- private static void liftToNextOther() {
- switched = false;
- for (int i = nextLevel(level); insideBorders(i); i = nextLevel(i))
- if (floorList[i].hasNext(travelsUp) || lift.contains(i)) {
- level = i;
- return;
- }
- }
- private static Floor getFloor() {
- stopLiftAt(level);
- return floorList[level];
- }
- private static void transfer(Floor floor, int CAPACITY) {
- Queue<Integer> waitingPeople = floor.getTraveler(travelsUp);
- while (lift.contains(level)) lift.remove(level);
- while (CAPACITY - lift.size() > 0 && !waitingPeople.isEmpty()) lift.add(waitingPeople.poll());
- }
- private static void stopLiftAt(int level) {
- if (liftLog.peek() != level) liftLog.push(level);
- }
- private static int nextLevel(int i) {
- return i + (travelsUp ? 1 : -1);
- }
- private static boolean reachingBorder() {
- return level > floorList.length - 2 && travelsUp || level < 1 && !travelsUp;
- }
- private static boolean insideBorders(int i) {
- return i < floorList.length && i >= 0;
- }
- private static void switchDirection() {
- if (!switched) switched = true;
- else switched = null;
- travelsUp = !travelsUp;
- }
- private static void initialize(int[]... queues) {
- switched = false;
- travelsUp = true;
- lift = new ArrayList<>();
- floorList = new Floor[queues.length];
- Arrays.setAll(floorList, i -> new Floor(queues[i], i));
- level = 0;
- liftLog = new Stack<>();
- liftLog.push(level);
- }
- }
- class Floor {
- private final Queue<Integer> pressingUp = new LinkedList<>();
- private final Queue<Integer> pressingDown = new LinkedList<>();
- Floor(int[] waitingPeople, int level) {
- for (int i : waitingPeople) if (i > level) pressingUp.offer(i);
- else pressingDown.offer(i);
- }
- public boolean hasNext(boolean travelsUp) {
- return travelsUp ? !pressingUp.isEmpty() : !pressingDown.isEmpty();
- }
- public Queue<Integer> getTraveler(boolean travelsUp) {
- return travelsUp ? pressingUp : pressingDown;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement