Advertisement
Guest User

Untitled

a guest
Jun 12th, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.59 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.                         bool changed = false;
  62.  
  63.                         for (int p = 0; p < elevator.getPassengerCount(); p++)
  64.                         {
  65.                             if (request.getIdentifier() == elevator.getPassenger(p).getIdentifier())
  66.                             {
  67.                                 elevator.setDestination(request.getDestination());
  68.                                 changed = true;
  69.                             }
  70.  
  71.                         }
  72.  
  73.                         if (changed == false)
  74.                         {
  75.                             if (request.getFloor() != request.getDestination())
  76.                             {
  77.                                 if (request.getFloor() == elevator.getFloor())
  78.                                 {
  79.                                     if (elevator.getPassengerCount() < elevator.getCapacity())
  80.                                     {
  81.                                         elevator.addRider(request);
  82.  
  83.                                         decimal l = Math.Abs(elevator.getDestination() - request.getDestination());
  84.                                         decimal o = Math.Abs(elevator.getFloor() - elevator.getDestination());
  85.  
  86.                                         if (l < o)
  87.                                             elevator.setDestination(request.getDestination());
  88.  
  89.                                         request.setTimePickedUp(Time);
  90.                                         requests.Remove(request);
  91.                                     }
  92.  
  93.                                 }
  94.  
  95.                             }
  96.                         }
  97.  
  98.  
  99.                     }
  100.  
  101.  
  102.                     for (int i = 0; i < elevator.getPassengerCount(); i++)
  103.                     {
  104.                         Rider passenger = elevator.getPassenger(i);
  105.  
  106.  
  107.  
  108.                         if (passenger.getDestination() == elevator.getFloor())
  109.                         {
  110.                             elevator.removeRider(passenger);
  111.                             passenger.setTimeDroppedOff(Time);
  112.  
  113.                             string output = "Rider: " + passenger.getIdentifier() + " requested to be picked up at time: " + passenger.getRequestTime() +
  114.                                 " and was picked up at floor " + passenger.getFloor() +
  115.                                 " at time:" + passenger.getTimePickedUp() + " and dropped off on floor " +
  116.                                 passenger.getDestination() + " at time: " + passenger.getTimeDroppedOff();
  117.  
  118.                             Console.WriteLine(output);
  119.  
  120.                             riders.Remove(passenger);
  121.  
  122.                             elevator.setDestination(-1);
  123.                         }
  124.                     }
  125.  
  126.                     for (int i = 0; i < elevator.getPassengerCount(); i++)
  127.                     {
  128.                         decimal x = Math.Abs(elevator.getDestination() - elevator.getPassenger(i).getDestination());
  129.                         decimal o = Math.Abs(elevator.getFloor() - elevator.getDestination());
  130.  
  131.                         if (x < o)
  132.                             elevator.setDestination(elevator.getPassenger(i).getDestination());
  133.  
  134.                         if (elevator.getDestination() == -1)
  135.                             elevator.setDestination(elevator.getPassenger(i).getDestination());
  136.                     }
  137.  
  138.                     if (elevator.getDestination() != -1)
  139.                     {
  140.                         if (elevator.getDestination() > elevator.getFloor())
  141.                             elevator.setFloor((elevator.getFloor() + elevator.getSpeed()));
  142.  
  143.                         if (elevator.getDestination() < elevator.getFloor())
  144.                             elevator.setFloor((elevator.getFloor() - elevator.getSpeed()));
  145.  
  146.                     }
  147.                     if (riders.Count == 0 && requests.Count == 0)
  148.                         running = false;
  149.  
  150.  
  151.                 }
  152.                 Time++;
  153.             }
  154.  
  155.             Console.WriteLine("Output finished.");
  156.             Console.ReadLine();
  157.         }
  158.  
  159.     }
  160.  
  161.  
  162.     class Elevator
  163.     {
  164.         private string identifier;
  165.         private int capacity;
  166.         private decimal speed;
  167.         private decimal floor;
  168.         private int destination = -1;
  169.  
  170.         private List<Rider> passengers = new List<Rider>();
  171.  
  172.         public Elevator(string ident, int cap, decimal fps, decimal start)
  173.         {
  174.             identifier = ident;
  175.             capacity = cap;
  176.             speed = fps;
  177.             floor = start;
  178.         }
  179.  
  180.         public Rider getPassenger(int i)
  181.         {
  182.             return passengers[i];
  183.         }
  184.  
  185.         public decimal getFloor()
  186.         {
  187.             return floor;
  188.         }
  189.  
  190.         public decimal getSpeed()
  191.         {
  192.             return speed;
  193.         }
  194.  
  195.         public int getCapacity()
  196.         {
  197.             return capacity;
  198.         }
  199.  
  200.         public int getPassengerCount()
  201.         {
  202.             return passengers.Count;
  203.         }
  204.  
  205.         public int getDestination()
  206.         {
  207.             return destination;
  208.         }
  209.  
  210.         public void setDestination(int i)
  211.         {
  212.             destination = i;
  213.         }
  214.  
  215.         public void setFloor(decimal i)
  216.         {
  217.             floor = i;
  218.         }
  219.  
  220.         public void addRider(Rider rider)
  221.         {
  222.             passengers.Add(rider);
  223.         }
  224.  
  225.         public void removeRider(Rider rider)
  226.         {
  227.             passengers.Remove(rider);
  228.         }
  229.  
  230.     }
  231.  
  232.     class Rider
  233.     {
  234.         private string identifier;
  235.         private int request;
  236.         private int floor;
  237.         private int destination;
  238.  
  239.         private int timePickedUp = 0;
  240.         private int timeDroppedOff = 0;
  241.  
  242.         public Rider(string ident, int secs, int start, int dest)
  243.         {
  244.             identifier = ident;
  245.             request = secs;
  246.             floor = start;
  247.             destination = dest;
  248.         }
  249.  
  250.         public int getFloor()
  251.         {
  252.             return floor;
  253.         }
  254.  
  255.         public int getRequestTime()
  256.         {
  257.             return request;
  258.         }
  259.  
  260.         public int getDestination()
  261.         {
  262.             return destination;
  263.         }
  264.  
  265.         public int getTimePickedUp()
  266.         {
  267.             return timePickedUp;
  268.         }
  269.  
  270.         public int getTimeDroppedOff()
  271.         {
  272.             return timeDroppedOff;
  273.         }
  274.  
  275.         public string getIdentifier()
  276.         {
  277.             return identifier;
  278.         }
  279.  
  280.         public void setTimePickedUp(int i)
  281.         {
  282.             timePickedUp = i;
  283.         }
  284.  
  285.         public void setTimeDroppedOff(int i)
  286.         {
  287.             timeDroppedOff = i;
  288.         }
  289.  
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement