25
\$\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
  • 1
    \$\begingroup\$ No problem :) Really nice challenge btw! :) \$\endgroup\$
    – hyper-neutrino
    Commented May 23, 2017 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\$ Commented May 23, 2017 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\$
    – user58826
    Commented May 23, 2017 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\$ Commented May 23, 2017 at 14:43
  • 1
    \$\begingroup\$ @RandomUser derp \$\endgroup\$
    – Stephen
    Commented May 23, 2017 at 17:09

1 Answer 1

11
+950
\$\begingroup\$

Vim, 971 bytes

Go<C-v><C-v><C-v>
elsei'<C-v><C-v><C-v><C-r>t'==<Esc>^"zC<C-v><C-v><C-v>
let@b=<Esc>^"fC<C-v><C-v><C-v>
let@O=<Esc>^"xC:if'<C-v><C-v><C-v><C-r>0'==' '<Esc>^"yC<C-v><C-v><C-v>
norm <Esc>^"wC<C-v><C-v><C-v>
let@m='<Esc>^"vC'<C-v><C-v><C-r>f0<C-v><C-v><C-v>
el<C-v><C-v><C-v>
if'<C-v><C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'v'<C-r>w@d<C-r>z'^'<C-r>w@u<C-r>z'/'<C-r>w@s@e<C-r>z'?'<C-r>w@q@e<C-r>z'█'<C-r>wHdG"op<C-v>
k<C-r>z','<C-r>w@e<C-v>
el<C-r>w"Oyl@e<C-v>
en<C-v>
@c<Esc>^"cCma@myl<C-r>y<C-r>w`a@g<C-r>w@m<C-v>
en<C-v>
<Esc>^"eC<C-r>j58<C-r>vl'<C-r>z178<C-r>vj'<C-r>z38<C-r>vhj'<C-r>z134<C-r>vh'<C-r>z35<C-r>vk'<C-r>z<C-r>i<C-v>
en<C-v>
<Esc>^"gC:let@t=''<C-v>
h<C-r>h1<C-v>
en<C-v>
<C-v><Esc>k<C-r>h2<C-v>
en<C-v>
l<C-r>h3<C-v>
en<C-v>
l<C-r>h4<C-v>
en<C-v>
j<C-r>h5<C-v>
en<C-v>
j<C-r>h6<C-v>
en<C-v>
h<C-r>h7<C-v>
en<C-v>
h<C-r>h8<C-v>
en<C-v>
<C-v><Esc>kl<Esc>^"nC1l<Esc>"mx"bxgg
@c

Try it online!

That's a chonker of a Vim program right there. The inputted cube requires a padding of spaces around the sides. Additionally, I used instead of as the stopping point, but that is only because has a special meaning in V. You can replace the with and it will run just fine in Vim.

\$\endgroup\$

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