18
\$\begingroup\$

Do you ever look at 3D shapes and think that we have 2 many dimensions. So let's make 3D into 1D! (See what I did there?)

Task

Given a 2D representation of a cube (layout below), flatten it into a single string, based on pointers.

The CUBE

This is the layout of the cube filled with no-ops (,) and pointers at the corners (<>v^/). The Navigation Pointer (NP) begins the program where shown.

         | NP begins here, moving right.
         V
         >,,,,,,,,,,,,,,,,,,v
        ,                  ,,
       ,                  , ,
      ,                  ,  ,
     ,                  ,   ,
    ,                  ,    ,
   ,                  ,     ,
  ,                  ,      ,
 ,                  ,       ,
/,,,,,,,,,,,,,,,,,,/        /
,                  ,       ,
,                  ,      ,
,                  ,     ,
,                  ,    ,
,                  ,   ,
,                  ,  ,
,                  , ,
,                  ,,
^,,,,,,,,,,,,,,,,,,<

The NP continues moving right from the start until it meets a corner. Each corner has a default direction that, unless overridden at the corner by a pointer, will point the NP in a new direction. They are shown on the above diagram. The pointers in the program are v^<>/ (the / points it either down or up, due to the layout there will never be any confusion between the two)

The NP stops moving when it reaches the symbol (Yes I know this is a multi-byte character, deal with it). Each character it goes over is added to the final output string except for the pointers, the no-op (,) and the symbol.

Input

Input will be a multi-line string that matches the layout of the CUBE with the non-whitespace characters replaced with any character in the range 0x21 to 0x7E and at least one instance of the EOF.

You may take input as:

  • A multiline string
  • A list of strings where each line is a new element in the list
  • A 2D list of strings where each line is broken down into a list

Output

The cube, flattened down into a single line string.

Conditional Pointer

The NP has a boolean value that is used for something called a "conditional" pointer that begins at true.

The "conditional" pointer (?) is a pointer which will alternate the NP value between true and false every time it is used. Conditional pointers only take effects when used on the corners where 3 tracks (3-point corner) meet. If the value is true when the NP reaches the conditional pointer, the NP moves to the default direction in the CUBE image and, after being directed, the NP value is toggled. Therefore, if it is on false, the NP follows the non-default direction.

There will never be an instance where the NP goes towards a conditional and the default direction is the direction it just came from. Imagine that this is the left corner of the front face and the NP value is currently true:

      NP direction
      /
     ,
    ,
,,,?
   ,
   ,

This will never happen as the default direction will mirror it back to how it was.

If the conditional pointer is on an 3-point corner:

  • It isn't added to the final string
  • The NP value is toggled between true and false after the NP has been directed.
  • If the NP value is true, the NP is redirected to the default direction. Otherwise, it is redirected to the non-default direction.

If it is on a track or a 2-point corner:

  • It is added to the final string
  • the value is toggled
  • The IP isn't directed

Something to watch out for

If a pointer is on a track and it doesn't point into spaces, the NP is directed into the new track, adds the next character to the final string and rotates the NP by 90 degrees to the right.

 abcd
   >e
  g f
 /
NP direction

would flatten to

(...) gef (...)

NOT

(...) g>def (...)

A / pointer doesn't redirect the NP in this instance as it may redirect into a space so

     f
     e
> ab/d

would become ab/def

Rules

  • The width, height and depth of the cube may change
  • The height and depth of the cube will always be the same and will be half the width, so the width will always be even.
  • The width will be between (inclusive) 6 and 60.
  • There will always be an EOF () that can be reached by the NP. E.g there will never be something like this in the cube: <¶> as the NP would never be able to reach the cube.
  • As a space cannot be inserted into the cube's tracks, the final string shouldn't ever have a space in it.
  • If a pointer is placed in one of the tracks (not on a corner) and it would direct the NP into spaces, the NP stays constant and the character is added to the final string.
  • However, if the pointer keeps it on the track, the NP is directed and the pointer character isn't added
  • As no-ops (,) aren't added to the final string, the final string shouldn't contain any , in it.
  • You can assume that the input will be correct.
  • This is a so shortest code in bytes wins!

Examples

Input
Output
-----
Input
Output

  abcdef
 ¶    ,g
q,,,,, h
p    ,i
onmlkj

abcdefghijklmnopq

-----

         >,,,,,,,,,,,,,,,,,,v
        ¶                  ,,
       ,                  , ,
      ,                  ,  ,
     ,                  ,   ,
    ,                  ,    ,
   ,                  ,     ,
  ,                  ,      ,
 ,                  ,       ,
/,,,,,,,,,,,,,,,,,,/        /
,                  ,       ,
,                  ,      ,
,                  ,     ,
,                  ,    ,
,                  ,   ,
,                  ,  ,
,                  , ,
,                  ,,
^,,,,,,,,,,,,,,,,,,<

nothing

-----

                   Mr/~gm,Vc!r,0cbmPC@Zg9jb)7=m^ppwN*Nuk){r
                  c                                      ¶Z
                 J                                      ; #
                e                                      j  8
               ]                                      b   "
              #                                      `    4
             h                                      $     -
            p                                      n      2
           L                                      S       P
          B                                      ;        R
         @                                      J         D
        E                                      N          <
       O                                      \           p
      B                                      y            =
     |                                      9             ;
    T                                      x              [
   6                                      o               k
  !                                      L                =
 E                                      K                 ~
>mjmz6,?A*Q)^.a:IpGP"^bK}DSXL)2F!BV|"m6/                  T
a                                      A                 6
5                                      =                p
1                                      i               R
4                                      /              ?
-                                      Z             (
1                                      "            `
'                                      0           {
N                                      p          M
O                                      *         K
Z                                      J        $
"                                      (       d
4                                      +      o
f                                      U     g
i                                      v    b
U                                      ~   L
U                                      M  h
g                                      ^ D
2                                      (g
5]u*n<Ldn</9/XQyC444Va\m}W3Rf,rp"a@5cZB0

(Newlines added for readability)
Mr/~gmVc!r0cbmPC@Zg9jb)7=m^ppwN*Nuk){rZ#8"4-2PRD<p=;[k=~T
6pR?(`{MK$dogbLhDg0BZc5@a"prfR3W}m\aV444CyQX/9/ndLn*u]52g
UUif4"ZON'1-415amjmz6?A*Q)^.a:IpGP"^bK}DSXL)2F!BV|"m6KLox9
y\NJ;Sn$`bj;

-----

       NEWYORKCITYBABY!
      ¶              ,,
     ,              , ,
    ,              ,  ,
   ,              ,   ,
  ,              ,    ,
 ,              ,     ,
,,,,,,,,,,,,,,,,      ,
,              ,     ,
,              ,    ,
,              ,   ,
,              ,  ,
,              , ,
,              ,,
,,,,,,,,,,,,,,,,

NEWYORKCITYBABY!

-----

                          /{Y!_(=-)s<e!D^<+<+1o30d>QOx9kfu?3X2I:8],t>Ye~,Y9/6;u@
                         ]                                                    >Y
                        !                                                    1 )
                       c                                                    7  8
                      ^                                                    K   &
                     f                                                    K    9
                    M                                                    }     O
                   s                                                    _      ?
                  h                                                    N       e
                 ?                                                    u        ,
                g                                                    =         s
               >                                                    Q          M
              a                                                    0           9
             b                                                    c            (
            h                                                    {             c
           [                                                    m              *
          I                                                    R               %
         K                                                    E                >
        u                                                    5                 n
       W                                                    f                  ]
      }                                                    |                   y
     F                                                    E                    q
    )                                                    <                     ;
   X                                                    L                      S
  F                                                    M                       |
 u                                                    *                        Z
>XZpJ=L`+8ONRk?T7$qVr6-U^f14{>>ABEEP`Mjx&T[/&o*F=*qq{/                         e
¶                                                    <                        ^
+                                                    P                       C
.                                                    x                      m
;                                                    ^                     >
y                                                    4                    b
5                                                    v                   `
Q                                                    B                  c
2                                                    e                 r
a                                                    x                I
3                                                    1               Y
Q                                                    v              `
w                                                    ;             o
*                                                    S            h
E                                                    b           S
r                                                    6          P
M                                                    `         l
%                                                    M        A
-                                                    `       "
j                                                    \      s
}                                                    ~     J
$                                                    h    c
%                                                    p   O
I                                                    Z  Z
]                                                    N E
`                                                    3<
x^}}SI"-a3[%7U5EV}"UM?Aov|E_yjrMt<HAQ@}QAABO&y//R0$\2p

(Newlines added for readability)
]!c^fMsh?g>abh[IKuW}F)XFuXZpJ=L`+8ONRk?T7$qVr6-U^f14{ABEE
P`Mjx&T[/&o*F=*qq{*ML<E|f5ERm{c0Q=uN_}KK71Y)8&9O?esM9(c*%
>n]yq;S|Ze^Cm>b`crIY`ohSPlA"sJcOZE3NZph~\`M`6bS;;Sb6`M`\~
hpZN3p2\$0R//y&OBAAQ}@QAHtMrjy_E|voA?MU"}VE5U7%[3a-"IS}}^
x`]I%$}j-%MrE*wQ3a2Q5y;.+

-----

   %:biv+|?
  ¶      \+
 T      c ,
?{yX<//v  "
i      [ @
Q      Tj
U8sUNl.?

%:biv+|+"@jT[[T.lNUs8UQi{yXXy{T

-----

     FCf2hoYA)&*j
    F          ¶#
   3          p (
  &          ~  w
 ;          *   t
39Hq{F'MmmO/    9
5          \   D
-          |  }
8          c l
a          25
GKa"<0/7MG[?

FCf2hoYA)&*j#(wt9D}l5[GM7/0"aKGa8-53;&3FFCf2hoYA)&*j#(wt9D}l52c|\*~p

-----

           #S^[WNk3;9v;e2\h!@CI#Il?
          G                      g`
         $                      2 \
        V                      9  S
       m                      Y   b
      6                      )    *
     U                      '     V
    [                      g      V
   I                      8       X
  <                      T        U
 K                      ¶         C
?ozMt+xxxxutvQgkx&D9I/<v          '
w                      K         p
V                      y        h
(                      R       y
e                      P      i
]                      !     x
Z                      /    s
y                      w   '
#                      X  4
c                      ~ W
Z                      E,
~IdXPYS.Lc49lC~vjULUBv.?

(Newlines added for readability)    
#S^[WNk3;9v;e2\h!@CI#Il`\Sb*VVXUC'phyixs'4WE~Xw/!PRyKKyRP!/wX~E
.vBULUjv~Cl94cL.SYPXdI~Zc#yZ]e(VwozMt+xxxxutvQgkx&D9I//I9D&xkgQ
vtuxxxx+tMzoK<I[U6mV$G#S^[WNk3;9v;e2\h!@CI#Ilg29Y)'g8T
\$\endgroup\$
5
  • \$\begingroup\$ No problem :) Really nice challenge btw! :) \$\endgroup\$ – HyperNeutrino May 23 '17 at 14:10
  • \$\begingroup\$ @HyperNeutrino I kinda feel guilty now that the bounty has started. This was a project that I was working on but gave up and decided to post. So I can just polish off my old solution and post it for the bounty! I'll only do that if there are no answer by the end of the week, though \$\endgroup\$ – caird coinheringaahing May 23 '17 at 14:17
  • \$\begingroup\$ @RandomUser go ahead and post an answer now, people will be more inclined to answer when they have something to beat. And don't feed guilty, it's the equivalent of 5 answer upvotes. \$\endgroup\$ – programmer5000 May 23 '17 at 14:33
  • \$\begingroup\$ @programmer5000 It's not finished and no where near golfed but I should get it done in a few days \$\endgroup\$ – caird coinheringaahing May 23 '17 at 14:43
  • \$\begingroup\$ @RandomUser derp \$\endgroup\$ – Stephen May 23 '17 at 17:09
9
+25
\$\begingroup\$

Python 3, 1558 1553 1419 1417 1347 1258 1106 bytes

L=list
M=map
Q=len
m=~0
G,H,J,K,Y,I=[0,1],[0,m],[1,0],[m,0],[m,1],[1,m]
z=lambda p,u:L(M(sum,zip(p,u)))
def f(a):
 def U(p,c,u,a,d):
  try:u=[J,I,Y,H,G,I,K][d.index(p)]
  except:0
  return F+c*a,z(p,u),u
 a=L(M(L,a.split('\n')));p=0,0;n=a[0]
 for i in range(Q(n)):
  if' '<n[i]:p=0,~-i,;break
 c,P,F,w,h,e,l,t=n[p[1]],'><^v/','',~-Q(n),Q(a)//2,~-Q(a[m]),~-Q(a),{'v':J,'^':K,'>':G,'<':H};d,A,D=[[0,w],[h,e],[h,0],[l,e]],t.get,[[0,h],[h,w],[l,0]];C,u,s,r,E=d+D,[G,A(c,I)][c in P],0,1,[[0,w-1],[1,w-1],[1,w],[~-h,1],[h,1],[e-2,-~e],[e-2,e]]
 while'¶'!=c:
  k=p,c,u;g=z(p,u);S=c=='/'
  if s:s=0;u=u[::m]
  if c in P:
   if p in C:u=[t[c],[Y,I][p[1]==w]][S];p=g
   elif p in E:F,p,u,s=[(F,g,t[c],1),U(*k,1,C)+s][S]
   else:
    o=A(c,u);v,y=z(p,o)
    try:n=a[v][y];u=[u,o][(' '<n)*~(p[0]<1<'^'==c)]
    except:0
    p=g
    if u!=A(c):F,p,u=U(*k,1,C)
  elif'?'==c:
   F+=c*-~(p in C!=F[m]!=c);r=~r
   if~r<1and p in d:u=[[I,H][u!=G],[H,J][u!=K],[G,J][u!=K],[K,Y][u!=I]][d.index(p)];p=z(p,u)
   else:F,p,u=U(*k,0,C)
  else:F,p,u=U(*k,-~(c in' ,'),C)
  c=a[p[0]][p[1]]
 for c in'?, ':F=F.replace(c,'')
 return F

Try it online!

Saved 5 bytes thanks to @sagiksp

Saved 20 bytes thanks to @Mr.Xcoder and @totallyhuman in chat

Saved 128 bytes thanks to various golfing tricks

Saved 72 bytes thanks to @JonathanFrech

Saved another 89 bytes thanks to @Mr.Xcoder

\$\endgroup\$
19
  • \$\begingroup\$ Nice golfing, though you missed some with not(p[0] == 0), it could be just p[0]!=0. \$\endgroup\$ – sagiksp May 28 '17 at 14:57
  • \$\begingroup\$ you can cut many bytes by differing indentation levels with a single byte only \$\endgroup\$ – Uriel May 28 '17 at 15:45
  • \$\begingroup\$ @Uriel the tabs actually go space then \t (tab) then space\t etc. Its just the way SE formats posts \$\endgroup\$ – caird coinheringaahing May 28 '17 at 15:53
  • \$\begingroup\$ @Ilikemydog oh great \$\endgroup\$ – Uriel May 28 '17 at 16:03
  • \$\begingroup\$ Why indent it at all? \$\endgroup\$ – Shaggy May 28 '17 at 16:32

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.