14
\$\begingroup\$

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 the 1 itself is what counts as 3 steps, because as a result of this movement the car appears in the 1 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 , 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#.

\$\endgroup\$
  • \$\begingroup\$ Related. \$\endgroup\$ – Charlie Jul 20 '18 at 10:05
  • \$\begingroup\$ Since we're apparently dealing with crazy parking lot designers, may a ramp go directly from, say, 1st floor to 3rd floor? I think you should either add such a test case (which could be pretty fun) or explain that it's not going to happen. \$\endgroup\$ – Arnauld Jul 20 '18 at 12:39
  • 2
    \$\begingroup\$ @Arnauld added. A difficult task from the mobile app... \$\endgroup\$ – Charlie Jul 20 '18 at 12:51
  • 1
    \$\begingroup\$ -1 no colorful blocks or kid's stories this time \$\endgroup\$ – Luis Mendo Jul 20 '18 at 13:43
  • 1
    \$\begingroup\$ @mazzy I meant that adding a new test case to the text of the question was a difficult task using the limited UI of the Stack Exchange mobile app... \$\endgroup\$ – Charlie Jul 25 '18 at 13:28
6
\$\begingroup\$

JavaScript (ES6), 199 bytes

Takes input as an array of arrays of arrays of characters, using the characters described in the challenge. Returns \$0\$ if there's no free parking space.

a=>(m=g=(c,t,f,x,y,F=f||a.find(r=>r.some((a,i)=>~(y=i,x=a.indexOf(c)))),r=F[y])=>r&&(c=r[x])&&(r[x]=g,c>'z'?m=m<t?m:t:!f|c<1?[0,-1,0,1].map((d,i)=>g(0,-~t,F,x+d,y+~-i%2)):+c&&g(c,t+2),r[x]=c))('E')|m

Try it online!

How?

The recursive function g() takes as input:

  • c : the character of the entry point we're looking for if we've just left a floor or we are at the very beginning of the process; 0 otherwise
  • t : the total number of moves so far (initially undefined)
  • f, x, y : a pointer to the current floor and the current coordinates in this floor; they are all undefined if we're looking for a new floor

If f is defined, we simply copy it to the current floor F. Otherwise, we need to look for the new floor and the new coordinates by iterating over each floor and over each row until we find the entry point c:

F = f || a.find(r => r.some((a, i) => ~(y = i, x = a.indexOf(c))))

We define r as the current row in the current floor:

r = F[Y]

The next step is to make sure that the current cell c at (x, y) is defined:

r && (c = r[x]) && ...

If it is, we mark it as visited by setting it to g, a value that does not trigger any of the forthcoming tests:

r[x] = g

If c is a free parking space, we stop the recursion. And if the total number of moves is lower than our previous best score, we update m accordingly:

c > 'z' ? m = m < t ? m : t : ...

If we've just reached a new floor (f is undefined) or c is a space, we process a recursive call for each surrounding cell:

!f | c < 1 ?
  [0, -1, 0, 1].map((d, i) =>
    g(0, -~t, F, x + d, y + ~-i % 2)
  )
:
  ...

Otherwise, if c is a ramp marker (i.e. a non-zero digit), we process a single recursive call to reach the new floor:

+c && g(c, t + 2)

Finally, we restore the current cell to its initial value so that it can be visited again in other recursion paths:

r[x] = c
\$\endgroup\$
3
\$\begingroup\$

Kotlin, 768 bytes

used period . instead of ·. Wish I understood Arnauld's answer because losing 500 bytes would be nice.

fun s(l:List<List<CharArray>>)={val b=listOf(l.size-1,l[0].size-1,l[0][0].size-1)
val e=Int.MAX_VALUE
var r=e
var t=listOf(e,e,e)
val d=Array(b[0]+1){Array(b[1]+1){Array(b[2]+1){e}}}
val f={c:Char,n:List<Int>->var a=t
(0..b[0]).map{f->(0..b[1]).map{r->(0..b[2]).map{var s=listOf(f,r,it)
if(l[f][r][it]==c&&!s.equals(n))a=s}}}
a}
fun m(p:List<Int>,c:Int){if(p[0]in 0..b[0]&&p[1]in 0..b[1]&&p[2]in 0..b[2]&&d[p[0]][p[1]][p[2]]>c){d[p[0]][p[1]][p[2]]=c
val h=l[p[0]][p[1]][p[2]]
when(h){' ','E'->(-1..1 step 2).map{m(listOf(p[0],p[1]+it,p[2]),c+1)
m(listOf(p[0],p[1],p[2]+it),c+1)}
'.'->if(r>c)r=c
in '1'..'9'->{val n=f(h,p)
d[n[0]][n[1]][n[2]]=c+2
(-1..1 step 2).map{m(listOf(n[0],n[1]+it,n[2]),c+3)
m(listOf(n[0],n[1],n[2]+it),c+3)}}}}}
m(f('E',t),0)
if(r<e)r
else-1}()

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Powershell, 299 292 bytes

It is assumed that the map is rectangle.

It uses x instead ·. To get ·, you need to save script as ASCII (not UTF-8) and replace x on ·.

filter f{$l=($_-split"
")[0].length
$p,$d,$e='x','\d',' '|%{"$_(?=E)|(?<=E)$_|$_(?=[\s\S]{$l}E)|(?<=E[\s\S]{$l})$_"}
for($r=1;$_-notmatch$p;$r++){$m=$_-replace'F','E'-replace'G','F'-replace'H','G'
if($m-match$d){$m=$m-replace$Matches[0],'H'}$m=$m-replace$e,'E'
if($m-eq$_){return -1}$_=$m}$r}

Ungolfed and test script:

filter f{
    #Write-Host "`nStep:`n$_" # uncomment this to display each step

    $l = ($_ -split "`n")[0].length
    $p,$d,$e = 'x', '\d', ' '| % {"$_(?=E)|(?<=E)$_|$_(?=[\s\S]{$l}E)|(?<=E[\s\S]{$l})$_"}

    for($r = 1;$_ -notmatch $p;$r++) {
        $m = $_ -replace 'F', 'E' -replace 'G', 'F' -replace 'H', 'G'
        if ($m -match $d) {
            $m = $m -replace $Matches[0], 'H'
        }
        $m = $m -replace $e, 'E'

        if ($m -eq $_) {
            return -1
        }

        $_=$m
    }
    $r
}

@(
    , (2, @"
****
*x E
****
"@)
    , (1, @"
****
* xE
****
"@)
    , (1, @"
****
* x*
* E*
****
"@)
    , (1, @"
****
* E*
* x*
****
"@)
    , (-1, @"
****
2 E1
*  *
****
"@)
    , (28, @"
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*               *
**********E******
*****************
*               2
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               1
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               *
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               3
*****************
"@)
    , (16, @"
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*               *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************
"@)
    , (29, @"
************
*CCCCCCCCC 1
*          *
*CCCCCCCCC E
************
************
*CCCCCCCCC 2
*          *
*CCCCCCCCC 1
************
************
*CCCCCCCCC 3
*          *
*CCCCCCCCC 2
************
************
*xCCCCCCCC 4
*          *
*xxCCCCCCC 3
************
************
*xxxxxxxxC *
*          *
*xxxxxxxCC 4
************
"@
    )
    , (-1, @"
************
*          *
*          *
*         E*
************
"@)
    , (-1, @"
************
* xxxxx    *
*x      ****
* xxxxx * E 
*********   
"@)
) | % {
    $e, $m = $_
    $r = $m|f
    "$($e-eq$r): $r $e"
}

Output:

True: 2 2
True: 1 1
True: 1 1
True: 1 1
True: -1 -1
True: 28 28
True: 16 16
True: 29 29
True: -1 -1
True: -1 -1

Extended output for parking with 16 steps:

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*               *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*         E     *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*        EEE    *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*       EEEEE   *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*      EEEEEEE  *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*     EEEEEEEEE *
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCC *
*    EEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* ************* *
* CCCCCCCCCCCCCE*
*   EEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCC *
* *************E*
* CCCCCCCCCCCCCE*
*  EEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*               1
* CCCCCCCCCCCCCE*
* *************E*
* CCCCCCCCCCCCCE*
* EEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*              E1
* CCCCCCCCCCCCCE*
* *************E*
* CCCCCCCCCCCCCE*
*EEEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               1
*****************

Step:
*****************
*             EEH
* CCCCCCCCCCCCCE*
* *************E*
*ECCCCCCCCCCCCCE*
*EEEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               H
*****************

Step:
*****************
*            EEEG
* CCCCCCCCCCCCCE*
*E*************E*
*ECCCCCCCCCCCCCE*
*EEEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               G
*****************

Step:
*****************
*           EEEEF
*ECCCCCCCCCCCCCE*
*E*************E*
*ECCCCCCCCCCCCCE*
*EEEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*               F
*****************

Step:
*****************
*E         EEEEEE
*ECCCCCCCCCCCCCE*
*E*************E*
*ECCCCCCCCCCCCCE*
*EEEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxx *
*              EE
*****************

Step:
*****************
*EE       EEEEEEE
*ECCCCCCCCCCCCCE*
*E*************E*
*ECCCCCCCCCCCCCE*
*EEEEEEEEEEEEEEE*
**********E******
*****************
*               *
* CCCCCCCCCCCCC *
* ************* *
* xCCCCCCCCCCCC *
*               3
*****************
*****************
*               3
* xxxxCxxCCCCCC *
* ************* *
* xxxCCCCCCCCCC *
*               2
*****************
*****************
*               2
* xxxxxxCxxxxxx *
* ************* *
* xxCxxxxxxxCxxE*
*             EEE
*****************
True: 16 16

Explanation

It's kind of Lee pathfinding algorithm. Only one smart thing: 3 steps on ramp are realized as a dummy states H->G->F->E


Powershell for a genius parking lot designer, 377 369 bytes

Parking design is an 2D string array. No assumtions about map: any length strings, floors without walls, parking without an entry point, multi-floor-and-multi-exit-ramps. Genus cost is +26%.

filter f{for($r=1;$_-notmatch$p;$r++){$m=$_-replace'F','E'-replace'G','F'-replace'H','G'
if($m-match$d){$m=$m-replace$Matches[0],'H'}$m=$m-replace$e,'E'
if($m-eq$_){return-1}$_=$m}$r}$g={$l=($args|%{$_|%{$_.length}}|sort)[-1]
$p,$d,$e='x','\d',' '|%{"$_(?=E)|(?<=E)$_|$_(?=[\s\S]{$l}E)|(?<=E[\s\S]{$l})$_"}
(($args|%{$_.PadRight($l,'*')-join"
"})-join"
"+'-'*$l+"
")|f}

Ungolfed and test script:

filter f{
    #Write-Host "`nStep:`n$_" # uncomment this to display each step

    for($r = 1;$_ -notmatch $p;$r++) {
        $m = $_ -replace 'F', 'E' -replace 'G', 'F' -replace 'H', 'G'
        if ($m -match $d) {
            $m = $m -replace $Matches[0], 'H'
        }
        $m = $m -replace $e, 'E'

        if ($m -eq $_) {
            return -1
        }

        $_=$m
    }
    $r
}

$g = {

    $l = ($args| % {$_| % {$_.length}}|sort)[-1]
    $p,$d,$e = 'x', '\d', ' '| % {"$_(?=E)|(?<=E)$_|$_(?=[\s\S]{$l}E)|(?<=E[\s\S]{$l})$_"}
    (($args| % {$_.PadRight($l, '*') -join "`n"}) -join "`n"+'-' * $l+"`n")|f

}


@(
    , (2, @(, (
                "****",
                "*x E",
                "****"
            )))
    , (1, @(, (
                "****",
                "* xE",
                "****"
            )))
    , (1, @(, (
                "****",
                "* x*",
                "* E*",
                "****"
            )))
    , (1, @(, (
                "****",
                "* E*",
                "* x*",
                "****"
            )))
    , (-1, @(, (
                "****",
                "2 E1",
                "*  *",
                "****"
            )))
    , (28, @(, (
                "*****************",
                "*               1",
                "* CCCCCCCCCCCCC *",
                "* ************* *",
                "* CCCCCCCCCCCCC *",
                "*               *",
                "**********E******"
            ), (
                "*****************",
                "*               2",
                "* CCCCCCCCCCCCC *",
                "* ************* *",
                "* xCCCCCCCCCCCC *",
                "*               1",
                "*****************"
            ), @(
                "*****************",
                "*               3",
                "* xxxxCxxCCCCCC *",
                "* ************* *",
                "* xxxCCCCCCCCCC *",
                "*               2",
                "*****************"
            ), @(
                "*****************",
                "*               *",
                "* xxxxxxCxxxxxx *",
                "* ************* *",
                "* xxCxxxxxxxCxx *",
                "*               3",
                "*****************"
            )))
    , (16, @(, (
                "*****************",
                "*               1",
                "* CCCCCCCCCCCCC *",
                "* ************* *",
                "* CCCCCCCCCCCCC *",
                "*               *",
                "**********E******"
            ), @(
                "*****************",
                "*               *",
                "* CCCCCCCCCCCCC *",
                "* ************* *",
                "* xCCCCCCCCCCCC *",
                "*               3",
                "*****************"
            ), @(
                "*****************",
                "*               3",
                "* xxxxCxxCCCCCC *",
                "* ************* *",
                "* xxxCCCCCCCCCC *",
                "*               2",
                "*****************"
            ), @(
                "*****************",
                "*               2",
                "* xxxxxxCxxxxxx *",
                "* ************* *",
                "* xxCxxxxxxxCxx *",
                "*               1",
                "*****************"
            )))
    , (29, @(, (
                "************",
                "*CCCCCCCCC 1",
                "*          *",
                "*CCCCCCCCC E",
                "************"
            ), @(
                "************",
                "*CCCCCCCCC 2",
                "*          *",
                "*CCCCCCCCC 1",
                "************"
            ), @(
                "************",
                "*CCCCCCCCC 3",
                "*          *",
                "*CCCCCCCCC 2",
                "************"
            ), @(
                "************",
                "*xCCCCCCCC 4",
                "*          *",
                "*xxCCCCCCC 3",
                "************"
            ), @(
                "************",
                "*xxxxxxxxC *",
                "*          *",
                "*xxxxxxxCC 4",
                "************"
            )))
    , (-1, @(, (
                "************",
                "*          *",
                "*          *",
                "*         E*",
                "************"
            )))
    , (-1, @(, (
                "************",
                "* xxxxx    *",
                "*x      ****",
                "* xxxxx * E",
                "*********"
            )))
) | % {
    $e, $m = $_
    $r = &$g @m
    "$($e-eq$r): $r $e"
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.