This is the robber's thread. The cop's thread is here.

A snake matrix is a square matrix that follows this pattern:


1  2  3
6  5  4
7  8  9

and 4-by-4:

1  2  3  4
8  7  6  5
9  10 11 12
16 15 14 13

Your task is to write a code that takes an input n and creates such a matrix, in the same language as a cop post, and with a code that matches the cop's regex. The output format of your code must match the output format of the cop's code.

Please leave a comment under the Cop's post to indicate that you have cracked it.

Winning criterion:

The winner will be the user that has cracked the most submissions. In case of a tie, then there will be multiple winners.


41 Answers 41


Jelly, 9 bytes, cracks @Dennis' answer


Try it online!

Should be correct now; I had to rather rethink what I was doing.


The hardest part of this problem is getting a list of the even indices inside the ¦ (which applies an operation at specific indices). Unlike most operations, which use the input to the whole program as a default for the second operand, ¦ uses the most recently seen value as a default (because it conceptually has two left operands, rather than a left and a right operand like most things that take in two values).

However, we can observe that we have a list of numbers, including all integers from 1 to half the input, already in the current value at the time. As such, flattening and doubling it gives us a list of even numbers, including all the even indices (also some other even numbers but we don't care about those). It's even possible to spend one µ solely to avoid a parsing ambiguity and still come in within 9 characters.

 s         Split
²          {the list of numbers from 1 to} the square of {the input}
  µ        into a number of pieces equal to {the input};
   U       then reverse
       ¦   the elements at indexes
    F      obtained by flattening the split list
      $    and
     Ḥ     doubling each element in the flattened list;
        G  finally, format it into a grid.         
  • \$\begingroup\$ Uh, the way ¦ works kills me every time, I tried this yesterday but with rather than U and decided the was not working out. \$\endgroup\$ Commented Mar 9, 2017 at 12:25
  • \$\begingroup\$ Nice, I nearly had it but got stuck at selecting every other element as well. F was a great idea, \$\endgroup\$ Commented Mar 9, 2017 at 12:38
  • \$\begingroup\$ My original code is pretty much identical to this. I just used J instead of F. \$\endgroup\$
    – Dennis
    Commented Mar 9, 2017 at 12:39
  • \$\begingroup\$ @Dennis Oh, J... I had tried with LR but couldn't get it under 11 bytes \$\endgroup\$ Commented Mar 9, 2017 at 12:41

05AB1E, Emigna

This was my first time using 05AB1E. Got it with a little bit of help. That was fun. :)


Try it online


U                       Assign input to X
 XF                     For N in range(0,X):
   XL                   Push list [1 .. X]
     NX*+               Add N*X to each element of the list
         N2BSR1k_       Super clever way to get N%2:
                            not bool(reversed(str(bin(N))).index('1')) (N%2 is odd)
                 iR}    if N%2 is odd, then reverse the list
                    ˆ   Add row to global array
                        Implicit print

I actually found this similar program on my own, but the output format is different:


Try it online

[1, 2, 3, 4]
[8, 7, 6, 5]
[9, 10, 11, 12]
[16, 15, 14, 13]

See the edit history for my previous two attempts.

  • \$\begingroup\$ Nice job! Quite similar to the original solution. \$\endgroup\$
    – Emigna
    Commented Mar 8, 2017 at 22:55
  • 5
    \$\begingroup\$ not bool(reversed(str(bin(N))).index('1'))... I think that's the most absurd way I've seen anyone do a N%2 operation. \$\endgroup\$ Commented Mar 9, 2017 at 7:29
  • 3
    \$\begingroup\$ @StewieGriffin When life gives you lemons but no water or sugar, you just have to eat them raw. :D \$\endgroup\$
    – mbomb007
    Commented Mar 9, 2017 at 14:24

Python 2, Dennis

This is a fun token golfing problem.

while ord>eval:1;int,eval=int<1and(int,eval+1)or(-input(),1);i=int;ord=i*i;print'%*s'%(-i,(eval+~i+~-eval%-i*~1,eval)[eval/i&1])+'\nt'[:-1<eval%int],

Regex verification


Ohm, Nick Clifford

My first time attempting Ohm.
Really nice language that I look forward to use again :)



²             # square input
 @            # range [1 ... input^2]
  ┼σ          # split in input sized chunks
    ▓         # perform the following block on each element of the array
     _        # current element
      ^è?     # if index is odd
         R    # reverse

My first attempt that didn't work as adding an array and an int is not possible:


My second attempt that didn't match the regex:

  • \$\begingroup\$ You did it exactly how I did! Great job! \$\endgroup\$ Commented Mar 9, 2017 at 12:50

05AB1E, Emigna (2nd submission)

First time working with 05AB1E.


Try it online! | Regex verification


VYLUYFYXDˆ+RU  # Implicit input
V              # Save input to Y
 YL            # Push [1 .. Y]
   U           # Save list to X
    YF         # Repeat Y times:
      YX       # Push Y, then X
        Dˆ     # Add X into the global array (could've used X here instead)
          +    # Push X + Y
           R   # Reverse top of stack
            U  # Save updated list to X
               # Implicit loop end
               # Implicit global array print if stack is empty
  • \$\begingroup\$ Nice job! You got the intended solution :) \$\endgroup\$
    – Emigna
    Commented Mar 10, 2017 at 21:11
  • \$\begingroup\$ @Emigna thanks! I wouldn't have gotten it if it weren't for the other solutions on here (yours included) that let me realize that the global array will print if the stack is empty! If it weren't for that I'd never have figured it out; I kept trying to do tricks that ended with ) thinking it'd be the only way to get the correct result on the stack. \$\endgroup\$
    – Value Ink
    Commented Mar 10, 2017 at 23:03
  • \$\begingroup\$ Yeah it's pretty impossible to do with ) when you can only use 2 non-alphanumeric characters. The tricky part here was meant to be structuring the program to only use 2 wildcards and have them sequential. Would likely have been a bit harder without the other solutions but there should have been some puzzling involved :) \$\endgroup\$
    – Emigna
    Commented Mar 11, 2017 at 8:53
  • \$\begingroup\$ @Emigna What I want to know is if ^\w*..$ is possible. \$\endgroup\$
    – mbomb007
    Commented Mar 14, 2017 at 16:55
  • \$\begingroup\$ @mbomb007: I don't think so. With this tactic you'd need to save the result of the addition for the next iteration and you can't use the stack for this meaning that one of UV need to come after . I can't think of another way to do it with only 2 wildcards at the end either. It can be done with 3 wildcards though. \$\endgroup\$
    – Emigna
    Commented Mar 14, 2017 at 17:03

CJam, Lynn


All linefeeds are for cosmetic purposes and can be removed without affecting the program.

Try it online!


After Lynn removed {|} from the list of allowed characters, I had to try something new. It turns out we can still construct arbitrary strings and evaluate them as code.

First, we need to get some value onto the stack. The only available built-ins that push something without popping something else first (and without reading the input) are es, ea and et. I'm sure you could start from all of these one way or another, but I went with es which pushes the current timestamp. Since I didn't want to make any assumptions about its actual value, I test its primality with mp (which gives 0 and 1) and test that value's primality again to ensure I've got a 0 on the stack. A 1 will be more useful, so we compute exp(0) with me and turn it into an integer with i. So all the numbers start with:


Now we've got a whole bunch of unary maths operators to work with:

i    int(x) (floor for positive numbers, ceiling for negative)
me   exp(x)
ml   ln(x)
mq   sqrt(x)
mo   round(x)
mt   tan(x)

We can also combine a few built-ins for more elaborate functions of x:

sci     Extract first digit of x and add 48 (convert to string, convert
        to character, convert to integer).
ceui    Convert to character, convert to upper case, convert to integer.
celi    Convert to character, convert to lower case, convert to integer.
mfsi    Get a sorted list of prime factors of x and concatenate them into 
        a new number.
mfseei  Get a sorted list of prime factors, interleave it with 1,2,3,..., and
        concatenate the result into a new number.

Using these, we can obtain any number in 0 <= x < 128 (and many others) in less than 10 steps from 1. I'm sure a much smaller subset of these commands would also suffice. I've written a small Mathematica program, to determine all of these snippets (it's not very readable, sorry):

codes = SortBy[
  Select[Nest[Select[DeleteDuplicatesBy[SortBy[Join @@ (Through[{
               If[1 <= # < 50, {Exp@#, #2 <> "me"}, Nothing] &,
               If[# >= 1, {Log@#, #2 <> "ml"}, Nothing] &,
               If[# > 1, {Sqrt@#, #2 <> "mq"}, Nothing] &,
               {If[# > 0, Floor@#, Ceiling@#], #2 <> "i"} &,
               {Floor[# + 1/2], #2 <> "mo"} &,
               {Tan@#, #2 <> "mt"} &,               
               If[NumberQ@# && # >= 0, {First@
                   ToCharacterCode@ToString@#, #2 <> "sci"}, 
                 Nothing] &,
               If[IntegerQ@# && 
                  32 < # < 65536, {First@
                    ToUpperCase@FromCharacterCode@#, #2 <> "ceui"}, 
                 Nothing] &,
               If[IntegerQ@# && 
                  32 < # < 65536, {First@
                    ToLowerCase@FromCharacterCode@#, #2 <> "celi"}, 
                 Nothing] &,
               If[IntegerQ@# && # > 0, ## & @@ {
                    "" <> (ToString /@ (f = 
                    Join @@ Table @@@ FactorInteger@#))], #2 <> 
                    "" <> (ToString /@ 
                    MapIndexed[## & @@ {#2[[1]] - 1, #} &, f])], #2 <>
                   }, Nothing] &
               }@##] &) @@@ #, StringLength@*Last], 
       First], #[[1]] < 1000000 &] &, {{1, "esmpmpmei"}}, 9], 
   IntegerQ@# && 0 <= # < 128 &@*First], First]

With that, we can simply push an arbitrary list of character codes, converting each of them to a character with c afterwards. Once we've pushed the entire code we want to execute, we push 95 (]). We eval that one with ~ to wrap all the others in a string, and then we eval that string with ~.

The actual code run at the end of the program is again:


See my previous solution for an explanation.


Python 3, TuukkaX

Sorry, the regex you used was too easy to trivialize. No 0, # or ? No problem!

I might've misinterpreted the example output but it's still pretty easy to tweak since I have 45 spare characters left over

  • \$\begingroup\$ Nice! :D FYI: I'll create another one in Python, if you're interested to solve it ;) It'll be a bit longer, but the beauty lies in the uncrackfulness. \$\endgroup\$
    – Yytsi
    Commented Mar 9, 2017 at 9:17

R, MickyT


Tests cases:

> lets_make_a_snake(4)
1 2 3 4 
8 7 6 5 
9 10 11 12 
16 15 14 13 
> lets_make_a_snake(7)
1 2 3 4 5 6 7 
14 13 12 11 10 9 8 
15 16 17 18 19 20 21 
28 27 26 25 24 23 22 
29 30 31 32 33 34 35 
42 41 40 39 38 37 36 
43 44 45 46 47 48 49 

And regex confirmation: https://regex101.com/r/OB8ZIM/1

I also had:


which gives the same output and match the same regex.

  • 6
    \$\begingroup\$ lets_make_a_snake... I'd be surprised if that was the intended solution :P \$\endgroup\$ Commented Mar 9, 2017 at 9:48
  • \$\begingroup\$ @plannapus Great job . The first one is basically what I was intending using ` for ` and ` if `, but a lot better golfed than mine. \$\endgroup\$
    – MickyT
    Commented Mar 9, 2017 at 17:27

Röda, fergusq


This is an anonymous function that matches this PCRE regex: ^{(\|[^\/#\s]*){8}$.

Try it online!


Bash, @Marcos M

sort -n <(seq 1 $((n * n)) | xargs -n$n | sed -n 'p;n'; seq $((n * n)) 1 | xargs -n$n | sort -n | sed -n 'n;p')


sort -n <(               # feed the stdout of this subshell into stdin for `sort -n`
    seq 1 $((n * n)) |   #   generate 1 2 3 ... n²
        xargs -n$n |     #     add line break every n numbers
        sed -n 'p;n';    #     print only odd lines
    seq $((n * n)) 1 |   #   generate n² ... 3 2 1
        xargs -n$n |     #     add line break every n numbers
        sort -n |        #     reverse all lines (keeping each row decreasing)
        sed -n 'n;p'     #     print only even lines

The first part of the subcommand will generate 1 2 3 4, 9 10 11 12, and the second part will generate 8 7 6 5, 16 15 14 13. The outer sort -n will properly mix them together to form the snake pattern.

I used the trick in https://superuser.com/a/101760 to print the odd and even lines. Thank you Marcos, really a fun one.

  • \$\begingroup\$ Very nice solution \$\endgroup\$ Commented Mar 11, 2017 at 18:13

Javascript, Tom




Python 3, @TuukkaX

n=int(input());j=0;exec("print([(j-+i%n-n++2*n-0,j+i%n+1)[1&int(i/n)//1^(0x1)//1]*(2*(i%n)*0+2222222//2222222)for i in range(j,j+n)]);j+=n;"*n)

Slightly analyzing the cop's regex shows a fixed template:

________________________"___________i%n____2*n-____i%n__________i/n)//1_____)//1___2*(i%n)____^^^^^^^^^^^^^^^^for i in range(j,____])______"*n)

where _ is any character except [ '"#] and ^ is any of [int()2/].

The "*n) at the end clearly shows an eval("..."*n) or exec("..."*n) is going on, so we just need to make sure the "..." prints the j-th row.

The for i in range(j, is too close to the end of the string, hinting list comprehension without any if. So we need to construct the i-th column using those i%n, 2*n stuff.

n = int(input())
        j - +i%n - n ++ 2*n - 0,    # equivalent to (n + j - i%n) for the decreasing rows
        j + i%n + 1                 # equivalent to (j + i%n + 1 == i + 1) for the increasing rows
    )[1 & int(i/n)//1 ^ (0x1)//1]   # int(i/n)   ->    get row number 0, 1, 2, 3, ...; 
                                    # 1 & int(i/n)//1    ->    0 for increasing rows, 1 for decreasing rows, 
                                    # 1 & int(i/n)//1 ^ (0x1)//1    ->   flip 0 and 1
    * (2*(i%n)*0+2222222//2222222)  # multiply by the constant 1.
    for i in range(j,j+n)
]); j+=n; "*n)
  • \$\begingroup\$ Nice! Well, almost survived a week :D I'll post my original code. \$\endgroup\$
    – Yytsi
    Commented Mar 9, 2017 at 21:13

dc, Mitchell Spector

This was my first entry to a cops and robbers challenge, and I had a lot of fun. The regex needed to be matched was simple, ^[^# !]{59}$, which basically turned my job into a golfing one, without using those 3 characters. Initially I had difficulties getting below 60 bytes, but I cracked it in the end.


Try it online!


My code uses one loop with N 2 iterations, keeping a zero based counter (1D), and calculates what number needs to be printed based on the corresponding matrix row and column (r, c) coordinates.

Example of what I mean, if N = 4:

 0  1  2  3            (0,0) (0,1) (0,2) (0,3)             1  2  3  4
 4  5  6  7     ->     (1,0) (1,1) (1,2) (1,3)     ->      8  7  6  5
 8  9 10 11            (2,0) (2,1) (2,2) (2,3)             9 10 11 12
12 13 14 15            (3,0) (3,1) (3,2) (3,3)            16 15 14 13

It looks complicated, but the intermediary step is helpful. Plus, I've tried using 2 loops from the start, but I ended up above the regex character limit. Number generation at each iteration (zero based):

  • if r % 2 = 0 (normal row), n = (r * N) + c = counter
  • if r % 2 = 1 (reversed row), n = ((r + 1) * N) - c - 1 = counter + N - (2 * c) - 1

Or all at once, as one based numbering: n = counter + ((N - (2 * c) - 1) * (r % 2)); n++

?sN0             # read input, save as N, initialize iteration counter
[AP]sP           # macro 'P' that prints a newline (ASCII code 10 = A)
[                # start loop
    ddlN~        # push N, calculate row and column coordinates:
                 #r = int(counter / N), c = counter % N, '~' calculates both
    _2*lN+1-     # c is on top, so this does: N - (2 * c) - 1
    r2%*+        # now r is on top, do: (r % 2) * (previous result) + counter
    1+n32P       # do: n++, print space (ASCII code 32)
    1+           # increment counter
    dlN%0=P      # call macro 'P' every Nth printed number
    dlNd*>L      # if: N * N > counter, repeat loop
]dsLx            # this saves the loop to macro 'L', then executes it
qqq              # my script was shorter, so I added a bunch of quit commands to
                 #fit the regex limit. Use of comments ('#') was prohibited.
  • \$\begingroup\$ @MitchellSpector Here is my explanation. As you see, we both have literally the same algorithm, only I use the command ~ to calculate the row and column indexes in one go. But one of my previous attempts did calculate them separately as you did. Great minds think alike? :) \$\endgroup\$
    – seshoumara
    Commented Mar 10, 2017 at 18:46
  • 1
    \$\begingroup\$ Yes, it really is the same algorithm. I like your use of ~ to shorten the code. \$\endgroup\$ Commented Mar 10, 2017 at 19:15
  • \$\begingroup\$ I think you can shorten it by one byte if you use the square-root trick in the loop test at the end of the macro: ?sN0[AP]sP[ddlN~_2*lN+1-r2%*+1+n32P1+dlN%0=PdvlN>L]dsLx tio.run/nexus/… \$\endgroup\$ Commented Mar 10, 2017 at 19:23
  • \$\begingroup\$ @MitchellSpector You're right, and I did notice it when reading your explanation, but commented in the chat room instead. \$\endgroup\$
    – seshoumara
    Commented Mar 10, 2017 at 19:29
  • \$\begingroup\$ Yes, I see that now -- I hadn't looked at the chatroom this morning yet. \$\endgroup\$ Commented Mar 10, 2017 at 19:44

PowerShell, ConnorLSW


$mySnakeIndex=1;$seq=1..$args[0];$seq|%{$rowNum=$seq|%{($mySnakeIndex++)};if(!($_%2)){[array]::Reverse($rowNum)};$rowNum-join" "}

I started with a smaller solution to the issue and padded my variable names to get the regex to match. Trying to find a use for the colon I suppose was the hardest part to wrap my head around.

$a=1..$args[0];$i=1;$a|%{$r=$a|%{($i++)};if(!($_%2)){[array]::Reverse($r)};$r-join" "}


# Initialize a counter that starts at one.
# Save the integer array from 1 to the input value. 
# For each row of the output...
    # Build the integer array for this row sequentially
        # Increase the integer index while sending it down the pipeline
        # Check if this is and odd row. If so reverse the integer array.
        # Take this row and join all the numbers with spaces.
        $rowNum-join" "
  • \$\begingroup\$ Nice, I used a $script variable and some really messy looping to pad it out, the [array]::Reverse() was correct though, congrats - I think you might want to even out the length of $i and $MySnakeIndex though. \$\endgroup\$
    – colsw
    Commented Mar 10, 2017 at 21:22
  • \$\begingroup\$ @ConnorLSW I know you would not be able to sleep at night knowing I had errors in my robber. I have fixed it. \$\endgroup\$
    – Matt
    Commented Mar 11, 2017 at 1:23

CJam, Lynn

Something like this:




All whitespace is for... "readability"... and can be omitted to comply with Lynn's regex.

Try it online!


The regex requires that we solve problem using only:

  • Lower-case letters.
  • {}, which can be used to create blocks.
  • |, mostly used for bitwise OR.
  • ~, "eval" and bitwise NOT (also "dump array", but I'm not going to use it).

Since we have, ~, if we can construct arbitrary strings, we can run arbitrary code. However, at first it's not obvious how to do that.

The first piece of the puzzle is that blocks are unevaluated bits of code, which can turn into strings with s. So {abc}s gives us "{abc}". Next, we can use eu to convert these strings to upper case.

{abc}seu  e# gives "{ABC}"

The benefit of this is that upper-case letters are pre-initialised variables, so we can get a lot of constant values by creating such a string, and eval'ing it twice (once to turn the string back into a block and once to execute that block). We can't get all the letters, because some, like x aren't valid commands (so CJam will refuse to parse a block containing them). We can't use f as is, because it needs to be followed by another command, but we can use fb and then OR the two values together. Likewise, we can use ee instead of e. With that, we can get the numbers 0, -1, 3, and 10 to 19. The -1 is convenient, because if we turn it into a string ("-1") then into a character ('-) and then eval it, we can get either subtraction or set difference. Like I said, we can't get X (for 1), but we can take the absolute value of -1 with z.

We can also use s to get a string containing a space, and use c to turn that into a space character:


This is convenient, because from there we can get lots of useful commands in the lower ASCII range by ORing the space with various numbers. To get some of the characters above code point 48, we use the character '0 as the basis instead:


This is already enough to construct arbitrary strings, because we can get '+ (addition and string concatenation) from the following snippet:


And we have a literal 1 so we can just push space characters, increment them to the value we need and then concatenate them all together, but that's a bit boring and the code would become massive.

Instead, I've generated [ and ] and evalled them, so that all the characters I push in between are wrapped in a string automatically. That's these two lines:




And finally, we'll need f and ~ in the string we're generating. While those are valid characters already, we don't have string literals or character literals, so we'd have to generate these as well and building up larger code points from the space is a bit annoying. Instead, I've made use of the set subtraction here, but subtracting two blocks (to get rid of the {}):


That's pretty much all there is to it. We eval [. We push all the characters, obtained by various computations from the few built-in constants we have, |, - (via eval) and + (via eval). We eval ]. We flatten the entire thing into a string, because at some point I added some strings or numbers into the list. We eval our arbitrary string with ~.

The ri...p are part of the actual final program, but I've extracted them because they don't need encoding.

Finally, this is the program we're actually running:


ri      e# Read input and convert to integer.
__      e# Make two copies.
_*      e# Square the last copy.
,       e# Turn into range [0 1 ... n^2-1].
:)      e# Increment each to get [1 2 ... n^2].
/       e# Split into chunks of length n, creating a square.
2/      e# Split into pairs of lines.
[1-1]   e# Push [1 -1].
f.%     e# Use this to reverse the second line in each pair. If n was odd,
        e# this will pair a -1 with the last line.
:~      e# Flatten the pairs back into the square.
<       e# Truncate to n lines to get rid of that extraneous -1 for odd inputs.
p       e# Pretty-print.

tinylisp, @DLosc


Try it online!

This defines a function f that returns the answer. It also prints the names of the functions I defined to stdout, but I assume that doesn't matter, since [we're allowed to choose our output stream] for code golf, at least. If it's a big deal, I think I can tweak it to not print those. How did I do it? I started with something fairly standard:

(d f (q ((n)
  (g n (s n 1) ()))))

(d mod (q((x y)
  (i (l x y) x
    (mod (s x y) y)))))

(d range (q((x y z w)
  (i (l (times z x 0) (times z y 0))
    (range x (s y z) z (c (s y z) w))

(d times (q ((x y acc)
  (i (l x 0) (times (s 0 x) (s 0 y) acc)
  (i (e x 0) acc
    (times (s x 1) y (a y acc)))))))

(d g (q ((n w r)
  (i (l w 0) r
    (g n (s w 1)
       (c (i (e 0 (mod w 2))
             (range (a 1 (times w n 0)) (a (a 1 (times w n 0)) n) 1 ())
             (range (a (times w n 0) n) (times w n 0) (s 0 1) ()))

Then, I observed that one can transform functions definitions like this:

(d mod (q ((x y) (body))))



And function calls like this:

(a x y)



I used this recursive Vim macro, stored in register q, to do the second one (I have jk mapped to <Esc>): f s(v(h(q(jkea))))jk@q.

These two transformations were sufficient to eliminate all spaces.

  • \$\begingroup\$ Nicely done! My original code is quite similar, though I wrote a helper macro to make it a bit less painful to write. I'm curious as to your thoughts on how to avoid printing the defined symbols--if you'd care to share them, I'll be in the tinylisp chat room. \$\endgroup\$
    – DLosc
    Commented Mar 16, 2017 at 7:03
  • \$\begingroup\$ @DLosc Done, I posted there. \$\endgroup\$ Commented Mar 16, 2017 at 16:43

Swift, @James Webster

{n in for j in 0..<n{print((1...n).map{j%2==0 ?$0+j*n:j*n+n+1-$0})}} as(CShort)->()

Verification: https://regex101.com/r/7ukrM0/1

  • \$\begingroup\$ I get "Expression resolves to an unused function" with this on its own, but I'm unfamiliar with everything that makes an acceptable answer. Is the e.g. `let a = <code>; a(CShort(4)); required to make it run not necessary? \$\endgroup\$ Commented Mar 9, 2017 at 13:11
  • \$\begingroup\$ @JamesWebster I'm not sure about the exact rules since "a code" is pretty ambiguous. For code-golf the submission can be a program or function, so I just provide an anonymous function here. How else do we receive the input arguments? \$\endgroup\$
    – kennytm
    Commented Mar 9, 2017 at 13:17
  • \$\begingroup\$ My original was the body of a function, so not sure mine is valid either! So I'm just gonna go with "Sure, this is fine" and upvote it. :) \$\endgroup\$ Commented Mar 9, 2017 at 13:18
  • \$\begingroup\$ @JamesWebster You can call the whole thing as (…)(4), no need to cast the integer literal to CShort. \$\endgroup\$
    – kennytm
    Commented Mar 9, 2017 at 13:18
  • \$\begingroup\$ Ah yeah! I would never have thought of that. \$\endgroup\$ Commented Mar 9, 2017 at 13:19

PHP, @JörgHülsermann


221 bytes is too long (thus the snake), and the lack of whitespace can be easily worked-around.


    function($n, $snake) {
        foreach (array_chunk(range(1, $n*$n), $n) as $i => $a) {
            if($i % 2)
                $a = array_reverse($a);
            echo "\n", join('', array_map(function($e) {
                return (sprintf("%3d", $e));
            }, $a));
)($argv[1], '-=-=-=-=-=-=-=-=-=-=-=-=-=-o~')
  • \$\begingroup\$ Nice variant. My fault was not to think about a function and make only one output \$\endgroup\$ Commented Mar 9, 2017 at 14:11

Jelly, length 12, @JonathanAllan


Try it online!

How it works

Ḷ-*m@"s@²$G  Main link. Argument: n

Ḷ            Unlength; yield [0, ..., n-1].
 -*          Yield [(-1)**0, ..., (-1)**(n-1)].
         $   Combine the two links to the left into a chain.
        ²    Yield n².
      s@     Split [1, ..., n²] into chunks of length n.
   m@"       Take the lists to the right modulo the units to the left, 1 being
             normal order and -1 being reversed.

Jelly, 12 bytes, cracks @JonathanAllan's second answer


Try it online!


This is almost the same as my other answer. I just made two changes:

First, I changed U ("reverse each element") to Ṛ€ ("reverse" "each element"). That doesn't help by itself, because is also banned.

Then, I changed the ("reverse") to ;@/ (/ "fold by" ; "concatenating" @ "in the opposite order to the original list"). That avoids all the banned characters, giving a valid solution.

I assume the next step would be to start banning array-manipulation quicks, in addition to the atoms.

  • \$\begingroup\$ Uh, I knew I should have banned ... \$\endgroup\$ Commented Mar 10, 2017 at 0:35
  • \$\begingroup\$ You can write in terms of / too. It's just rather more verbose than this solution. \$\endgroup\$
    – user62131
    Commented Mar 10, 2017 at 0:39
  • \$\begingroup\$ Yep and one could ;@\ṫ0, the regex is getting long. \$\endgroup\$ Commented Mar 10, 2017 at 0:44

Jelly, length 13, @JonathanAllan


Try it online!

How it works

1r-*Nm@"s@²$G  Main link. Argument: n

1r             Range 1; yield [1, ..., n].
 -*            Yield [(-1)**1, ..., (-1)**n].
   N           Negate each unit.
           $   Combine the two links to the left into a chain.
          ²    Yield n².
        s@     Split [1, ..., n²] into chunks of length n.
     m@"       Take the lists to the right modulo the units to the left, 1 being
               normal order and -1 being reversed.
  • \$\begingroup\$ Oh wait, what, I missed m?! \$\endgroup\$ Commented Mar 10, 2017 at 1:49

Jelly, length 14, @JonathanAllan


Try it online!

  • \$\begingroup\$ That is impressive :D \$\endgroup\$ Commented Mar 10, 2017 at 2:57
  • \$\begingroup\$ Thanks! :) For some reason, I was convinced that my code couldn't contain any newlines, which forced me to discover some chain trickery. \$\endgroup\$
    – Dennis
    Commented Mar 10, 2017 at 3:03

Scala, @Soapy

def g(n:Int) = {
    var vec = Vector.fill(0)(Vector.fill(0)(1))
    for (p <- 1 to n) {
        var vec2 = Vector.fill(0)(1)
        for (q <- (p-1)*n+1 to p*n) {
            vec2 = vec2 ++ Vector(q)
        if (p%2==1) vec = vec ++ Vector(vec2)
        else vec = vec ++ Vector(vec2.reverse)


Haven't touched Scala in awhile, it was fun to revisit. Unfortunately, this solution misses out on a lot of Scala's cool features.

Try it out here

Regex Confirmation


QBasic (QB64), @DLosc

Note that since the . does not match \n (U+000A, LF), the newline here is a \r (U+000D, CR).



<_sre.SRE_Match object; span=(0, 141), match='INPUT N:ZERO=N-N:ONE=N/N:FOR I=ZERO TO N-ONE:FOR >

The main difficulty is how to insert a word after the ;. Thankfully, QB64 treats CR as a newline while Python's regex doesn't, so we could slip a REM\r here. Out of the five allowed regex flavors,

So this crack is fine as long as we don't mention JavaScript... 🤐

  • \$\begingroup\$ I'll accept this, since it works in QB64. However, I will also say that archive.org's QBasic (which I think is the actual QBasic rather than an emulation) complains about REM immediately following a statement with no statement separator. My original solution did not use comments. I've got another variation that I will be posting shortly. :D \$\endgroup\$
    – DLosc
    Commented Mar 11, 2017 at 23:11

Python 3, kennytm

def S(N,A=1,K=1,E=list,r=range):print(E(r(A,A+N)[::K]))or(A>N*N-N)or(((S(N,A+N,-K))))

Regex verification


><>, torcado

!v &0_!
?!v1+:n' 'o:&:&%_
?!v:n' 'o1-:&:&%_

C, @Yimin Rong

main(int c,char**p){int n=atoi(*++p),i=n-n,t,o=c;for(--o;i<n;++i)for(t=o;t<=n;++t)printf("%-*d%c",n-o,i%c?i*n+n+o-t:i*n+t,t%n?' ':'\n');}

The program cannot contain numbers, but we can get numbers via:

  1. c, commonly known as "argc", which is always 2.
  2. + and - are available, so we can create 0 with n-n, and create 1 with o=c;--o.
  • \$\begingroup\$ Minor issue, cop version was tab-delimited as opposed to space-justified, but no biggy. \$\endgroup\$
    – user15259
    Commented Mar 9, 2017 at 22:31

Ruby, @Value Ink


[(-=Z-~]* means "I can write anything I like :)"

  • \$\begingroup\$ Ah, I messed that up didn't I \$\endgroup\$
    – Value Ink
    Commented Mar 13, 2017 at 19:08

tinylisp, @DLosc

A very straightforward solution, and totally unoptimized :)

(d p(q((m n)(s m(s(s 1 1)n)))))(d j(q((f g)(i(l f g)(c f(j(p f 1) g))()))))(d r(q((f g)(i(l f g)(c(s g 1)(r f(s g 1)))()))))(d k(q((m o f g n)(i(l n g)()(c(m f(p f n))(k o m(p f n)(p g 1) n))))))(d f(q((n)(k j r 1 1 n))))

Call as (disp (f 4)).

  • (p m n) computes m + n using subtraction s (m + n == m - ((1 - 1) - n))
  • (j f g) generates (f f+1 f+2 ... g-1)
  • (r f g) generates (g-1 g-2 g-3 ... f)
  • (k m o f g n) generate one row of the snake matrix, and then recurve itself for the next row, until n rows are created. The arguments m, o are substituted by j/r to generate increasing or decreasing rows. The arguments f, g are running indices to know which row we are on.
  • (f n) calls (k j r 1 1 n) to start the generation.
  • \$\begingroup\$ Good work. (BTW, it's more idiomatic just to do (f 4)--the disp is implied.) \$\endgroup\$
    – DLosc
    Commented Mar 13, 2017 at 5:54

PHP, @Ionut Botizan

In the moment I have no better idea to crack the original solution.

Supports n<=15

It is the first time that I use getopt. Maybe not the best idea to use options as input.

start from the command line like this

php hack.php -a=4

Original Regex

Level 1:


Very nice combination of letters. Feel free to vote up the cops thread.

It blocks me functions like - strrev - array_reverse - get_defined_vars


Level 2:





Level 2:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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