Flatten the CUBE

17

3

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

caird coinheringaahing

Posted 2017-05-21T09:19:36.887

Reputation: 13 702

No problem :) Really nice challenge btw! :) – HyperNeutrino – 2017-05-23T14:10:36.467

@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 – caird coinheringaahing – 2017-05-23T14:17:10.437

@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. – programmer5000 – 2017-05-23T14:33:16.277

@programmer5000 It's not finished and no where near golfed but I should get it done in a few days – caird coinheringaahing – 2017-05-23T14:43:18.870

@RandomUser derp – Stephen – 2017-05-23T17:09:20.160

Answers

8

Python 3, 1558 1553 1419 1417 1347 1258 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]
def U(p,c,u,a):
 global F;F+=c*a
 try:u=[G,J,I,I,Y,K,H][[[0,h],[0,w],[h,w],[h,e],[h,0],[l,0],[l,e]].index(p)]
 except:0
 p=L(M(sum,zip(p,u)));return F,p,u
def f(a):
 global w,h,e,l,F;a=L(M(L,a.split('\n')));p=0,0
 for(i)in range(Q(a[0])):
  if' '<a[0][i]:p=0,~-i,;break
 c,P,F,w,h,e,l,t=a[p[0]][p[1]],'><^v/','',~-Q(a[0]),Q(a)//2,~-Q(a[m]),~-Q(a),{'v':J,'^':K,'>':G,'<':H}
 C,u,s,r,E=[[0,w],[0,h],[h,w],[h,e],[h,0],[l,0],[l,e]],[G,t.get(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=L(M(sum,zip(p,u)))
  if s:s=0;u=u[::m]
  if(c)in' ,':F,p,u=U(*k,0)
  elif(c)in P:
   if(p)in C:u=[t[c],[Y,I][p[1]==w]][c=='/'];p=g
   elif(p)in E:F,p,u,s=[(F,g,t[c],1),U(*k,1)+s][c=='/']
   else:
    o=t.get(c,u)
    try:n=a[p[0]+o[0]][p[1]+o[1]];u=[u,o][(' '<n)*~(p[0]<1<'^'==c)]
    except:0
    if u!=t.get(c):F,p,u=U(*k,1)
    else:p=g
  elif'?'==c:
   F+=c*-~(p in C!=F[m]!=c);r=~r
   if~r<1and(p)in[[0,w],[h,e],[h,0],[l,e]]:u=[[I,H][u!=G],[H,J][u!=K],[G,J][u!=K],[K,Y][u!=I]][[[0,w],[h,e],[h,0],[l,e]].index(p)];p=L(M(sum,zip(p,u)))
   elif-1>r:F,p,u=U(*k,0)
  else:F,p,u=U(*k,1)
  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

caird coinheringaahing

Posted 2017-05-21T09:19:36.887

Reputation: 13 702

Nice golfing, though you missed some with not(p[0] == 0), it could be just p[0]!=0. – sagiksp – 2017-05-28T14:57:28.793

you can cut many bytes by differing indentation levels with a single byte only – Uriel – 2017-05-28T15:45:23.783

@Uriel the tabs actually go space then \t (tab) then space\t etc. Its just the way SE formats posts – caird coinheringaahing – 2017-05-28T15:53:04.597

@Ilikemydog oh great – Uriel – 2017-05-28T16:03:07.203

Why indent it at all? – Shaggy – 2017-05-28T16:32:24.943

@Shaggy python requires if and while statements to be indented to tell them apart from regular code. – caird coinheringaahing – 2017-05-28T16:37:55.853

G,H,J,K,Y,I=[[0,1],[0,-1],[1,0],[-1,0],[-1,1],[1,-1]] -> G,H,J,K,Y,I=[0,1],[0,-1],[1,0],[-1,0],[-1,1],[1,-1] – Erik the Outgolfer – 2017-08-30T09:05:24.167

The literal -1 is used seven times. Replacing those with a variable (-7 bytes) defined as m=~0; (+5 bytes) could save two bytes. – Jonathan Frech – 2017-09-23T23:36:38.577

@JonathanFrech or, I could assign it as m=-1 rather than all this bitwise nonsense :P – caird coinheringaahing – 2017-09-23T23:38:23.627

Well, your choice. C)*( -> C!= and 1)*( -> 1!= could save two bytes. – Jonathan Frech – 2017-09-24T00:02:15.183

There are two unnecessary spaces after if p in C:. – Jonathan Frech – 2017-09-24T00:03:54.327

@JonathanFrech thanks for the golfs. I'll edit the answer when I next get to a computer – caird coinheringaahing – 2017-09-24T00:09:22.630

With the prior implemented, 1359 bytes. (Using some bitwise "nonsense".)

– Jonathan Frech – 2017-09-24T00:25:19.793

1349 bytes. – Jonathan Frech – 2017-09-24T00:51:47.933

1347 bytes. – Jonathan Frech – 2017-09-24T01:04:41.817

p=[0]*2 may be golfable to p=0,0, though I am not certain. – Jonathan Frech – 2017-10-01T20:47:09.247

You can save a considerable amount of bytes by removing unnecessary global imports, turning lists into tuples, removing obsolete declarations and many other function -> variable tricks for 1258 bytes (-90)

– Mr. Xcoder – 2018-03-11T11:40:30.257

@Mr.Xcoder Thanks, edited! – caird coinheringaahing – 2018-03-11T12:31:43.000

Your code seems to break on the example that shouldn't output anything. – NieDzejkob – 2020-01-10T18:36:37.607