Output the anti-clockwise inward spiral of a 2D array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
14
down vote

favorite












From this stackoverflow question



Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.



Example Given:



$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$



The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.



Now we repeat the process for the inner values. This will end up with a matrix like the following



$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$



And the inner values is then $ 6,10,11,7 $



The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $




Rules



  • Assume non-empty input

  • Assume matrix values as positive integers

  • Standard I/O Methods apply

  • Standard code-golf rules and winning criteria apply


Some test cases



Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13

--------------------------------------------------------

Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8

-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6









share|improve this question























  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    23 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    23 hours ago






  • 5




    Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
    – Digital Trauma
    22 hours ago















up vote
14
down vote

favorite












From this stackoverflow question



Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.



Example Given:



$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$



The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.



Now we repeat the process for the inner values. This will end up with a matrix like the following



$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$



And the inner values is then $ 6,10,11,7 $



The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $




Rules



  • Assume non-empty input

  • Assume matrix values as positive integers

  • Standard I/O Methods apply

  • Standard code-golf rules and winning criteria apply


Some test cases



Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13

--------------------------------------------------------

Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8

-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6









share|improve this question























  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    23 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    23 hours ago






  • 5




    Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
    – Digital Trauma
    22 hours ago













up vote
14
down vote

favorite









up vote
14
down vote

favorite











From this stackoverflow question



Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.



Example Given:



$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$



The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.



Now we repeat the process for the inner values. This will end up with a matrix like the following



$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$



And the inner values is then $ 6,10,11,7 $



The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $




Rules



  • Assume non-empty input

  • Assume matrix values as positive integers

  • Standard I/O Methods apply

  • Standard code-golf rules and winning criteria apply


Some test cases



Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13

--------------------------------------------------------

Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8

-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6









share|improve this question















From this stackoverflow question



Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.



Example Given:



$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$



The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.



Now we repeat the process for the inner values. This will end up with a matrix like the following



$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$



And the inner values is then $ 6,10,11,7 $



The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $




Rules



  • Assume non-empty input

  • Assume matrix values as positive integers

  • Standard I/O Methods apply

  • Standard code-golf rules and winning criteria apply


Some test cases



Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13

--------------------------------------------------------

Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8

-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6






code-golf matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 mins ago









hakr14

1,16529




1,16529










asked yesterday









Luis felipe De jesus Munoz

3,24611049




3,24611049











  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    23 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    23 hours ago






  • 5




    Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
    – Digital Trauma
    22 hours ago

















  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    23 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    23 hours ago






  • 5




    Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
    – Digital Trauma
    22 hours ago
















So are we going clockwise or counterclockwise?
– LegionMammal978
23 hours ago




So are we going clockwise or counterclockwise?
– LegionMammal978
23 hours ago












@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
– Luis felipe De jesus Munoz
23 hours ago




@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
– Luis felipe De jesus Munoz
23 hours ago




5




5




Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
– Digital Trauma
22 hours ago





Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
– Digital Trauma
22 hours ago











14 Answers
14






active

oldest

votes

















up vote
11
down vote














R, 54 bytes



Several bytes saved by @Giuseppe and @J.Doe.





f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))


Try it online!



Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:



f <- function(m) 
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))




It was pointed out that ncol(m) could be golfed to sum(m) to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)






share|improve this answer






















  • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
    – Giuseppe
    22 hours ago










  • and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
    – Giuseppe
    22 hours ago










  • @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
    – ngm
    22 hours ago










  • Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
    – J.Doe
    21 hours ago


















up vote
9
down vote














Python 2, 52 bytes





f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()


Try it online!






share|improve this answer



























    up vote
    7
    down vote














    Pyth, 9 bytes



    shMM.utC_


    Try it here!



    How?



    shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
    .u Until a result that has occurred before is found, loop and collect...
    _ Reverse the matrix (reverse the order of its rows).
    C Transpose.
    t Remove first element.
    hMM For each element in the resulting 3D array, get the heads of its elements.
    s Flatten.





    share|improve this answer




















    • Thats cool. As a Pyth beginner I now know I have a lot to learn,
      – ElPedro
      19 hours ago

















    up vote
    5
    down vote














    Stax, 7 bytes



    ôQÖG·í<


    Run and debug it



    It takes an array of rows on one line, and produces newline-separated output.



    Unpacked, ungolfed, and commented, it looks like this.



    W repeat the rest of the program until cancelled explicitly
    rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
    |c assert matrix is truthy. (has rows) cancel otherwise.
    B remove the top row of the matrix, and push separately to main stack
    rm reverse the top row (this fixes the rotation direction), and print each value


    Run this one






    share|improve this answer



























      up vote
      4
      down vote













      Pyth, 20 bytes



      J.TQWJ=+YhJ=J_.TtJ)Y


      Try it here



      Explanation



      J.TQWJ=+YhJ=J_.TtJ)Y
      J.TQ Call the transposed input J.
      WJ ) While J is not empty...
      =+YhJ ... put the top row into Y (initially )...
      =J tJ ... remove the top row...
      _.T ... reverse and transpose (rotate clockwise).
      Y Output the result.





      share|improve this answer



























        up vote
        3
        down vote














        Clean, 69 bytes



        import StdEnv,StdLib
        ? =transpose
        @[h:t]=h++ @(reverse(?t))
        @_=




        @o?


        Try it online!



        Moves the next row/column to the head of the list so it can pattern matched in the argument.



        For the first example in the challenge, this looks like:



        @o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
        @ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
        [1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
        [1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
        [1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
        [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
        [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
        [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @





        share|improve this answer





























          up vote
          3
          down vote














          oK, 12 bytes



          *+,/(1_+|:)


          Try it online!



          This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:).



           +|: /reverse, then transpose (rotate right)
          1_ /remove first line
          ( ) /fixpoint of the above, keeping intermediate results (scan)
          ,/ /concatenate all the rows
          *+ /get the first element of each row





          share|improve this answer





























            up vote
            2
            down vote














            JavaScript (Node.js), 89 bytes





            f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:


            Try it online!



            Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.






            share|improve this answer



























              up vote
              2
              down vote














              05AB1E, 13 11 bytes



              ΔRøćRˆ}¯˜õK


              -2 bytes thanks to @Emigna.



              Try it online or verify all test cases.



              Explanation:





              Δ # Loop until the stack no longer changes:
              R # Reverse the order of the rows
              # i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
              # → [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
              ø # Zip, swapping rows and column
              # → [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
              ć # Head extracted
              # → [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
              R # Reverse this row
              # → [1,5,9,13]
              ˆ # Pop and push it to the global array
              } # After the loop:
              ¯ # Push the global array
              # i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
              ˜ # Flatten it
              # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
              õK # Remove all empty string (and output it implicitly)
              # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]





              share|improve this answer





























                up vote
                2
                down vote














                Julia 1.0, 50 bytes





                f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]


                Try it online!



                Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
                Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.



                E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):



                f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]


                However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!






                share|improve this answer





























                  up vote
                  1
                  down vote














                  Jelly, 9 bytes



                  ZḊUƊƬḢ€€Ẏ


                  Try it online!






                  share|improve this answer



























                    up vote
                    1
                    down vote














                    Charcoal, 25 bytes



                    ≔⮌EA⮌ιθWθ«≔E§θ⁰⮌Eθ§μλθI⊟θ


                    Try it online! Link is to verbose version of code. Explanation:



                    ≔⮌EA⮌ιθ


                    Rotate the input by 180°. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)



                    Wθ«


                    Repeat until the array is empty.



                    ≔E§θ⁰⮌Eθ§μλθ


                    Rotate the array by 90°.



                    I⊟θ


                    Remove the last row of the array and print the element as strings on separate lines.






                    share|improve this answer





























                      up vote
                      1
                      down vote














                      Ruby, 65 bytes





                      ->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x


                      Try it online!






                      share|improve this answer



























                        up vote
                        1
                        down vote














                        APL (Dyalog), 24 22 bytes





                        ×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬


                        Try it online!






                        share|improve this answer




















                          Your Answer




                          StackExchange.ifUsing("editor", function ()
                          return StackExchange.using("mathjaxEditing", function ()
                          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                          );
                          );
                          , "mathjax-editing");

                          StackExchange.ifUsing("editor", function ()
                          StackExchange.using("externalEditor", function ()
                          StackExchange.using("snippets", function ()
                          StackExchange.snippets.init();
                          );
                          );
                          , "code-snippets");

                          StackExchange.ready(function()
                          var channelOptions =
                          tags: "".split(" "),
                          id: "200"
                          ;
                          initTagRenderer("".split(" "), "".split(" "), channelOptions);

                          StackExchange.using("externalEditor", function()
                          // Have to fire editor after snippets, if snippets enabled
                          if (StackExchange.settings.snippets.snippetsEnabled)
                          StackExchange.using("snippets", function()
                          createEditor();
                          );

                          else
                          createEditor();

                          );

                          function createEditor()
                          StackExchange.prepareEditor(
                          heartbeatType: 'answer',
                          convertImagesToLinks: false,
                          noModals: false,
                          showLowRepImageUploadWarning: true,
                          reputationToPostImages: null,
                          bindNavPrevention: true,
                          postfix: "",
                          onDemand: true,
                          discardSelector: ".discard-answer"
                          ,immediatelyShowMarkdownHelp:true
                          );



                          );













                           

                          draft saved


                          draft discarded


















                          StackExchange.ready(
                          function ()
                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173869%2foutput-the-anti-clockwise-inward-spiral-of-a-2d-array%23new-answer', 'question_page');

                          );

                          Post as a guest






























                          14 Answers
                          14






                          active

                          oldest

                          votes








                          14 Answers
                          14






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes








                          up vote
                          11
                          down vote














                          R, 54 bytes



                          Several bytes saved by @Giuseppe and @J.Doe.





                          f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))


                          Try it online!



                          Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:



                          f <- function(m) 
                          if(ncol(m) == 1)
                          m
                          else
                          c(m[,1], f(t(m[nrow(m):1,-1])))




                          It was pointed out that ncol(m) could be golfed to sum(m) to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)






                          share|improve this answer






















                          • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                            – Giuseppe
                            22 hours ago










                          • and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
                            – Giuseppe
                            22 hours ago










                          • @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
                            – ngm
                            22 hours ago










                          • Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
                            – J.Doe
                            21 hours ago















                          up vote
                          11
                          down vote














                          R, 54 bytes



                          Several bytes saved by @Giuseppe and @J.Doe.





                          f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))


                          Try it online!



                          Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:



                          f <- function(m) 
                          if(ncol(m) == 1)
                          m
                          else
                          c(m[,1], f(t(m[nrow(m):1,-1])))




                          It was pointed out that ncol(m) could be golfed to sum(m) to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)






                          share|improve this answer






















                          • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                            – Giuseppe
                            22 hours ago










                          • and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
                            – Giuseppe
                            22 hours ago










                          • @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
                            – ngm
                            22 hours ago










                          • Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
                            – J.Doe
                            21 hours ago













                          up vote
                          11
                          down vote










                          up vote
                          11
                          down vote










                          R, 54 bytes



                          Several bytes saved by @Giuseppe and @J.Doe.





                          f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))


                          Try it online!



                          Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:



                          f <- function(m) 
                          if(ncol(m) == 1)
                          m
                          else
                          c(m[,1], f(t(m[nrow(m):1,-1])))




                          It was pointed out that ncol(m) could be golfed to sum(m) to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)






                          share|improve this answer















                          R, 54 bytes



                          Several bytes saved by @Giuseppe and @J.Doe.





                          f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))


                          Try it online!



                          Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:



                          f <- function(m) 
                          if(ncol(m) == 1)
                          m
                          else
                          c(m[,1], f(t(m[nrow(m):1,-1])))




                          It was pointed out that ncol(m) could be golfed to sum(m) to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 2 hours ago

























                          answered 23 hours ago









                          ngm

                          2,74923




                          2,74923











                          • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                            – Giuseppe
                            22 hours ago










                          • and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
                            – Giuseppe
                            22 hours ago










                          • @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
                            – ngm
                            22 hours ago










                          • Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
                            – J.Doe
                            21 hours ago

















                          • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                            – Giuseppe
                            22 hours ago










                          • and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
                            – Giuseppe
                            22 hours ago










                          • @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
                            – ngm
                            22 hours ago










                          • Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
                            – J.Doe
                            21 hours ago
















                          wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                          – Giuseppe
                          22 hours ago




                          wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                          – Giuseppe
                          22 hours ago












                          and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
                          – Giuseppe
                          22 hours ago




                          and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
                          – Giuseppe
                          22 hours ago












                          @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
                          – ngm
                          22 hours ago




                          @Giuseppe back to 59 bytes! I was pleasantly surprised by t() to not have to use an is.null test that was in my original attempts.
                          – ngm
                          22 hours ago












                          Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
                          – J.Doe
                          21 hours ago





                          Isn't that last m going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
                          – J.Doe
                          21 hours ago











                          up vote
                          9
                          down vote














                          Python 2, 52 bytes





                          f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()


                          Try it online!






                          share|improve this answer
























                            up vote
                            9
                            down vote














                            Python 2, 52 bytes





                            f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()


                            Try it online!






                            share|improve this answer






















                              up vote
                              9
                              down vote










                              up vote
                              9
                              down vote










                              Python 2, 52 bytes





                              f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()


                              Try it online!






                              share|improve this answer













                              Python 2, 52 bytes





                              f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()


                              Try it online!







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 23 hours ago









                              TFeld

                              12.5k2835




                              12.5k2835




















                                  up vote
                                  7
                                  down vote














                                  Pyth, 9 bytes



                                  shMM.utC_


                                  Try it here!



                                  How?



                                  shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
                                  .u Until a result that has occurred before is found, loop and collect...
                                  _ Reverse the matrix (reverse the order of its rows).
                                  C Transpose.
                                  t Remove first element.
                                  hMM For each element in the resulting 3D array, get the heads of its elements.
                                  s Flatten.





                                  share|improve this answer




















                                  • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                    – ElPedro
                                    19 hours ago














                                  up vote
                                  7
                                  down vote














                                  Pyth, 9 bytes



                                  shMM.utC_


                                  Try it here!



                                  How?



                                  shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
                                  .u Until a result that has occurred before is found, loop and collect...
                                  _ Reverse the matrix (reverse the order of its rows).
                                  C Transpose.
                                  t Remove first element.
                                  hMM For each element in the resulting 3D array, get the heads of its elements.
                                  s Flatten.





                                  share|improve this answer




















                                  • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                    – ElPedro
                                    19 hours ago












                                  up vote
                                  7
                                  down vote










                                  up vote
                                  7
                                  down vote










                                  Pyth, 9 bytes



                                  shMM.utC_


                                  Try it here!



                                  How?



                                  shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
                                  .u Until a result that has occurred before is found, loop and collect...
                                  _ Reverse the matrix (reverse the order of its rows).
                                  C Transpose.
                                  t Remove first element.
                                  hMM For each element in the resulting 3D array, get the heads of its elements.
                                  s Flatten.





                                  share|improve this answer













                                  Pyth, 9 bytes



                                  shMM.utC_


                                  Try it here!



                                  How?



                                  shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
                                  .u Until a result that has occurred before is found, loop and collect...
                                  _ Reverse the matrix (reverse the order of its rows).
                                  C Transpose.
                                  t Remove first element.
                                  hMM For each element in the resulting 3D array, get the heads of its elements.
                                  s Flatten.






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 20 hours ago









                                  Mr. Xcoder

                                  30.9k758195




                                  30.9k758195











                                  • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                    – ElPedro
                                    19 hours ago
















                                  • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                    – ElPedro
                                    19 hours ago















                                  Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                  – ElPedro
                                  19 hours ago




                                  Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                  – ElPedro
                                  19 hours ago










                                  up vote
                                  5
                                  down vote














                                  Stax, 7 bytes



                                  ôQÖG·í<


                                  Run and debug it



                                  It takes an array of rows on one line, and produces newline-separated output.



                                  Unpacked, ungolfed, and commented, it looks like this.



                                  W repeat the rest of the program until cancelled explicitly
                                  rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
                                  |c assert matrix is truthy. (has rows) cancel otherwise.
                                  B remove the top row of the matrix, and push separately to main stack
                                  rm reverse the top row (this fixes the rotation direction), and print each value


                                  Run this one






                                  share|improve this answer
























                                    up vote
                                    5
                                    down vote














                                    Stax, 7 bytes



                                    ôQÖG·í<


                                    Run and debug it



                                    It takes an array of rows on one line, and produces newline-separated output.



                                    Unpacked, ungolfed, and commented, it looks like this.



                                    W repeat the rest of the program until cancelled explicitly
                                    rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
                                    |c assert matrix is truthy. (has rows) cancel otherwise.
                                    B remove the top row of the matrix, and push separately to main stack
                                    rm reverse the top row (this fixes the rotation direction), and print each value


                                    Run this one






                                    share|improve this answer






















                                      up vote
                                      5
                                      down vote










                                      up vote
                                      5
                                      down vote










                                      Stax, 7 bytes



                                      ôQÖG·í<


                                      Run and debug it



                                      It takes an array of rows on one line, and produces newline-separated output.



                                      Unpacked, ungolfed, and commented, it looks like this.



                                      W repeat the rest of the program until cancelled explicitly
                                      rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
                                      |c assert matrix is truthy. (has rows) cancel otherwise.
                                      B remove the top row of the matrix, and push separately to main stack
                                      rm reverse the top row (this fixes the rotation direction), and print each value


                                      Run this one






                                      share|improve this answer













                                      Stax, 7 bytes



                                      ôQÖG·í<


                                      Run and debug it



                                      It takes an array of rows on one line, and produces newline-separated output.



                                      Unpacked, ungolfed, and commented, it looks like this.



                                      W repeat the rest of the program until cancelled explicitly
                                      rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
                                      |c assert matrix is truthy. (has rows) cancel otherwise.
                                      B remove the top row of the matrix, and push separately to main stack
                                      rm reverse the top row (this fixes the rotation direction), and print each value


                                      Run this one







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 21 hours ago









                                      recursive

                                      4,6061220




                                      4,6061220




















                                          up vote
                                          4
                                          down vote













                                          Pyth, 20 bytes



                                          J.TQWJ=+YhJ=J_.TtJ)Y


                                          Try it here



                                          Explanation



                                          J.TQWJ=+YhJ=J_.TtJ)Y
                                          J.TQ Call the transposed input J.
                                          WJ ) While J is not empty...
                                          =+YhJ ... put the top row into Y (initially )...
                                          =J tJ ... remove the top row...
                                          _.T ... reverse and transpose (rotate clockwise).
                                          Y Output the result.





                                          share|improve this answer
























                                            up vote
                                            4
                                            down vote













                                            Pyth, 20 bytes



                                            J.TQWJ=+YhJ=J_.TtJ)Y


                                            Try it here



                                            Explanation



                                            J.TQWJ=+YhJ=J_.TtJ)Y
                                            J.TQ Call the transposed input J.
                                            WJ ) While J is not empty...
                                            =+YhJ ... put the top row into Y (initially )...
                                            =J tJ ... remove the top row...
                                            _.T ... reverse and transpose (rotate clockwise).
                                            Y Output the result.





                                            share|improve this answer






















                                              up vote
                                              4
                                              down vote










                                              up vote
                                              4
                                              down vote









                                              Pyth, 20 bytes



                                              J.TQWJ=+YhJ=J_.TtJ)Y


                                              Try it here



                                              Explanation



                                              J.TQWJ=+YhJ=J_.TtJ)Y
                                              J.TQ Call the transposed input J.
                                              WJ ) While J is not empty...
                                              =+YhJ ... put the top row into Y (initially )...
                                              =J tJ ... remove the top row...
                                              _.T ... reverse and transpose (rotate clockwise).
                                              Y Output the result.





                                              share|improve this answer












                                              Pyth, 20 bytes



                                              J.TQWJ=+YhJ=J_.TtJ)Y


                                              Try it here



                                              Explanation



                                              J.TQWJ=+YhJ=J_.TtJ)Y
                                              J.TQ Call the transposed input J.
                                              WJ ) While J is not empty...
                                              =+YhJ ... put the top row into Y (initially )...
                                              =J tJ ... remove the top row...
                                              _.T ... reverse and transpose (rotate clockwise).
                                              Y Output the result.






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 23 hours ago









                                              Mnemonic

                                              4,5021629




                                              4,5021629




















                                                  up vote
                                                  3
                                                  down vote














                                                  Clean, 69 bytes



                                                  import StdEnv,StdLib
                                                  ? =transpose
                                                  @[h:t]=h++ @(reverse(?t))
                                                  @_=




                                                  @o?


                                                  Try it online!



                                                  Moves the next row/column to the head of the list so it can pattern matched in the argument.



                                                  For the first example in the challenge, this looks like:



                                                  @o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
                                                  @ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
                                                  [1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
                                                  [1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
                                                  [1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
                                                  [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
                                                  [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
                                                  [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @





                                                  share|improve this answer


























                                                    up vote
                                                    3
                                                    down vote














                                                    Clean, 69 bytes



                                                    import StdEnv,StdLib
                                                    ? =transpose
                                                    @[h:t]=h++ @(reverse(?t))
                                                    @_=




                                                    @o?


                                                    Try it online!



                                                    Moves the next row/column to the head of the list so it can pattern matched in the argument.



                                                    For the first example in the challenge, this looks like:



                                                    @o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
                                                    @ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
                                                    [1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
                                                    [1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
                                                    [1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
                                                    [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
                                                    [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
                                                    [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @





                                                    share|improve this answer
























                                                      up vote
                                                      3
                                                      down vote










                                                      up vote
                                                      3
                                                      down vote










                                                      Clean, 69 bytes



                                                      import StdEnv,StdLib
                                                      ? =transpose
                                                      @[h:t]=h++ @(reverse(?t))
                                                      @_=




                                                      @o?


                                                      Try it online!



                                                      Moves the next row/column to the head of the list so it can pattern matched in the argument.



                                                      For the first example in the challenge, this looks like:



                                                      @o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
                                                      @ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
                                                      [1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
                                                      [1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @





                                                      share|improve this answer















                                                      Clean, 69 bytes



                                                      import StdEnv,StdLib
                                                      ? =transpose
                                                      @[h:t]=h++ @(reverse(?t))
                                                      @_=




                                                      @o?


                                                      Try it online!



                                                      Moves the next row/column to the head of the list so it can pattern matched in the argument.



                                                      For the first example in the challenge, this looks like:



                                                      @o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
                                                      @ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
                                                      [1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
                                                      [1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
                                                      [1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @






                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited 19 hours ago

























                                                      answered 19 hours ago









                                                      Οurous

                                                      5,42311031




                                                      5,42311031




















                                                          up vote
                                                          3
                                                          down vote














                                                          oK, 12 bytes



                                                          *+,/(1_+|:)


                                                          Try it online!



                                                          This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:).



                                                           +|: /reverse, then transpose (rotate right)
                                                          1_ /remove first line
                                                          ( ) /fixpoint of the above, keeping intermediate results (scan)
                                                          ,/ /concatenate all the rows
                                                          *+ /get the first element of each row





                                                          share|improve this answer


























                                                            up vote
                                                            3
                                                            down vote














                                                            oK, 12 bytes



                                                            *+,/(1_+|:)


                                                            Try it online!



                                                            This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:).



                                                             +|: /reverse, then transpose (rotate right)
                                                            1_ /remove first line
                                                            ( ) /fixpoint of the above, keeping intermediate results (scan)
                                                            ,/ /concatenate all the rows
                                                            *+ /get the first element of each row





                                                            share|improve this answer
























                                                              up vote
                                                              3
                                                              down vote










                                                              up vote
                                                              3
                                                              down vote










                                                              oK, 12 bytes



                                                              *+,/(1_+|:)


                                                              Try it online!



                                                              This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:).



                                                               +|: /reverse, then transpose (rotate right)
                                                              1_ /remove first line
                                                              ( ) /fixpoint of the above, keeping intermediate results (scan)
                                                              ,/ /concatenate all the rows
                                                              *+ /get the first element of each row





                                                              share|improve this answer















                                                              oK, 12 bytes



                                                              *+,/(1_+|:)


                                                              Try it online!



                                                              This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:).



                                                               +|: /reverse, then transpose (rotate right)
                                                              1_ /remove first line
                                                              ( ) /fixpoint of the above, keeping intermediate results (scan)
                                                              ,/ /concatenate all the rows
                                                              *+ /get the first element of each row






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited 7 hours ago

























                                                              answered 17 hours ago









                                                              zgrep

                                                              1,18139




                                                              1,18139




















                                                                  up vote
                                                                  2
                                                                  down vote














                                                                  JavaScript (Node.js), 89 bytes





                                                                  f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:


                                                                  Try it online!



                                                                  Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.






                                                                  share|improve this answer
























                                                                    up vote
                                                                    2
                                                                    down vote














                                                                    JavaScript (Node.js), 89 bytes





                                                                    f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:


                                                                    Try it online!



                                                                    Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.






                                                                    share|improve this answer






















                                                                      up vote
                                                                      2
                                                                      down vote










                                                                      up vote
                                                                      2
                                                                      down vote










                                                                      JavaScript (Node.js), 89 bytes





                                                                      f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:


                                                                      Try it online!



                                                                      Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.






                                                                      share|improve this answer













                                                                      JavaScript (Node.js), 89 bytes





                                                                      f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:


                                                                      Try it online!



                                                                      Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered 16 hours ago









                                                                      Shieru Asakoto

                                                                      2,040312




                                                                      2,040312




















                                                                          up vote
                                                                          2
                                                                          down vote














                                                                          05AB1E, 13 11 bytes



                                                                          ΔRøćRˆ}¯˜õK


                                                                          -2 bytes thanks to @Emigna.



                                                                          Try it online or verify all test cases.



                                                                          Explanation:





                                                                          Δ # Loop until the stack no longer changes:
                                                                          R # Reverse the order of the rows
                                                                          # i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
                                                                          # → [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
                                                                          ø # Zip, swapping rows and column
                                                                          # → [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
                                                                          ć # Head extracted
                                                                          # → [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
                                                                          R # Reverse this row
                                                                          # → [1,5,9,13]
                                                                          ˆ # Pop and push it to the global array
                                                                          } # After the loop:
                                                                          ¯ # Push the global array
                                                                          # i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
                                                                          ˜ # Flatten it
                                                                          # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
                                                                          õK # Remove all empty string (and output it implicitly)
                                                                          # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]





                                                                          share|improve this answer


























                                                                            up vote
                                                                            2
                                                                            down vote














                                                                            05AB1E, 13 11 bytes



                                                                            ΔRøćRˆ}¯˜õK


                                                                            -2 bytes thanks to @Emigna.



                                                                            Try it online or verify all test cases.



                                                                            Explanation:





                                                                            Δ # Loop until the stack no longer changes:
                                                                            R # Reverse the order of the rows
                                                                            # i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
                                                                            # → [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
                                                                            ø # Zip, swapping rows and column
                                                                            # → [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
                                                                            ć # Head extracted
                                                                            # → [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
                                                                            R # Reverse this row
                                                                            # → [1,5,9,13]
                                                                            ˆ # Pop and push it to the global array
                                                                            } # After the loop:
                                                                            ¯ # Push the global array
                                                                            # i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
                                                                            ˜ # Flatten it
                                                                            # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
                                                                            õK # Remove all empty string (and output it implicitly)
                                                                            # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]





                                                                            share|improve this answer
























                                                                              up vote
                                                                              2
                                                                              down vote










                                                                              up vote
                                                                              2
                                                                              down vote










                                                                              05AB1E, 13 11 bytes



                                                                              ΔRøćRˆ}¯˜õK


                                                                              -2 bytes thanks to @Emigna.



                                                                              Try it online or verify all test cases.



                                                                              Explanation:





                                                                              Δ # Loop until the stack no longer changes:
                                                                              R # Reverse the order of the rows
                                                                              # i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
                                                                              # → [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
                                                                              ø # Zip, swapping rows and column
                                                                              # → [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
                                                                              ć # Head extracted
                                                                              # → [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
                                                                              R # Reverse this row
                                                                              # → [1,5,9,13]
                                                                              ˆ # Pop and push it to the global array
                                                                              } # After the loop:
                                                                              ¯ # Push the global array
                                                                              # i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
                                                                              ˜ # Flatten it
                                                                              # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
                                                                              õK # Remove all empty string (and output it implicitly)
                                                                              # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]





                                                                              share|improve this answer















                                                                              05AB1E, 13 11 bytes



                                                                              ΔRøćRˆ}¯˜õK


                                                                              -2 bytes thanks to @Emigna.



                                                                              Try it online or verify all test cases.



                                                                              Explanation:





                                                                              Δ # Loop until the stack no longer changes:
                                                                              R # Reverse the order of the rows
                                                                              # i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
                                                                              # → [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
                                                                              ø # Zip, swapping rows and column
                                                                              # → [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
                                                                              ć # Head extracted
                                                                              # → [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
                                                                              R # Reverse this row
                                                                              # → [1,5,9,13]
                                                                              ˆ # Pop and push it to the global array
                                                                              } # After the loop:
                                                                              ¯ # Push the global array
                                                                              # i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
                                                                              ˜ # Flatten it
                                                                              # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
                                                                              õK # Remove all empty string (and output it implicitly)
                                                                              # → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]






                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited 8 hours ago

























                                                                              answered 9 hours ago









                                                                              Kevin Cruijssen

                                                                              31.7k553171




                                                                              31.7k553171




















                                                                                  up vote
                                                                                  2
                                                                                  down vote














                                                                                  Julia 1.0, 50 bytes





                                                                                  f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]


                                                                                  Try it online!



                                                                                  Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
                                                                                  Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.



                                                                                  E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):



                                                                                  f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]


                                                                                  However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!






                                                                                  share|improve this answer


























                                                                                    up vote
                                                                                    2
                                                                                    down vote














                                                                                    Julia 1.0, 50 bytes





                                                                                    f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]


                                                                                    Try it online!



                                                                                    Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
                                                                                    Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.



                                                                                    E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):



                                                                                    f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]


                                                                                    However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!






                                                                                    share|improve this answer
























                                                                                      up vote
                                                                                      2
                                                                                      down vote










                                                                                      up vote
                                                                                      2
                                                                                      down vote










                                                                                      Julia 1.0, 50 bytes





                                                                                      f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]


                                                                                      Try it online!



                                                                                      Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
                                                                                      Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.



                                                                                      E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):



                                                                                      f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]


                                                                                      However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!






                                                                                      share|improve this answer















                                                                                      Julia 1.0, 50 bytes





                                                                                      f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]


                                                                                      Try it online!



                                                                                      Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
                                                                                      Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.



                                                                                      E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):



                                                                                      f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]


                                                                                      However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!







                                                                                      share|improve this answer














                                                                                      share|improve this answer



                                                                                      share|improve this answer








                                                                                      edited 3 hours ago

























                                                                                      answered 3 hours ago









                                                                                      Kirill L.

                                                                                      2,8961117




                                                                                      2,8961117




















                                                                                          up vote
                                                                                          1
                                                                                          down vote














                                                                                          Jelly, 9 bytes



                                                                                          ZḊUƊƬḢ€€Ẏ


                                                                                          Try it online!






                                                                                          share|improve this answer
























                                                                                            up vote
                                                                                            1
                                                                                            down vote














                                                                                            Jelly, 9 bytes



                                                                                            ZḊUƊƬḢ€€Ẏ


                                                                                            Try it online!






                                                                                            share|improve this answer






















                                                                                              up vote
                                                                                              1
                                                                                              down vote










                                                                                              up vote
                                                                                              1
                                                                                              down vote










                                                                                              Jelly, 9 bytes



                                                                                              ZḊUƊƬḢ€€Ẏ


                                                                                              Try it online!






                                                                                              share|improve this answer













                                                                                              Jelly, 9 bytes



                                                                                              ZḊUƊƬḢ€€Ẏ


                                                                                              Try it online!







                                                                                              share|improve this answer












                                                                                              share|improve this answer



                                                                                              share|improve this answer










                                                                                              answered 23 hours ago









                                                                                              Erik the Outgolfer

                                                                                              29.8k42899




                                                                                              29.8k42899




















                                                                                                  up vote
                                                                                                  1
                                                                                                  down vote














                                                                                                  Charcoal, 25 bytes



                                                                                                  ≔⮌EA⮌ιθWθ«≔E§θ⁰⮌Eθ§μλθI⊟θ


                                                                                                  Try it online! Link is to verbose version of code. Explanation:



                                                                                                  ≔⮌EA⮌ιθ


                                                                                                  Rotate the input by 180°. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)



                                                                                                  Wθ«


                                                                                                  Repeat until the array is empty.



                                                                                                  ≔E§θ⁰⮌Eθ§μλθ


                                                                                                  Rotate the array by 90°.



                                                                                                  I⊟θ


                                                                                                  Remove the last row of the array and print the element as strings on separate lines.






                                                                                                  share|improve this answer


























                                                                                                    up vote
                                                                                                    1
                                                                                                    down vote














                                                                                                    Charcoal, 25 bytes



                                                                                                    ≔⮌EA⮌ιθWθ«≔E§θ⁰⮌Eθ§μλθI⊟θ


                                                                                                    Try it online! Link is to verbose version of code. Explanation:



                                                                                                    ≔⮌EA⮌ιθ


                                                                                                    Rotate the input by 180°. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)



                                                                                                    Wθ«


                                                                                                    Repeat until the array is empty.



                                                                                                    ≔E§θ⁰⮌Eθ§μλθ


                                                                                                    Rotate the array by 90°.



                                                                                                    I⊟θ


                                                                                                    Remove the last row of the array and print the element as strings on separate lines.






                                                                                                    share|improve this answer
























                                                                                                      up vote
                                                                                                      1
                                                                                                      down vote










                                                                                                      up vote
                                                                                                      1
                                                                                                      down vote










                                                                                                      Charcoal, 25 bytes



                                                                                                      ≔⮌EA⮌ιθWθ«≔E§θ⁰⮌Eθ§μλθI⊟θ


                                                                                                      Try it online! Link is to verbose version of code. Explanation:



                                                                                                      ≔⮌EA⮌ιθ


                                                                                                      Rotate the input by 180°. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)



                                                                                                      Wθ«


                                                                                                      Repeat until the array is empty.



                                                                                                      ≔E§θ⁰⮌Eθ§μλθ


                                                                                                      Rotate the array by 90°.



                                                                                                      I⊟θ


                                                                                                      Remove the last row of the array and print the element as strings on separate lines.






                                                                                                      share|improve this answer















                                                                                                      Charcoal, 25 bytes



                                                                                                      ≔⮌EA⮌ιθWθ«≔E§θ⁰⮌Eθ§μλθI⊟θ


                                                                                                      Try it online! Link is to verbose version of code. Explanation:



                                                                                                      ≔⮌EA⮌ιθ


                                                                                                      Rotate the input by 180°. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)



                                                                                                      Wθ«


                                                                                                      Repeat until the array is empty.



                                                                                                      ≔E§θ⁰⮌Eθ§μλθ


                                                                                                      Rotate the array by 90°.



                                                                                                      I⊟θ


                                                                                                      Remove the last row of the array and print the element as strings on separate lines.







                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited 7 hours ago

























                                                                                                      answered 21 hours ago









                                                                                                      Neil

                                                                                                      76.4k744173




                                                                                                      76.4k744173




















                                                                                                          up vote
                                                                                                          1
                                                                                                          down vote














                                                                                                          Ruby, 65 bytes





                                                                                                          ->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x


                                                                                                          Try it online!






                                                                                                          share|improve this answer
























                                                                                                            up vote
                                                                                                            1
                                                                                                            down vote














                                                                                                            Ruby, 65 bytes





                                                                                                            ->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x


                                                                                                            Try it online!






                                                                                                            share|improve this answer






















                                                                                                              up vote
                                                                                                              1
                                                                                                              down vote










                                                                                                              up vote
                                                                                                              1
                                                                                                              down vote










                                                                                                              Ruby, 65 bytes





                                                                                                              ->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x


                                                                                                              Try it online!






                                                                                                              share|improve this answer













                                                                                                              Ruby, 65 bytes





                                                                                                              ->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x


                                                                                                              Try it online!







                                                                                                              share|improve this answer












                                                                                                              share|improve this answer



                                                                                                              share|improve this answer










                                                                                                              answered 5 hours ago









                                                                                                              G B

                                                                                                              6,9871326




                                                                                                              6,9871326




















                                                                                                                  up vote
                                                                                                                  1
                                                                                                                  down vote














                                                                                                                  APL (Dyalog), 24 22 bytes





                                                                                                                  ×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬


                                                                                                                  Try it online!






                                                                                                                  share|improve this answer
























                                                                                                                    up vote
                                                                                                                    1
                                                                                                                    down vote














                                                                                                                    APL (Dyalog), 24 22 bytes





                                                                                                                    ×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬


                                                                                                                    Try it online!






                                                                                                                    share|improve this answer






















                                                                                                                      up vote
                                                                                                                      1
                                                                                                                      down vote










                                                                                                                      up vote
                                                                                                                      1
                                                                                                                      down vote










                                                                                                                      APL (Dyalog), 24 22 bytes





                                                                                                                      ×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬


                                                                                                                      Try it online!






                                                                                                                      share|improve this answer













                                                                                                                      APL (Dyalog), 24 22 bytes





                                                                                                                      ×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬


                                                                                                                      Try it online!







                                                                                                                      share|improve this answer












                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer










                                                                                                                      answered 2 hours ago









                                                                                                                      Zacharý

                                                                                                                      4,90511035




                                                                                                                      4,90511035



























                                                                                                                           

                                                                                                                          draft saved


                                                                                                                          draft discarded















































                                                                                                                           


                                                                                                                          draft saved


                                                                                                                          draft discarded














                                                                                                                          StackExchange.ready(
                                                                                                                          function ()
                                                                                                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173869%2foutput-the-anti-clockwise-inward-spiral-of-a-2d-array%23new-answer', 'question_page');

                                                                                                                          );

                                                                                                                          Post as a guest













































































                                                                                                                          Comments

                                                                                                                          Popular posts from this blog

                                                                                                                          What does second last employer means? [closed]

                                                                                                                          Installing NextGIS Connect into QGIS 3?

                                                                                                                          One-line joke