Ad
Algorithms
Data Structures
Simulation
Logic
Games
Date Time
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";
    • for (int i = 0; i < route.length; i++) {
    • String destination = route[i];
    • int index = 0;
    • while(index < route.length) {
    • String destination = route[index];
    • Map<String, double[]> destinationMap = tabla.get(origin);
    • 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 = tabla.get(origin).get(transfer);
    • double[] times = tableTime.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[]>>() {
    • 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);
    • }
    • }

Pepe is a train driver who works with trains all over Spain. Sometimes, Pepe has very long trips and does not know when his shift will end. That is why it is your job to figure out at what time he will arrive at his final destination, taking into account that he has to wait some time whenever he reaches his destination.

Sometimes, the machine that prints the itinerary fails and gives Pepe an empty list or a list with two or more identical cities in a row. If he gets an empty list, Pepe will return home and end his shift and if the next destination is the same city he is currently in, he will have to wait there for an hour to complain to his superior.

ORIGIN DESTINATION STANDBY TIME TRIP DURATION
Madrid Barcelona 15 min 2 hours
Barcelona Madrid 10 min 2 hours
Madrid Valencia 20 min 3 hours
Valencia Madrid 15 min 3 hours
Valencia Barcelona 15 min 1.5 hours
Barcelona Valencia 10 min 1.5 hours
Madrid Cádiz 15 min 4 hours
Cádiz Madrid 10 min 4 hours

You will be provided with a list of Pepe's destinations in order and the departure time from his starting station in Madrid.
Your task is to write a program that, using this information, calculates at what time Pepe will arrive his final destination.

EXAMPLES

["Barcelona"], "10:00" -> should return "12:15"
["Barcelona", "Madrid", "Valencia"], "10:00" -> should return "17:45" 
["Cadiz", "Madrid", "Valencia"], "21:30"  -> should return "09:15" 
["Madrid"], "22:00"  -> should return "23:00" 
["Barcelona", "Cadiz"], "12:00" -> should return "20:40"

SPECIAL CASES

  • If there are no destinations, return "Pepe has the day off".
  • Pepe always starts his shift at Madrid.
  • Whenever a train is taken, the waiting time must be added, even if it's the last trip.
  • If there is no direct connection to his next destination, Pepe must return to Madrid and then continue his journey. The time it takes to return to Madrid and the waiting time also count towards the final result.
  • The format that will be returned as a result is "HH:mm". Returning "8:00" is not a valid format but "08:00" is.
  • Cities passed by parameter do not have accents (Cadiz and not Cádiz).
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 "Pepe has the day off";

		LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
		String origin = "Madrid";
		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 transbordo = "Madrid";
				double[] times = tabla.get(origin).get(transbordo);

				timeResult = calculateTime(timeResult, times);
				origin = transbordo;
				i--;
			}
		}

		return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
	}

	
	private static Map<String, Map<String, double[]>> tabla = new HashMap<String, Map<String, double[]>>() {
		{
			addRecord("Madrid", "Barcelona", 15, 2);
			addRecord("Madrid", "Valencia", 20, 3);
			addRecord("Valencia", "Barcelona", 15, 1.5);
			addRecord("Madrid", "Cadiz", 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 hours = (int) times[1];
		int remainingMinutes = (int) ((times[1] - hours) * 60);

		return currentTime.plusMinutes(minutes).plusHours(hours).plusMinutes(remainingMinutes);
	}
}