Discussion
· Feb 13

Code Golf: Clockwise Spiral

It's been a while (and everyone is well-rested after Advent Of Code!) so let's run another round of Code Golf.

Your task is navigating in a grid-like labyrinth in a clockwise spiral pattern. As it traverses the matrix, it collects characters, revealing a secret message.
Your challenge: find the shortest, most elegant code to decode this spiral cipher.
Input:
1. A multidimensional string array with comma separated characters (n x n)
2. Starting coordinates X and Y

Output:
The decoded message as a single string

Constraints:
1. 1 ≤ n ≤ 100
2. The starting position is always valid within the matrix
3. The matrix contains only printable ASCII characters

Example:

Input:

 matrix(1)="H,E,L"
 matrix(2)="R,L,L"
 matrix(3)="O,W,O"

Starting position: (1, 1)

Output: "HELLOWORL"

Note

Rules

  1. The signature of the contest entry MUST be:

    Class codeGolf.ClockwiseWord
    {
    
    ClassMethod Solution(ByRef matrix, x, y) As %String
    {
        // Your solution here
    }
    
    }
    
  2. It is forbidden to modify class/signature, including but not limited to:

    • Adding inheritance
    • Setting default argument values
    • Adding class elements (Parameters, Methods, Includes, etc).
  3. It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:

    ClassMethod Build(f As %Integer)
    {
      W ##class(myPackage.myClass).test(a)
    }
    
  4. The use of $ZWPACK and $ZWBPACK is also discouraged.

  5. You can use this test case:

    Class codeGolf.unittests.ClockwiseWord Extends %UnitTest.TestCase
    {
        Method TestSolutions()
        {
            Set matrix($Increment(matrix)) = "H,E,L"
            Set matrix($Increment(matrix)) = "R,L,L"
            Set matrix($Increment(matrix)) = "O,W,O"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 1, 1), "HELLOWORL")
        }
    
        Method TestStartTopRight()
        {
            Set matrix($Increment(matrix)) = "A,B,C"
            Set matrix($Increment(matrix)) = "H,I,D"
            Set matrix($Increment(matrix)) = "G,F,E"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 1, 3), "CDEFGHABI")
        }
    
        Method TestStartMiddleRight()
        {
            Set matrix($Increment(matrix)) = "A,B,C"
            Set matrix($Increment(matrix)) = "H,I,D"
            Set matrix($Increment(matrix)) = "G,F,E"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 2, 3), "DEFGHABI")
        }
    
        Method Test1x1Matrix()
        {
            Set matrix($Increment(matrix)) = "A"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 1, 1), "A")
        }
    
        Method Test4x4Matrix()
        {
            Set matrix($Increment(matrix)) = "C,O,D,E"
            Set matrix($Increment(matrix)) = "U,C,H,G"
            Set matrix($Increment(matrix)) = "M,U,F,O"
            Set matrix($Increment(matrix)) = "S,I,F,L"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 1, 1), "CODEGOLFISMUCHFU")
        }
    
        Method TestStartBottomLeft()
        {
            Set matrix($Increment(matrix)) = "1,2,3"
            Set matrix($Increment(matrix)) = "8,9,4"
            Set matrix($Increment(matrix)) = "7,6,5"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 1, 3), "781234569")
        }
    
        Method TestAllSameCharacters()
        {
            Set matrix($Increment(matrix)) = "X,X"
            Set matrix($Increment(matrix)) = "X,X"
            Do $$$AssertEquals(##class(codeGolf.ClockwiseWord).Solution(.matrix, 1, 1), "XXXX")
        }
    }
    
Discussion (2)1
Log in or sign up to continue

You talk about a multidimensional matrix but obviously mean a two dimensional matrix - right?
You talk about a matrix of size: N x N but neither the given code signaure nor the task description specify where the value N is given. In your examples you create the matrix by continuous incrementing the root node of matrix - the root node is equal to N, is this always valid or just in your examples or in other words, would this be a valid call:

kill box
set box(1)="A,B"
set box(2)="C,D"

do ##class(codeGolf.ClockwiseWord).Solution(.box,1,1)

I know, I one can obtain the value for N with a simple $order()

set N = $order(matrix(""),-1)

You expect a correct solution, we expect correct a description
justmy2cents