9
\$\begingroup\$

The Challenge

Given either a string (may have newlines), or a two dimensional array, and a positive integer n, output the position of the platforms n turns after the initial position.


U, D, R, L are platforms.

^, v, >, < are arrows that change the directions of the platforms.

U, D, R, L move up, down, right and left, respectively. When an arrow is in front of a platform, it changes the direction.

Affects platform:

R<

D
^

v
U

>L

>L
 <

(top arrow will affect top L, but bottom arrow won't affect top L)

Won't affect:

 <
R

>
 L

v
 U

D
 ^

<R

(R is going right, so < won't affect the R)


For example, if this was the string:

>R   <

The platform R would move right until it's nearly touching the arrow:

>   R<

After, it would change the direction and start going left:

>  R <

(even though now it's going left, the letter won't change.)

There are some cases when the platform won't move, such as

>R<

or

v
U
^

Last example:

v   >
D    Rv
   ^U
^    <

After one turn,

v   >
    U v
D  ^ R
^    <

After one turn,

v   >
D    Uv
   ^R
^    <

And one more turn:

v   >
    R v
D  ^ U
^    <

You can assume that the platforms, after n turns, won't overlap, that the platforms won't go out of bounds, and that a platform won't touch an arrow that's pointing to the same direction as the platform.


Test Cases

Input:
">R   <", 4
Output:
">  R <"

Input:
">R   <", 6
Output:
">R   <"

Input:
">R<", 29
Output:
">R<"

Input:
"v
 U
 ^", 5
Output:
"v
 U
 ^"

Input:
"v

 D
 ^", 1
Output:
"v
 D

 ^"

Input:
"v

 D
 ^", 4
Output:
"v

 D
 ^"

Input:
"v   >
 D    Rv
    ^U
 ^    < ", 2
Output:
"v   >
 D    Uv
    ^R
 ^    <

Input:
">RL<", 3
Output:
">LR<"

Input:
">L  R<", 4
Output:
"> RL <"

Input:
"> RR<
 >L  R <", 6
Ouput:
">RR <
 > RL  <"

Input:
"R   <", 4
Output:
"  R <"

Input:
"R   <", 6
Ouput:
"R   <"

Rules

  • This is , so shortest answer in bytes wins!
  • Standard loopholes are disallowed.
\$\endgroup\$
  • 3
    \$\begingroup\$ @closevoters: what is unclear about this challenge? \$\endgroup\$ – Leaky Nun Aug 2 '16 at 18:33
  • \$\begingroup\$ Time to make an esoteric programming language based on this. \$\endgroup\$ – DanTheMan Aug 3 '16 at 1:00
  • \$\begingroup\$ Also, what happens if a platform goes off of the grid? \$\endgroup\$ – Quelklef Aug 4 '16 at 7:20
  • \$\begingroup\$ @Quelklef you can assume that the platforms won't go off of the grid after n turns. \$\endgroup\$ – acrolith Aug 4 '16 at 15:42
2
\$\begingroup\$

C#, 1245 bytes

(i,n)=>{string q="RlherLHEfquDFQUd",D="><v^",H="Rlhe",E="LrHE",X="Dufq",Y="UdFQ",S="#v<>";Func<string,char,int>I=(v,L)=>v.IndexOf(L);Func<char,int,char>z=(y,m)=>q[I(q,y)+m*4];Func<string,char,bool>_=(s,F)=>s.Contains(F);var g=((i as char[][])??((string)i).Split('\n').Select(f=>f.ToCharArray())).ToList();int w=g[0].Length,h=g.Count,u;g=g.Select((r,o)=>r.Select((t,p)=>'R'==t&&w>p+1&&0<=(u=I(D,r[p+1]))?z(t,u):'L'==t&&0<=p-1&&0<=(u=I(D,r[p-1]))?z(t,-1+u):'D'==t&&h>o+1&&0<=(u=I(D,g[o+1][p]))?z(t,-2+u):'U'==t&&0<=o-1&&0<=(u=I(S,g[o-1][p]))?z(t,-u):t).ToArray()).ToList();for(var j=0;j<n;j++){bool L,R,T,B;g=g.Select((r,o)=>r.Select((t,p)=>_(D,t)?t:(R=0<=p-1)&&_(H,r[p-1])?w>p+1&&0<=(u=I(D,r[p+1]))?z(r[p-1],u):r[p-1]:(L=w>p+1)&&_(E,r[p+1])?0<=p-1&&0<=(u=I(D,r[p-1]))?z(r[p+1],-1+u):r[p+1]:(B=0<=o-1)&&_(X,g[o-1][p])?h>o+1&&0<=(u=I(D,g[o+1][p]))?z(g[o-1][p],-2+u):g[o-1][p]:(T=h>o+1)&&_(Y,g[o+1][p])?0<=o-1&&0<=(u=I(S,g[o-1][p]))?z(g[o+1][p],-u):g[o+1][p]:(L&&_(H,t)&&!_(D,r[p+1]))||(R&&_(E,t)&&!_(D,r[p-1]))||(B&&_(Y,t)&&!_(D,g[o-1][p]))||(T&&_(X,t)&&!_(D,g[o+1][p]))?' ':t).ToArray()).ToList();}return string.Join("\n",g.Select(s=>new string(s))).ToUpper().Replace("H","U").Replace("E","D").Replace("F","R").Replace("Q","L").Replace("V","v");};

It seemed simpler at first, but then I just kept writing more code. :D

LINQ to enumerate and update the board, changing the character to indicate the direction it's moving. The characters are reverted back before returning. Also assumes that the board is square (so had to modify some of the multi-line test cases to fit this restriction).

Expanded:

// Casts to Func<object, int, string> so as to accept both string and char[][] input
(i, n) =>{
    // Shorten constants/functions
    string q = "RlherLHEfquDFQUd", D = "><v^", H = "Rlhe", E = "LrHE", X = "Dufq", Y = "UdFQ",S="#v<>";
    Func<string, char, int> I = (v, L) => v.IndexOf(L);
    Func<char, int, char> z = (y, m) => q[I(q,y) + m * 4]; // Updates the direction of the platform
    Func<string, char, bool> _ = (s, F) => s.Contains(F);

    // Convert either string or char[][] input into common format
    var g = ((i as char[][]) ?? ((string)i).Split('\n').Select(f => f.ToCharArray())).ToList();

    // Get board dimensions
    int w = g[0].Length,h = g.Count,u;

    // Update platforms to reflect the direction they're initially moving
    g = g.Select((r, o) => r.Select((t, p) =>
        'R' == t &&w>p+1&&0<=(u=I(D,r[p+1]))?z(t,u):
        'L' == t &&0<=p-1&&0<=(u= I(D, r[p-1]))?z(t,-1+u):
        'D' == t &&h>o+1&&0<=(u= I(D, g[o+1][p]))?z(t,-2+u):
        'U' == t &&0<=o-1&&0<=(u= I(S,g[o-1][p]))?z(t,-u):t
    ).ToArray()).ToList();

    // Go through each timestep
    for (var j=0;j<n;j++)
    {
        bool L,R,T,B;
        g = g.Select((r, o) => r.Select((t, p) => 
            // Don't change <>^v characters
            _(D,t) ? t :

            // Move platforms going right
            (R=0 <= p - 1) && _(H,r[p-1]) ? w > p+1 && 0<=(u= I(D, r[p+1])) ? z(r[p-1],u) : r[p - 1] :

            // Move platforms going left
            (L=w > p + 1) && _(E,r[p+1]) ? 0 <= p-1 && 0<=(u= I(D, r[p-1])) ? z(r[p+1],-1+u) : r[p + 1] :

            // Move platforms going down
            (B=0 <= o - 1) && _(X,g[o-1][p]) ? h > o+1 && 0<=(u= I(D, g[o+1][p])) ? z(g[o - 1][p],-2+u) : g[o-1][p] :

            // Move platforms going up
            (T=h > o + 1) && _(Y,g[o+1][p]) ? 0<=o-1&&0<=(u= I(S, g[o-1][p]))?z(g[o + 1][p],-u) :g[o+1][p]:

            // Erase platforms that moved
            (L&&_(H,t)&&!_(D,r[p+1]))||
            (R&&_(E,t)&&!_(D,r[p-1]))||
            (B&&_(Y,t)&&!_(D,g[o-1][p]))||
            (T&&_(X,t)&&!_(D,g[o+1][p]))
            ? ' ':

            // Maintain whatever character this was
            t
            ).ToArray()).ToList();
    }

    // Replace direction characters with platform label and join into string return value.
    return string.Join("\n",g.Select(s=>new string(s))).ToUpper().Replace("H", "U").Replace("E", "D").Replace("F", "R").Replace("Q", "L").Replace("V", "v");
};
\$\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.