Ad

Trains and Trails

It's been a while since you saw your friends and you are eager to meet them.
You all agreed to meet at a point, but each one of your friends could arrive at a different station.

You've arrived early, so you are waiting for your friends to come.
Since you are impatient, and have nothing better to do, you try to guess the place and order in which your friends will arrive, if they do.

You know each one of them is in a different train, labeled from A to Z.

So you take a look at the map:

A----\              1
      ------\
    B--¡------------2
C-------)    ---/
    D-------/       3

The map leyend reads:

Character Description Time Value
0-9 Different stations
A-Z Different trains your friends are in
) Indicates a tunel. The trains that enter it will be lost
- Indicates a rail for the train to move forward 1
\ Indicates a turn. The train will move down and forward 2
/ Indicates a turn. The train will move up and forward 2
¡ Indicates a traffic light. The train will wait 1 turn and continue 3

In this example we can see that all the trains will arrive at station 2. So the trains there will be :

  • Train A takes 21 units of time to arrive
  • Train B takes 16 units of time to arrive
  • Train D takes 17 units of time to arrive
  • Train C goes into a tunnel so we dont count it

Having this in mind, our result will be:

{
	 '2' : ( 'B', 'D', 'A' )
}
In java the result is a Map\<Character,List\<Character\>\>

Edge cases:

  • If 2 trains take the same time to arrive at the station, they should be ordered alphaberically
  • Collisions between trains do not affect them
  • If the trains get out of the map, dont count them

Trains and Trails (RENFE)

It's been a while since you saw your friends and you are eager to meet them.
You all agreed to meet at a point, but each one of your friends could arrive at a different station.

You've arrived early, so you are waiting for your friends to come.
Since you are impatient, and have nothing better to do, you try to guess the place and order in which your friends will arrive, if they do.
You know each one of them is in a different train, labeled from A to Z.

So you take a look at the map:

A----\              1
      ------\
    B--¡------------2
C-------)    ---/
    D-------/       3

The map leyend reads:

Character Description Time Value
0-9 Different stations
A-Z Different trains your friends are in
) Indicates a tunel. The trains that enter it will be lost
- Indicates a rail for the train to move forward 1
\ Indicates a turn. The train will move down and forward 2
/ Indicates a turn. The train will move up and forward 2
¡ Indicates a traffic light. The train will wait 1 turn and continue 3

In this example we can see that all the trains will arrive at station 2. So the trains there will be :

  • Train A takes 21 units of time to arrive
  • Train B takes 16 units of time to arrive
  • Train D takes 17 units of time to arrive
  • Train C goes into a tunnel so we dont count it

Having this in mind, our result will be ( dont return the empty stations ):

{
	 '2' : ( 'B', 'D', 'A' )
}
In java the result is a Map\<Character,List\<Character\>\>

Edge cases:

  • If 2 trains take the same time to arrive at the station, they should be ordered alphaberically
  • If not a single train arrives at the station, the list should be empty
  • Collisions between trains do not affect them
  • The trains dont get out of the map
Code
Diff
  • import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class TrainsAndRailwaysv2 {
    
    	public static Map<Character, List<Character>> travel(char[][] tracks) {
    		Map<Character,List<Map.Entry<Character,Integer>>> result = new java.util.HashMap<>();
    		for (int row = 0; row < tracks.length; row++) {
    			for (int column = 0; column < tracks[row].length; column++) {
    				if (isTrain(tracks[row][column])) {
    					Map.Entry<Character, Integer> train = new java.util.AbstractMap.SimpleEntry(tracks[row][column],0);
    					goOnTraveling(tracks, row, column+1,train, result);
    				}
    			}
    		}
    		Map<Character,List<Character>> sortedShipOnStations = new java.util.HashMap<>();
    		result.forEach((k,v) -> sortedShipOnStations.put(k,sortTrains(v)));
    		
        return sortedShipOnStations;
    	}
    
    	private static boolean isTrain(char piece) {
    		return piece >= 65 && piece <= 90;
    	}
    	
    	private static boolean isStation (char piece) {
    		return piece >= 48 && piece <= 57;
    	}
    
    	private static void goOnTraveling(char[][] tracks, int row, int column, Entry<Character, Integer> train, Map<Character, List<Map.Entry<Character, Integer>>> result) {
    		while (column < tracks[row].length) {
    			switch (tracks[row][column]) {
    				case '¡': {
    					train.setValue(train.getValue()+2);
    					column++;
    					break;
    				}
    				case '-': {
    					column++;
    					break;
    				}
    				case '/': {
    					train.setValue(train.getValue()+1);
    					row--;
    					break;
    				}
    				case '\\': {
    					train.setValue(train.getValue()+1);
    					row++;
    					break;
    				}
    				case ')': {
    					return;
    				}
    				default: {
    					if (isStation(tracks[row][column])) {
    						result.putIfAbsent(tracks[row][column], new java.util.LinkedList<Map.Entry<Character,Integer>>());
    						result.get(tracks[row][column]).add(train);
    					}
    					return;
    				}
    			}
    			train.setValue(train.getValue()+1);
    		}
    	}
    	
    	private static List<Character> sortTrains (List<Entry<Character, Integer>> stationArrivals) {
    		List<Character> orderedTrains = new java.util.LinkedList<>();
    		stationArrivals.forEach(e -> orderedTrains.add(e.getKey()));
    		return stationArrivals.stream()
    		.sorted((entry1,entry2) -> 
    		Integer.compare(entry1.getValue(), entry2.getValue()))
    		.map(Entry::getKey)
    		.toList();
    	}
    } 
    
    • import java.util.List;
    • import java.util.Map;
    • import java.util.Map.Entry;
    • public class TrainsAndRailwaysv2 {
    • public static Map<Character, List<Character>> travel(char[][] tracks) {
    • Map<Character,List<Map.Entry<Character,Integer>>> result = new java.util.HashMap<>();
    • for (int row = 0; row < tracks.length; row++) {
    • for (int column = 0; column < tracks[row].length; column++) {
    • if (isTrain(tracks[row][column])) {
    • Map.Entry<Character, Integer> train = new java.util.AbstractMap.SimpleEntry(tracks[row][column],0);
    • goOnTraveling(tracks, row, column+1,train, result);
    • }
    • }
    • }
    • Map<Character,List<Character>> sortedShipOnStations = new java.util.HashMap<>();
    • result.forEach((k,v) -> sortedShipOnStations.put(k,sortTrains(v)));
    • return sortedShipOnStations;
    • return sortedShipOnStations;
    • }
    • private static boolean isTrain(char piece) {
    • return piece >= 65 && piece <= 90;
    • }
    • private static boolean isStation (char piece) {
    • return piece >= 48 && piece <= 57;
    • }
    • private static void goOnTraveling(char[][] tracks, int row, int column, Entry<Character, Integer> train, Map<Character, List<Map.Entry<Character, Integer>>> result) {
    • while (column < tracks[row].length) {
    • switch (tracks[row][column]) {
    • case '¡': {
    • train.setValue(train.getValue()+2);
    • column++;
    • break;
    • }
    • case '-': {
    • column++;
    • break;
    • }
    • case '/': {
    • train.setValue(train.getValue()+1);
    • row--;
    • break;
    • }
    • case '\\': {
    • train.setValue(train.getValue()+1);
    • row++;
    • break;
    • }
    • case ')': {
    • return;
    • }
    • default: {
    • if (isStation(tracks[row][column])) {
    • result.putIfAbsent(tracks[row][column], new java.util.LinkedList<Map.Entry<Character,Integer>>());
    • result.get(tracks[row][column]).add(train);
    • }
    • return;
    • }
    • }
    • train.setValue(train.getValue()+1);
    • }
    • }
    • private static List<Character> sortTrains (List<Entry<Character, Integer>> stationArrivals) {
    • List<Character> orderedTrains = new java.util.LinkedList<>();
    • stationArrivals.forEach(e -> orderedTrains.add(e.getKey()));
    • return stationArrivals.stream()
    • .sorted((entry1,entry2) ->
    • Integer.compare(entry1.getValue(), entry2.getValue()))
    • .map(Entry::getKey)
    • .toList();
    • }
    • }
    • }

Trains and Trails (RENFE)

It's been a while since you saw your friends and you are eager to meet them.
You all agreed to meet at a point, but each one of your friends could arrive at a different station.

You've arrived early, so you are waiting for your friends to come.
Since you are impatient, and have nothing better to do, you try to guess the place and order in which your friends will arrive, if they do.

You know each one of them is in a different train, labeled from A to Z.

So you take a look at the map:

A----\              1
      ------\
    B--¡------------2
C-------)    ---/
    D-------/       3

The map leyend reads:

Character Description Time Value
0-9 Different stations
A-Z Different trains your friends are in
) Indicates a tunel. The trains that enter it will be lost
- Indicates a rail for the train to move forward 1
\ Indicates a turn. The train will move down and forward 2
/ Indicates a turn. The train will move up and forward 2
¡ Indicates a traffic light. The train will wait 1 turn and continue 3

In this example we can see that all the trains will arrive at station 2. So the trains there will be :

  • Train A takes 21 units of time to arrive
  • Train B takes 16 units of time to arrive
  • Train D takes 17 units of time to arrive
  • Train C goes into a tunnel so we dont count it

Having this in mind, our result will be:

{
	 '2' : ( 'B', 'D', 'A' )
}
In java the result is a Map\<Character,List\<Character\>\>

Edge cases:

  • If 2 trains take the same time to arrive at the station, they should be ordered alphaberically
  • Collisions between trains do not affect them
  • If the trains get out of the map, dont count them
Code
Diff
  • import java.util.List;
    import java.util.Map;
    //Added description
    public class TrainsAndRailwaysv2 {
    
    	public static Map<Character, List<Character>> travel(char[][] tracks) {
        return null;  
      }
    }
    • import java.util.List;
    • import java.util.Map;
    • //Added description
    • public class TrainsAndRailwaysv2 {
    • public static Map<Character, List<Character>> travel(char[][] tracks) {
    • return null;
    • }
    • }
Code
Diff
  • let isPerfectSquare: (Int) -> Bool = { sqrt(Double($0)).truncatingRemainder(dividingBy: 1) == 0 }
    • func isPerfectSquare(_ input: Int) -> Bool {
    • let inputAsDouble = Double(input)
    • return inputAsDouble.squareRoot().rounded() == inputAsDouble.squareRoot()
    • }
    • let isPerfectSquare: (Int) -> Bool = { sqrt(Double($0)).truncatingRemainder(dividingBy: 1) == 0 }
Code
Diff
  • class Solution{
      static boolean isPerfectSquare(int num){ return Math.sqrt(num) % 1 == 0; }
    }
    • func isPerfectSquare(_ input: Int) -> Bool {
    • let inputAsDouble = Double(input)
    • return inputAsDouble.squareRoot().rounded() == inputAsDouble.squareRoot()
    • class Solution{
    • static boolean isPerfectSquare(int num){ return Math.sqrt(num) % 1 == 0; }
    • }