User bio
404 bio not found
Member since May 6, 2021
Replies:

Hi Julius, I was playing Devil's "avocado 😁" with that. One could argue the case for that being the correct approach. One could also argue for Paul's unit tests based on seeing no reason why rotational symmetry couldn't apply. (I think my solution for that is 225 and doesn't beat my friend Paul's answer so I don't like it.) The point is that the spiral has to get smaller somewhere but we haven't been given a clear enough rule to follow. In the example in the question, the spiral only turns inward when it hits a used letter or a corner - a dead end as @Robert Cemper puts it. The unit tests below could also be valid. (My solution for this is 199)

There's an unwritten rule in the question that could be "as a human, make a decision when to turn inwards to make sense out of the order of letters". How do you code that?

        Set matrix($INCREMENT(matrix)) = "A,B,C"
        Set matrix($INCREMENT(matrix)) = "H,I,D"
        Set matrix($INCREMENT(matrix)) = "G,F,E"
        d $$$AssertEquals(..Solution(.matrix, 1, 1), "ABCDEFGHI")
        d $$$AssertEquals(..Solution(.matrix, 1, 2), "BCDEFGHA") // !!?
        d $$$AssertEquals(..Solution(.matrix, 1, 3), "CDEFGHABI")
        d $$$AssertEquals(..Solution(.matrix, 2, 3), "DEFGHABC") // !!?
        d $$$AssertEquals(..Solution(.matrix, 3, 3), "EFGHABCDI")
        d $$$AssertEquals(..Solution(.matrix, 3, 2), "FGHABCDE") // !!?
        d $$$AssertEquals(..Solution(.matrix, 3, 1), "GHABCDEFI")
        d $$$AssertEquals(..Solution(.matrix, 2, 1), "HABCDEFG") // !!?

You are correct Paul, but there's a problem with other tests too. The one with 3,1 above could return GHABCDI because you could argue that when the spiral leaves an outer edge it should never return to it. If the test that starts at 1,2 doesn't return to row 1 then why should 1,3 return to row 1 after leaving? These could be the correct tests:

        Set matrix($INCREMENT(matrix)) = "A,B,C"
        Set matrix($INCREMENT(matrix)) = "H,I,D"
        Set matrix($INCREMENT(matrix)) = "G,F,E"
        d $$$AssertEquals(..Solution(.matrix, 1, 1), "ABCDEFGHI")
        d $$$AssertEquals(..Solution(.matrix, 1, 2), "BCDEFGHI")
        d $$$AssertEquals(..Solution(.matrix, 1, 3), "CDEFGHI")
        d $$$AssertEquals(..Solution(.matrix, 2, 3), "DEFGHI")
        d $$$AssertEquals(..Solution(.matrix, 3, 3), "EFGHI")
        d $$$AssertEquals(..Solution(.matrix, 3, 2), "FGHI")
        d $$$AssertEquals(..Solution(.matrix, 3, 1), "GHI")
        d $$$AssertEquals(..Solution(.matrix, 2, 1), "HI")

It took me a minute or two to spot that you had gone with the valid assumption that n would be defined as the number of rows and columns from $I(matrix) which I didn't notice, and the matrix would be square.

I've gone for an interpretation that I think is equivalent to clockwise, the result is the same: I'm going East (right), South (down), West (left), North (up), then back to East again. Heading in a direction until a dead end then changing direction. As such I have variable V to indicate direction of movement V=1 for vertical (up or down, north or south) and V=0 for horizontal (left or right, west or east). Then another variable D, D=1 indicating increase and D=-1 for decrease. When you hit a dead end change V with V='V and if V becomes 0 then D=-D. That will always spin you clockwise but without limits so you need code to spot a dead end. Also, as others have said, there's no way to know which direction to start in. If you are in the bottom left corner do you start by going right or up?

Here's my code with comments:

ClassMethod Solution(ByRef m, x, y) As %String
{
 // enforcing a decreasing spiral, 210 without comments
 // Right means change y with V=0, D=1
 // Down means change x with V=1, D=1
 // Left means change y with V=0, D=-1
 // Up means change x with V=1 ,D=-1
 //
 // first letter starts the word
 w=$p(m(x),",",y),V=0,D=1
 // mark the position so it can't be re-used
 // get the next letter from $$n, end when no letter comes back, extend the word if it does, and repeat
n(x,y)=w,l=$$q:e=3 w=w_a
 // default start position to right and clockwise
 // quit if you've tried 2 directions
 // get the letter in that direction
 // if no letter or already got then change direction clockwise
 // to change direction, if currently vertical then you won't be for the next move and vice versa
 // if you are now horizontal then change direction
n() q:$i(e)=3 "" X=V*D+x,Y='V*D+y,l=$p($g(m(X)),",",Y) l=""!$d(n(X,Y)) V='S:'D=-$$n()
 // flag any anticlockwise letter as used
 n(V*-D+x,'V*-D+y)=0,x=X,y=Y,e=0 l
}
 

Certifications & Credly badges:
Stuart has no Certifications & Credly badges yet.
Global Masters badges:
Stuart has no Global Masters badges yet.
Followers:
Stuart has no followers yet.
Following:
Stuart has not followed anybody yet.