Advertisement
Guest User

Untitled

a guest
Jun 12th, 2015
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.82 KB | None | 0 0
  1.     using System;
  2.     using System.Collections.Generic;
  3.     using System.IO;
  4.     using System.Threading.Tasks;
  5.  
  6.     namespace Challenge220
  7.     {
  8.         class Program
  9.         {
  10.             public static int Time = 0;
  11.             public static bool running = true;
  12.             static void Main(string[] args)
  13.             {
  14.                 List<string> lines = new List<string>();
  15.                 List<Elevator> lifts = new List<Elevator>();
  16.                 List<Rider> riders = new List<Rider>();
  17.                 List<Rider> requests = new List<Rider>();
  18.  
  19.                 try
  20.                 {
  21.                     using (StreamReader sr = new StreamReader(@"C:\input.txt"))
  22.                     {
  23.                         string line;
  24.  
  25.                         while ((line = sr.ReadLine()) != null)
  26.                         {
  27.                             lines.Add(line);
  28.                         }
  29.                     }
  30.                 }
  31.                 catch (Exception e)
  32.                 {
  33.                     Console.WriteLine(e.Message);
  34.                 }
  35.  
  36.                 for (int i = 0; i < lines.Count; i++)
  37.                 {
  38.                     string[] input = lines[i].Split(' ');
  39.  
  40.                     if (input[0].Contains("C")) { lifts.Add(new Elevator(input[0], Convert.ToInt16(input[1]), Convert.ToDecimal(input[2]), Convert.ToDecimal(input[3]))); };
  41.                     if (input[0].Contains("R")) { riders.Add(new Rider(input[0], Convert.ToInt16(input[1]), Convert.ToInt16(input[2]), Convert.ToInt16(input[3]))); };
  42.                 }
  43.  
  44.                 while (running)
  45.                 {
  46.                     for (int i = 0; i < riders.Count; i++)
  47.                     {
  48.                         if (riders[i].getRequestTime() == Time)
  49.                         {
  50.                             requests.Add(riders[i]);
  51.                             riders.Remove(riders[i]);
  52.                         }
  53.                     }
  54.  
  55.  
  56.                     foreach (var elevator in lifts)
  57.                     {
  58.                         for (int x = 0; x < requests.Count; x++)
  59.                         {
  60.                             var request = requests[x];
  61.  
  62.                             if (request.getFloor() != request.getDestination())
  63.                             {
  64.                                 if (request.getFloor() == elevator.getFloor())
  65.                                 {
  66.                                     if (elevator.getPassengerCount() < elevator.getCapacity())
  67.                                     {
  68.                                         elevator.addRider(request);
  69.  
  70.                                         decimal l = Math.Abs(elevator.getDestination() - request.getDestination());
  71.                                         decimal o = Math.Abs(elevator.getFloor() - elevator.getDestination());
  72.  
  73.                                         if (l < o)
  74.                                             elevator.setDestination(request.getDestination());
  75.                                
  76.                                         request.setTimePickedUp(Time);
  77.                                         requests.Remove(request);
  78.                                     }
  79.                                 }
  80.                             }
  81.  
  82.  
  83.                         }
  84.  
  85.  
  86.                         for (int i = 0; i < elevator.getPassengerCount(); i++)
  87.                         {
  88.                             Rider passenger = elevator.getPassenger(i);
  89.  
  90.                             if (passenger.getDestination() == elevator.getFloor())
  91.                             {
  92.                                 elevator.removeRider(passenger);
  93.                                 passenger.setTimeDroppedOff(Time);
  94.  
  95.                                 string output = "Rider: " + passenger.getIdentifier() + " requested to be picked up at time: " + passenger.getRequestTime() +
  96.                                     " and was picked up at floor " + passenger.getFloor() +
  97.                                     " at time:" + passenger.getTimePickedUp() + " and dropped off on floor " +
  98.                                     passenger.getDestination() + " at time: " + passenger.getTimeDroppedOff();
  99.  
  100.                                 Console.WriteLine(output);
  101.  
  102.                                 riders.Remove(passenger);
  103.  
  104.                                 elevator.setDestination(-1);
  105.                             }
  106.                         }
  107.  
  108.                         for (int i = 0; i < elevator.getPassengerCount(); i++)
  109.                         {
  110.                             decimal x = Math.Abs(elevator.getDestination() - elevator.getPassenger(i).getDestination());
  111.                             decimal o = Math.Abs(elevator.getFloor() - elevator.getDestination());
  112.  
  113.                             if (x < o)
  114.                                 elevator.setDestination(elevator.getPassenger(i).getDestination());
  115.  
  116.                             if (elevator.getDestination() == -1)
  117.                                 elevator.setDestination(elevator.getPassenger(i).getDestination());
  118.                         }
  119.  
  120.                         if (elevator.getDestination() != -1)
  121.                         {
  122.                             if (elevator.getDestination() > elevator.getFloor())
  123.                                 elevator.setFloor((elevator.getFloor() + elevator.getSpeed()));
  124.  
  125.                             if (elevator.getDestination() < elevator.getFloor())
  126.                                 elevator.setFloor((elevator.getFloor() - elevator.getSpeed()));
  127.  
  128.                         }
  129.                         if (riders.Count == 0 && requests.Count == 0)
  130.                             running = false;
  131.  
  132.  
  133.                     }
  134.                     Time++;
  135.                 }
  136.  
  137.                 Console.WriteLine("Output finished.");
  138.                 Console.ReadLine();
  139.             }
  140.  
  141.         }
  142.  
  143.  
  144.         class Elevator
  145.         {
  146.             private string identifier;
  147.             private int capacity;
  148.             private decimal speed;
  149.             private decimal floor;
  150.             private int destination = -1;
  151.  
  152.             private List<Rider> passengers = new List<Rider>();
  153.  
  154.             public Elevator(string ident, int cap, decimal fps, decimal start)
  155.             {
  156.                 identifier = ident;
  157.                 capacity = cap;
  158.                 speed = fps;
  159.                 floor = start;
  160.             }
  161.  
  162.             public Rider getPassenger(int i)
  163.             {
  164.                 return passengers[i];
  165.             }
  166.  
  167.             public decimal getFloor()
  168.             {
  169.                 return floor;
  170.             }
  171.  
  172.             public decimal getSpeed()
  173.             {
  174.                 return speed;
  175.             }
  176.  
  177.             public int getCapacity()
  178.             {
  179.                 return capacity;
  180.             }
  181.  
  182.             public int getPassengerCount()
  183.             {
  184.                 return passengers.Count;
  185.             }
  186.  
  187.             public int getDestination()
  188.             {
  189.                 return destination;
  190.             }
  191.  
  192.             public void setDestination(int i)
  193.             {
  194.                 destination = i;
  195.             }
  196.  
  197.             public void setFloor(decimal i)
  198.             {
  199.                 floor = i;
  200.             }
  201.  
  202.             public void addRider(Rider rider)
  203.             {
  204.                 passengers.Add(rider);
  205.             }
  206.  
  207.             public void removeRider(Rider rider)
  208.             {
  209.                 passengers.Remove(rider);
  210.             }
  211.  
  212.         }
  213.  
  214.         class Rider
  215.         {
  216.             private string identifier;
  217.             private int request;
  218.             private int floor;
  219.             private int destination;
  220.  
  221.             private int timePickedUp = 0;
  222.             private int timeDroppedOff = 0;
  223.  
  224.             public Rider(string ident, int secs, int start, int dest)
  225.             {
  226.                 identifier = ident;
  227.                 request = secs;
  228.                 floor = start;
  229.                 destination = dest;
  230.             }
  231.  
  232.             public int getFloor()
  233.             {
  234.                 return floor;
  235.             }
  236.  
  237.             public int getRequestTime()
  238.             {
  239.                 return request;
  240.             }
  241.  
  242.             public int getDestination()
  243.             {
  244.                 return destination;
  245.             }
  246.  
  247.             public int getTimePickedUp()
  248.             {
  249.                 return timePickedUp;
  250.             }
  251.  
  252.             public int getTimeDroppedOff()
  253.             {
  254.                 return timeDroppedOff;
  255.             }
  256.  
  257.             public string getIdentifier()
  258.             {
  259.                 return identifier;
  260.             }
  261.  
  262.             public void setTimePickedUp(int i)
  263.             {
  264.                 timePickedUp = i;
  265.             }
  266.  
  267.             public void setTimeDroppedOff(int i)
  268.             {
  269.                 timeDroppedOff = i;
  270.             }
  271.  
  272.         }
  273.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement