Skip to content

Instantly share code, notes, and snippets.

@ichensky
Created December 26, 2018 13:25
Show Gist options
  • Save ichensky/3648e635de8c1d70808b912b93c53a35 to your computer and use it in GitHub Desktop.
Save ichensky/3648e635de8c1d70808b912b93c53a35 to your computer and use it in GitHub Desktop.
public enum Moving {
None,
Down=1,
Up=2
}
public struct State {
public int Elevator;
public int Floor { get; set; }
public Moving Moving { get; set; }
}
class Building {
public int ElevatorsCount { get; set; } = 20;
public int FloorsCount { get; set; } = 100;
public int TimeForOneFloor { get; set; } = 2; // 2 sec
public int OpenDoorsTime { get; set; } = 5; // 5 sec
public List<State> States = new List<State>(20);
}
class Program
{
public static int func(Building building, Moving moving, int fromFloor)
{
int elevator = -1;
var x = int.MaxValue;
for (int i = 0; i < building.States.Count; i++)
{
if (moving == Moving.Up && building.States[i].Moving == Moving.Up)
{
var f = (fromFloor - building.States[i].Floor) * building.TimeForOneFloor;
if (f < x)
{
x = f;
elevator = building.States[i].Elevator;
}
}
if (moving == Moving.Down && building.States[i].Moving == Moving.Down)
{
var f = (fromFloor - building.States[i].Floor) * building.TimeForOneFloor;
if (f < x)
{
x = f;
elevator = building.States[i].Elevator;
}
}
else if (building.States[i].Moving == Moving.None)
{
var f = (fromFloor - building.States[i].Floor) * building.TimeForOneFloor + building.OpenDoorsTime;
if (f < x)
{
x = f;
elevator = building.States[i].Elevator;
}
}
}
return elevator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment