Kids-related intro
Whenever I take my kids to an amusement park, the kids get more nervous the closer we are to the park, with the nerve peak when we are in the parking lot and find no place to park. So I've decided I need a method to find the closest free parking space to minimise the time spent parking.
Technical intro
Imagine a representation of a parking lot like this one:
*****************
* *
* ··CC··C··CC·· *
* ************* *
* ··CCCCCCCCC·· *
* *
**********E******
In this representation a *
means a wall, a ·
a free parking space, a E
the entry point and a C
a car already parked. Every whitespace is a position the car to be parked can use to move around the parking lot. Now let's extend this concept to 3D to create a multi-level parking lot:
1st floor 2nd floor 3rd floor 4th floor
***************** ***************** ***************** *****************
* 1 * 2 * 3 * *
* CCCCCCCCCCCCC * * CCCCCCCCCCCCC * * ····C··CCCCCC * * ······C······ *
* ************* * * ************* * * ************* * * ************* *
* CCCCCCCCCCCCC * * CCCCCCCCCCCCC * * ···CCCCCCCCCC * * ··C·······C·· *
* * * 1 * 2 * 3
**********E****** ***************** ***************** *****************
The numbers 1
, 2
and 3
represent the connections between levels. The 1
from the first floor connects with the 1
in the second floor so a car stepping into the 1
position in the first floor appears in the 1
position in the second floor.
Challenge
Giving a scheme of a parking lot like the previously shown, write the shortest program that calculates the distance to the nearest free parking space, according to the following
Rules
- The input will be a 3D char array or a 2D string array or equivalent, and the output will be a single integer representing the number of steps the car must take to get to the nearest free parking space. If you receive a 3D char array the first index may represent the floor number and the second and third indices the (x,y) position for each floor, but this is up to you.
- There won't be more than 9 ramps, represented by
[1-9]
. - The car starts from the
E
position (there will be only one entry point per map) and moves around using the whitespaces in one of four directions each time: up, down, left, right. The car can also step into·
positions and[1-9]
positions. - Every change of position (step) counts as 1, and every time the car goes from one floor to another counts as 3 as the car must take a ramp. In this case, the movement from a whitespace beside a
1
to the1
itself is what counts as 3 steps, because as a result of this movement the car appears in the1
position on the other floor. - The car can't go beyond the matrix limits.
- The count will end when the car to be parked is in the same position as a
·
. If there are no reachable free parking spaces you can return zero, a negative integer, a null value or an error.
Examples
In the example above the result would be 32, as it is cheaper to go to the fourth floor and park in the closest parking space near the 3
. The nearest free parking spaces in the third floor are at a distance of 33 and 34.
Other examples:
1st floor 2nd floor 3rd floor 4th floor
***************** ***************** ***************** *****************
* 1 * 2 * 3 * *
* CCCCCCCCCCCCC * * CCCCCCCCCCCCC * * ····C··CCCCCC * * ······C······ *
* ************* * * ************* * * ************* * * ************* *
* CCCCCCCCCCCCC * * ·CCCCCCCCCCCC * * ···CCCCCCCCCC * * ··C·······C·· *
* * * 1 * 2 * 3
**********E****** ***************** ***************** *****************
Answer: 28 (now the parking space in the 2nd floor is closer)
1st floor 2nd floor 3rd floor 4th floor
***************** ***************** ***************** *****************
* 1 4 2 5 3 6 *
* CCCCCCCCCCCCC * * CCCCCCCCCCCCC * * ····C··CCCCCC * * ······C······ *
* ************* * * ************* * * ************* * * ************* *
* CCCCCCCCCCCCC * * CCCCCCCCCCCCC * * ···CCCCCCCCCC * * ··C·······C·· *
4 * 5 1 6 2 * 3
**********E****** ***************** ***************** *****************
Answer: 24 (now it's better to go to ramp 4 and then to ramp 5 to the third floor)
1st floor 2nd floor 3rd floor 4th floor
***************** ***************** ***************** *****************
* 1 * * * 3 * 2
* CCCCCCCCCCCCC * * CCCCCCCCCCCCC * * ····C··CCCCCC * * ······C······ *
* ************* * * ************* * * ************* * * ************* *
* CCCCCCCCCCCCC * * ·CCCCCCCCCCCC * * ···CCCCCCCCCC * * ··C·······C·· *
* * * 3 * 2 * 1
**********E****** ***************** ***************** *****************
Answer: 16 (now the parking space in the 4th floor is closer)
1st floor 2nd floor 3rd floor 4th floor 5th floor
************ ************ ************ ************ ************
*CCCCCCCCC 1 *CCCCCCCCC 2 *CCCCCCCCC 3 *·CCCCCCCC 4 *········C *
* * * * * * * * * *
*CCCCCCCCC E *CCCCCCCCC 1 *CCCCCCCCC 2 *··CCCCCCC 3 *·······CC 4
************ ************ ************ ************ ************
Answer: 29 (both the nearest parking spaces at the 4th and 5th floors are at the same distance)
1st floor 2nd floor 3rd floor
************ ************ ************
*CCCCCCCCC 1 *CCCCCCCCC 2 *CCCCCCCCC *
* * * * * *
*CCCCCCCCC E *CCCCCCCCC 1 *CCCCCCCCC 2
************ ************ ************
Answer: -1 (no free parking space)
1st floor
************
* *
* *
* E*
************
Answer: -1 (no parking space at all)
1st floor
************
* ····· *
*· ****
* ····· * E
*********
Answer: -1 (the parking lot designer was a genius)
Alternatives
- You can use whatever characters you want to represent the parking lot map, just specify in your answer which are your chosen characters and what they mean.
This is code-golf, so may the shortest program/method/lambda/whatever for each language win!
If you need help with the algorithm, please check my (ungolfed) implementation in C#.