/*static Connection getNthConnection(int stA, int stB, int n) * { * int counter = 0; * foreach (Connection con in allConnections) * { * if (con.StationA == stA && con.StationB == stB) counter++; * if (counter == n) return con; * } * return null; * }*/ static MyTime getPathTime(Path input) { MyTime start = new MyTime() { hour = startingtime.Hour, minute = startingtime.Minute }; int first = input[input.Length - 1].Number; //получили id первой станции int second = input[input.Length - 2].Number; //получили id второй станции int minutes; Bus between = getBusBetwenTwo(first, second, out minutes); //первый автобус, который нужен MyTime now = new MyTime() { hour = startingtime.Hour, minute = startingtime.Minute }; waitingforbus(getStationByNum(first + 1), between, start, out now); //Console.WriteLine($" time betwen {first + 1} and {second + 1} = {minutes}"); //Console.WriteLine($"Transfer from {first + 1} to {second + 1} on {between.Name} for {minutes} minutes"); now = now.addMinutes(minutes); //Console.WriteLine($"Transfer from {first + 1} to {second + 1} is done"); Bus previous = between; for (int i = input.Length - 2; i >= 1; i--) { first = input[i].Number; second = input[i - 1].Number; minutes = 0; between = getBusBetwenTwo(first, second, out minutes); //Console.WriteLine($"Transfer from {first + 1} to {second + 1} on {between.Name} for {minutes} minutes"); //$" previous = {previous.Name}"); if (between != previous) { //Console.WriteLine($"New bus!"); waitingforbus(getStationByNum(first + 1), between, (MyTime)now.Clone(), out now); now = now.addMinutes(getMinutesBetweenTwo(first + 1, second + 1)); } else { now = now.addMinutes(getMinutesBetweenTwo(first + 1, second + 1)); //Console.WriteLine($"Just going next from {first + 1} to {second + 1} now: {now.getString()}"); } previous = between; } return(now); }
static Path getFastestPath() { Path rez = null; int rezid = 0; MyTime fromhome = new MyTime() { hour = startingtime.Hour, minute = startingtime.Minute }; MyTime rezrezult = getPathTime(Paths[0]); Console.WriteLine("[ ]"); if (counter == 1) { rez = Paths[0]; MyTime pathrezult = rezrezult; //Console.WriteLine(rezrezult.getString() + " " + fromhome.getString()); if (pathrezult.gettotal() >= 24 * 60) { Console.WriteLine($"[!] There are only one path from {rez[rez.Length - 1].Number + 1} to {rez[0].Number + 1} and it is too late. Wasted time: {(rezrezult - fromhome).getString()}. Way complete at {rezrezult.getString()}"); } else { Console.WriteLine($"[!] There are only one path from {rez[rez.Length - 1].Number + 1} to {rez[0].Number + 1}, wasted time: {(rezrezult - fromhome).getString()}. Way complete at {rezrezult.getString()}"); } Paths[rezid].printRev(); } else { for (int j = 0; j < counter; j++) { if (rez == null) { rez = Paths[j]; } //Console.WriteLine($" [ ] Checking {j + 1}"); MyTime pathrezult = getPathTime(Paths[j]); if (pathrezult.gettotal() >= 24 * 60) { Console.WriteLine($" [ ] Too late for Path {j + 1}, wasted time: {(pathrezult - fromhome).getString()}. Way complete at {pathrezult.getString()}"); } else if (pathrezult != null) { //rezrezult = getPathTime(rez); Console.WriteLine($" [ ] Path {j + 1} rezult: {pathrezult.getString()}"); int first = Paths[j][Paths[j].Length - 1].Number; int second = Paths[j][Paths[j].Length - 2].Number; Bus pathstartBus = getBusBetwenTwo(first, second); MyTime startbusstart = new MyTime() { hour = pathstartBus.start.Hour, minute = pathstartBus.start.Minute }; Console.WriteLine($" [ ] Wasted time for Path {j + 1} = {(pathrezult - fromhome).getString()}"); if (pathrezult <= rezrezult) { rez = Paths[j]; rezid = j; rezrezult = pathrezult; } } } Console.WriteLine($"[!] The fastest path is {rezid + 1}, wasted time: {(rezrezult - fromhome).getString()}. Way complete at {rezrezult.getString()}"); Paths[rezid].printRev(); } return(rez); }
static void waitingforbus(Station where, Bus targetbus, MyTime now, out MyTime rezult) { //возвращает в rezult время прибытия автобуса //Console.WriteLine($"Begin wait on {where.num} for {targetbus.Name}. Now: {now.getString()}:"); rezult = (MyTime)now.Clone(); Station temp = targetbus.getFirstStation(); MyTime busstart = new MyTime() { hour = targetbus.start.Hour, minute = targetbus.start.Minute }; rezult = (MyTime)busstart.Clone(); //Console.WriteLine($"first station: {temp.num} Busstart: {rezult.getString()} WHERE: {where.num}"); if (where.num == temp.num && now.gettotal() <= busstart.gettotal()) { //rezult = now; } else if (where.num == temp.num) { while (true) { //Console.WriteLine("waitingfrobus"); int minutes = 0; temp = getNextStation(temp, targetbus, out minutes); rezult = rezult.addMinutes(minutes); //Console.WriteLine($"Transmissed to {temp.num}. rezult: {rezult.getString()}"); if (temp.num == where.num && rezult.gettotal() >= now.gettotal()) { break; } } } else { if (now.gettotal() <= busstart.gettotal()) { while (true) { //Console.WriteLine("waitingfrobus else"); int minutes = 0; temp = getNextStation(temp, targetbus, out minutes); rezult = rezult.addMinutes(minutes); //Console.WriteLine($"Transmissed to {temp.num}. rezult: {rezult.getString()}"); if (temp.num == where.num && rezult.gettotal() >= now.gettotal()) { break; } } } else { while (true) { //Console.WriteLine("waitingfrobus else else"); int minutes = 0; temp = getNextStation(temp, targetbus, out minutes); rezult = rezult.addMinutes(minutes); //Console.WriteLine($"Transmissed to {temp.num}. rezult: {rezult.getString()}" + //$"Needed time: {now.getString()}"); if (temp.num != where.num && rezult.gettotal() >= now.gettotal()) { break; } } } } //Console.WriteLine($"Wait finished, now: {rezult.getString()}"); }