Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading.Tasks;
- namespace Challenge220
- {
- class Program
- {
- public static int Time = 0;
- public static bool running = true;
- static void Main(string[] args)
- {
- List<string> lines = new List<string>();
- List<Elevator> lifts = new List<Elevator>();
- List<Rider> riders = new List<Rider>();
- List<Rider> requests = new List<Rider>();
- try
- {
- using (StreamReader sr = new StreamReader(@"C:\input.txt"))
- {
- string line;
- while ((line = sr.ReadLine()) != null)
- {
- lines.Add(line);
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- for (int i = 0; i < lines.Count; i++)
- {
- string[] input = lines[i].Split(' ');
- if (input[0].Contains("C")) { lifts.Add(new Elevator(input[0], Convert.ToInt16(input[1]), Convert.ToDecimal(input[2]), Convert.ToDecimal(input[3]))); };
- if (input[0].Contains("R")) { riders.Add(new Rider(input[0], Convert.ToInt16(input[1]), Convert.ToInt16(input[2]), Convert.ToInt16(input[3]))); };
- }
- while (running)
- {
- for (int i = 0; i < riders.Count; i++)
- {
- if (riders[i].getRequestTime() == Time)
- {
- requests.Add(riders[i]);
- riders.Remove(riders[i]);
- }
- }
- foreach (var elevator in lifts)
- {
- for (int x = 0; x < requests.Count; x++)
- {
- var request = requests[x];
- bool changed = false;
- for (int p = 0; p < elevator.getPassengerCount(); p++)
- {
- if (request.getIdentifier() == elevator.getPassenger(p).getIdentifier())
- {
- elevator.setDestination(request.getDestination());
- changed = true;
- }
- }
- if (changed == false)
- {
- if (request.getFloor() != request.getDestination())
- {
- if (request.getFloor() == elevator.getFloor())
- {
- if (elevator.getPassengerCount() < elevator.getCapacity())
- {
- elevator.addRider(request);
- decimal l = Math.Abs(elevator.getDestination() - request.getDestination());
- decimal o = Math.Abs(elevator.getFloor() - elevator.getDestination());
- if (l < o)
- elevator.setDestination(request.getDestination());
- request.setTimePickedUp(Time);
- requests.Remove(request);
- }
- }
- }
- }
- }
- for (int i = 0; i < elevator.getPassengerCount(); i++)
- {
- Rider passenger = elevator.getPassenger(i);
- if (passenger.getDestination() == elevator.getFloor())
- {
- elevator.removeRider(passenger);
- passenger.setTimeDroppedOff(Time);
- string output = "Rider: " + passenger.getIdentifier() + " requested to be picked up at time: " + passenger.getRequestTime() +
- " and was picked up at floor " + passenger.getFloor() +
- " at time:" + passenger.getTimePickedUp() + " and dropped off on floor " +
- passenger.getDestination() + " at time: " + passenger.getTimeDroppedOff();
- Console.WriteLine(output);
- riders.Remove(passenger);
- elevator.setDestination(-1);
- }
- }
- for (int i = 0; i < elevator.getPassengerCount(); i++)
- {
- decimal x = Math.Abs(elevator.getDestination() - elevator.getPassenger(i).getDestination());
- decimal o = Math.Abs(elevator.getFloor() - elevator.getDestination());
- if (x < o)
- elevator.setDestination(elevator.getPassenger(i).getDestination());
- if (elevator.getDestination() == -1)
- elevator.setDestination(elevator.getPassenger(i).getDestination());
- }
- if (elevator.getDestination() != -1)
- {
- if (elevator.getDestination() > elevator.getFloor())
- elevator.setFloor((elevator.getFloor() + elevator.getSpeed()));
- if (elevator.getDestination() < elevator.getFloor())
- elevator.setFloor((elevator.getFloor() - elevator.getSpeed()));
- }
- if (riders.Count == 0 && requests.Count == 0)
- running = false;
- }
- Time++;
- }
- Console.WriteLine("Output finished.");
- Console.ReadLine();
- }
- }
- class Elevator
- {
- private string identifier;
- private int capacity;
- private decimal speed;
- private decimal floor;
- private int destination = -1;
- private List<Rider> passengers = new List<Rider>();
- public Elevator(string ident, int cap, decimal fps, decimal start)
- {
- identifier = ident;
- capacity = cap;
- speed = fps;
- floor = start;
- }
- public Rider getPassenger(int i)
- {
- return passengers[i];
- }
- public decimal getFloor()
- {
- return floor;
- }
- public decimal getSpeed()
- {
- return speed;
- }
- public int getCapacity()
- {
- return capacity;
- }
- public int getPassengerCount()
- {
- return passengers.Count;
- }
- public int getDestination()
- {
- return destination;
- }
- public void setDestination(int i)
- {
- destination = i;
- }
- public void setFloor(decimal i)
- {
- floor = i;
- }
- public void addRider(Rider rider)
- {
- passengers.Add(rider);
- }
- public void removeRider(Rider rider)
- {
- passengers.Remove(rider);
- }
- }
- class Rider
- {
- private string identifier;
- private int request;
- private int floor;
- private int destination;
- private int timePickedUp = 0;
- private int timeDroppedOff = 0;
- public Rider(string ident, int secs, int start, int dest)
- {
- identifier = ident;
- request = secs;
- floor = start;
- destination = dest;
- }
- public int getFloor()
- {
- return floor;
- }
- public int getRequestTime()
- {
- return request;
- }
- public int getDestination()
- {
- return destination;
- }
- public int getTimePickedUp()
- {
- return timePickedUp;
- }
- public int getTimeDroppedOff()
- {
- return timeDroppedOff;
- }
- public string getIdentifier()
- {
- return identifier;
- }
- public void setTimePickedUp(int i)
- {
- timePickedUp = i;
- }
- public void setTimeDroppedOff(int i)
- {
- timeDroppedOff = i;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement