Ad
Algorithms
Data Structures
Simulation
Logic
Games
Date Time

The Train Driver is the most famous train driver of all Skyport. It is said they are the fastest and most handsome train driver. But they have a problem, they do not know hot to calculate the time properly.
As his/her assistant, your job is to figure out at what time they will arrive at their final destination, taking into account that they have to wait some time to prepare everytime before they start a trip.

You will be provided with a list of The Train Driver's destinations in order and the departure time from his starting station in Skyport.
Sometimes, the machine that prints the itinerary will give an empty list. In that case, that means The Train Driver has a day off.
If two identical cities a listed in a row, The Train Driver must wait there an hour in order to complain to his superior, The Mighty Boss.

ORIGIN DESTINATION STANDBY TIME TRIP DURATION
Skyport Crystalium 15 min 2 hours
Crystalium Skyport 10 min 2 hours
Skyport Oasis 20 min 3 hours
Oasis Skyport 15 min 3 hours
Oasis Crystalium 15 min 1.5 hours
Crystalium Oasis 10 min 1.5 hours
Skyport Nexus 15 min 4 hours
Nexus Skyport 10 min 4 hours

EXAMPLES

["Crystalium"], "10:00" -> should return "12:15"
Skyport -> Crystalium: 15 minutes + 2 hours -> 12:15
 
["Crystalium", "Skyport", "Oasis"], "10:00" -> should return "17:45" 
Skyport -> Crystalium: 15 minutes + 2 hours -> 12:15
Crystalium -> Skyport: 10 minutes + 2 hours -> 14:25
Skyport -> Oasis: 20 minutes + 3 hours -> 17:45
 
["Nexus", "Skyport", "Oasis"], "21:30"  -> should return "09:15" 
Skyport -> Nexus: 15 minutes + 4 hours -> 02:45
Nexus -> Skyport: 10 minutes + 4 hours -> 06:55
Skyport -> Oasis: 20 minutes + 3 hours -> 09:15
 
["Skyport"], "22:00"  -> should return "23:00" 
Skyport -> Skyport: 1 hour wait -> 23:00
 
["Crystalium", "Nexus"], "12:00" -> should return "20:40"
Skyport -> Crystalium: 15 minutes + 2 hours -> 14:15
Crystalium -> Skyport: 10 minutes + 2 hours -> 16:25
Skyport -> Nexus: 15 minutes + 4 hours -> 20:40

SPECIAL CASES

  • If there are no destinations, return "The Train Driver has the day off".
  • The Train Driver always starts his shift at Skyport.
  • Whenever a train is taken, the waiting time must be added, even if it's the last destination.
  • If there is no direct connection to his next destination, The Train Driver must return to Skyport as SkyPort has connection to all the other cities and then continue their journey from there. The time it takes to return to Skyport and the waiting time also count.
  • If the next destination is the same city The Train Driver is already at, they must wait an hour before continuing.
  • The expected format for the output is 'HH:mm'. '8:00' is not a valid format; instead, '08:00' should be returned.
Code
Diff
  • import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Kata {
    
    	public static String arrivalTime(final String[] route, final String departureTime) {
    		if (route.length == 0) return "The Train Driver has the day off";
    
    		LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
    		String origin = "Skyport";
    		int index = 0;
    		while (index < route.length) {
    			String destination = route[index];
    			Map<String, double[]> destinationMap = tableTime.get(origin);
    			if (destinationMap != null && destinationMap.containsKey(destination)) {
    				double[] times = destinationMap.get(destination);
    				timeResult = calculateTime(timeResult, times);
    				origin = destination;
    				index++;
    			} else if (origin.equals(destination)) {
    				timeResult = timeResult.plusHours(1);
    				index++;
    			} else {
    				String transfer = "Skyport";
    				double[] times = tableTime.get(origin).get(transfer);
    				timeResult = calculateTime(timeResult, times);
    				origin = transfer;
    			}
    		}
    		return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
    	}
    
    	private static Map<String, Map<String, double[]>> tableTime = new HashMap<String, Map<String, double[]>>() {
    		{
    			addRecord("Skyport", "Crystalium", 15, 2);
    			addRecord("Skyport", "Oasis", 20, 3);
    			addRecord("Oasis", "Crystalium", 15, 1.5);
    			addRecord("Skyport", "Nexus", 15, 4);
    		}
    
    	private void addRecord(final String origin, final String destiny, final double minutesWait,
    				final double hoursTravel) {
    			putIfAbsent(origin, new HashMap<>());
    			get(origin).put(destiny, new double[] { minutesWait, hoursTravel });
    
    			putIfAbsent(destiny, new HashMap<>());
    			get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel });
    		}
    	};
    
    	private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) {
    		int minutes = (int) times[0];
    		int remainingMinutes = (int) (times[1] * 60);
    
    		return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes);
    	}
    }
    • import java.time.LocalTime;
    • import java.time.format.DateTimeFormatter;
    • import java.util.HashMap;
    • import java.util.Map;
    • public class Kata {
    • public static String arrivalTime(final String[] route, final String departureTime) {
    • if (route.length == 0) return "The Train Driver has the day off";
    • LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
    • String origin = "Skyport";
    • int index = 0;
    • while (index < route.length) {
    • String destination = route[index];
    • Map<String, double[]> destinationMap = tableTime.get(origin);
    • if (destinationMap != null && destinationMap.containsKey(destination)) {
    • double[] times = destinationMap.get(destination);
    • timeResult = calculateTime(timeResult, times);
    • origin = destination;
    • index++;
    • } else if (origin.equals(destination)) {
    • timeResult = timeResult.plusHours(1);
    • index++;
    • } else {
    • String transfer = "Skyport";
    • double[] times = tableTime.get(origin).get(transfer);
    • timeResult = calculateTime(timeResult, times);
    • origin = transfer;
    • }
    • }
    • return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
    • }
    • private static Map<String, Map<String, double[]>> tableTime = new HashMap<String, Map<String, double[]>>() {
    • {
    • addRecord("Skyport", "Crystalium", 15, 2);
    • addRecord("Skyport", "Oasis", 20, 3);
    • addRecord("Oasis", "Crystalium", 15, 1.5);
    • addRecord("Skyport", "Nexus", 15, 4);
    • }
    • private void addRecord(final String origin, final String destiny, final double minutesWait,
    • private void addRecord(final String origin, final String destiny, final double minutesWait,
    • final double hoursTravel) {
    • putIfAbsent(origin, new HashMap<>());
    • get(origin).put(destiny, new double[] { minutesWait, hoursTravel });
    • putIfAbsent(destiny, new HashMap<>());
    • get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel });
    • }
    • };
    • private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) {
    • int minutes = (int) times[0];
    • int remainingMinutes = (int) (times[1] * 60);
    • return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes);
    • }
    • }
Algorithms
Data Structures
Simulation
Logic
Games
Date Time

The Train Driver is the most famous train driver in all Skyport. It is said they are the fastest and most handsome train driver. But they have a problem, they do not know hot to calculate the time properly.
As his/her assistant, your job is to figure out at what time they will arrive at their final destination, taking into account that they have to wait some time prepare whenever they reach a destination.

You will be provided with a list of The Train Driver's destinations in order and the departure time from his starting station in Skyport.
Sometimes, the machine that prints the itinerary will give an empty list. In that case, that means The Train Driver has a day off.
If two identical cities a listed in a row, The Train Driver must wait there an hour in order to complain to his superior, The Mighty Boss.

ORIGIN DESTINATION STANDBY TIME TRIP DURATION
Skyport Crystalium 15 min 2 hours
Crystalium Skyport 10 min 2 hours
Skyport Oasis 20 min 3 hours
Oasis Skyport 15 min 3 hours
Oasis Crystalium 15 min 1.5 hours
Crystalium Oasis 10 min 1.5 hours
Skyport Nexus 15 min 4 hours
Nexus Skyport 10 min 4 hours

EXAMPLES

["Crystalium"], "10:00" -> should return "12:15"
Skyport -> Crystalium: 15 minutes + 2 hours -> 12:15
 
["Crystalium", "Skyport", "Oasis"], "10:00" -> should return "17:45" 
Skyport -> Crystalium: 15 minutes + 2 hours -> 12:15
Crystalium -> Skyport: 10 minutes + 2 hours -> 14:25
Skyport -> Oasis: 20 minutes + 3 hours -> 17:45
 
["Nexus", "Skyport", "Oasis"], "21:30"  -> should return "09:15" 
Skyport -> Nexus: 15 minutes + 4 hours -> 02:45
Nexus -> Skyport: 10 minutes + 4 hours -> 06:55
Skyport -> Oasis: 20 minutes + 3 hours -> 09:15
 
["Skyport"], "22:00"  -> should return "23:00" 
Skyport -> Skyport: 1 hour wait -> 23:00
 
["Crystalium", "Nexus"], "12:00" -> should return "20:40"
Skyport -> Crystalium: 15 minutes + 2 hours -> 14:15
Crystalium -> Skyport: 10 minutes + 2 hours -> 16:25
Skyport -> Nexus: 15 minutes + 4 hours -> 20:40

SPECIAL CASES

  • If there are no destinations, return "The Train Driver has the day off".
  • The Train Driver always starts his shift at Skyport.
  • Whenever a train is taken, the waiting time must be added, even if it's the last destination.
  • If there is no direct connection to his next destination, The Train Driver must return to Skyport as SkyPort has connection to all the other cities and then continue their journey from there. The time it takes to return to Skyport and the waiting time also count.
  • If the next destination is the same city The Train Driver is already at, they must wait an hour before continuing.
  • The format that will be returned as a result is "HH:mm". Returning "8:00" is not a valid format but "08:00" is.
Code
Diff
  • import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Kata {
      
    	public static String arrivalTime(final String[] route, final String departureTime) {
    		if (route.length == 0)
    			return "The Train Driver has the day off";
    
    		LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
    		String origin = "Skyport";
    		for (int i = 0; i < route.length; i++) {
    			String destination = route[i];
    
    			Map<String, double[]> destinationMap = tabla.get(origin);
    			if (destinationMap != null && destinationMap.containsKey(destination)) {
    				double[] times = destinationMap.get(destination);
    
    				timeResult = calculateTime(timeResult, times);
    				origin = destination;
    			} else if (origin.equals(destination)) {
    				timeResult = timeResult.plusHours(1);
    			} else {
    				String transfer = "Skyport";
    				double[] times = tabla.get(origin).get(transfer);
    
    				timeResult = calculateTime(timeResult, times);
    				origin = transfer;
    				i--;
    			}
    		}
    
    		return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
    	}
    	
    	private static Map<String, Map<String, double[]>> tabla = new HashMap<String, Map<String, double[]>>() {
    		{
    			addRecord("Skyport", "Crystalium", 15, 2);
    			addRecord("Skyport", "Oasis", 20, 3);
    			addRecord("Oasis", "Crystalium", 15, 1.5);
    			addRecord("Skyport", "Nexus", 15, 4);
    		}
    
    		private void addRecord(final String origin, final String destiny, final double minutesWait, final double hoursTravel) {
    			putIfAbsent(origin, new HashMap<>());
    			get(origin).put(destiny, new double[] { minutesWait, hoursTravel });
    
    			putIfAbsent(destiny, new HashMap<>());
    			get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel });
    		}
    	};
    
    	private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) {
    		int minutes = (int) times[0];
    		int remainingMinutes = (int) (times[1] * 60);
    
    		return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes);
    	}
    }
    • import java.time.LocalTime;
    • import java.time.format.DateTimeFormatter;
    • import java.util.HashMap;
    • import java.util.Map;
    • public class Kata {
    • public static String arrivalTime(final String[] route, final String departureTime) {
    • if (route.length == 0)
    • return "The Train Driver has the day off";
    • LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
    • String origin = "Skyport";
    • for (int i = 0; i < route.length; i++) {
    • String destination = route[i];
    • Map<String, double[]> destinationMap = tabla.get(origin);
    • if (destinationMap != null && destinationMap.containsKey(destination)) {
    • double[] times = destinationMap.get(destination);
    • timeResult = calculateTime(timeResult, times);
    • origin = destination;
    • } else if (origin.equals(destination)) {
    • timeResult = timeResult.plusHours(1);
    • } else {
    • String transfer = "Skyport";
    • double[] times = tabla.get(origin).get(transfer);
    • timeResult = calculateTime(timeResult, times);
    • origin = transfer;
    • i--;
    • }
    • }
    • return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
    • }
    • private static Map<String, Map<String, double[]>> tabla = new HashMap<String, Map<String, double[]>>() {
    • {
    • addRecord("Skyport", "Crystalium", 15, 2);
    • addRecord("Skyport", "Oasis", 20, 3);
    • addRecord("Oasis", "Crystalium", 15, 1.5);
    • addRecord("Skyport", "Nexus", 15, 4);
    • }
    • private void addRecord(final String origin, final String destiny, final double minutesWait,
    • final double hoursTravel) {
    • private void addRecord(final String origin, final String destiny, final double minutesWait, final double hoursTravel) {
    • putIfAbsent(origin, new HashMap<>());
    • get(origin).put(destiny, new double[] { minutesWait, hoursTravel });
    • putIfAbsent(destiny, new HashMap<>());
    • get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel });
    • }
    • };
    • private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) {
    • int minutes = (int) times[0];
    • int remainingMinutes = (int) (times[1] * 60);
    • return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes);
    • }
    • }