# Cops: Make a regex - Make a snake

Write a code that takes an input n and creates a n-by-n "snake matrix".

A snake matrix is a matrix that follows this pattern:

3-by-3:

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


The exact output format is optional. You may for instance output [[1 2 3],[6 5 4],[7 8 9]], or something similar.

You must provide the language name, and a regex that fully matches your code. You can choose how detailed your regex should be. In the extreme, you can write a regex that matches every possible string, in which case it will be very easy to crack your code. You must also provide the output for n=4, so that robbers know the exact format you have opted for.

You may use one of the regex-flavors that are available on regex101.com, or the Ruby flavor.

• PCRE (PHP)
• Javascript
• Python
• Golang
• Ruby

You must specify which one you are using.

Notes:

• You must support any reasonably large n. You may assume it won't overflow the datatype or memory. If the default datatype is 8-bit signed integers, then you can assume n<=11, if it's unsigned 8-bit integers, then you can assume n<=15.
• The robbers have to match the submission's output format, except leading/trailing spaces and newlines, since that might have been stripped away by the SE formatting.

Winning criterion:

The winner will be the uncracked submission with the shortest regex, measured in number of characters.

If your post has remained uncracked for 7 days, then you may post the intended solution and mark your submission as safe.

• Seed, length 1, .. – user41805 Mar 8 '17 at 16:04
• Can I use one of the languages documented here? codegolf.stackexchange.com/questions/61804/… – user15259 Mar 8 '17 at 16:09
• @KritixiLithos Except you have to release your original program to become safe ;-) – ETHproductions Mar 8 '17 at 17:50
• @DeepakAgarwal - Write your code to generate a snake, then provide a regex which matches it. The robber's solution must be in the same language and match the regex as well. So one strategy is to provide a restrictive regex, to make it hard on the robber, but not so restrictive that you give the solution! – user15259 Mar 8 '17 at 19:48
• Is this meta consensus allowing unary I/O for sed, that has no data types, be valid for this challenge? – seshoumara Mar 10 '17 at 14:42

# Python3, (PCRE) length 27 (Cracked!)

Regex: ^([^# 0]){115}.{6}(?1){24}$ My first Cops and Robbers submission. ## Output format 4: [1, 2, 3, 4] [8, 7, 6, 5] [9, 10, 11, 12] [16, 15, 14, 13]  Original code: n=int(input());j=0;exec("print([int(i%n+1+(2*n-(2*(i%n)+1))*((((i/n)//1+1)/2)//1)+(2*(i%n)+1)*int(int(i/n)/2))for i in range(j,j+n)]);j+=n;"*n) # Jelly, length 12 - cracked cracked by Dennis ^[^ḊḢṚUṖṪ]*$


Python regex (although I think all flavours are valid for this pattern).

That's right, none of those lovely array manipulation atoms!
No:

• Ḋequeue;
• Ḣead;
• Ṛeversal;
• Upend;
• Ṗop; or
• Ṫail;

For an input of 4 mine outputs:

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


...because I formatted a list of lists as a grid with G.

• you do need ^$ (not a crack) – CAD97 Mar 9 '17 at 19:54 • Cracked. – Dennis Mar 9 '17 at 21:11 • @CAD97 The challenge spec says it has to be a full match. – Dennis Mar 9 '17 at 21:12 • @Dennis oh, right... but then why does anyone have the ^$ – CAD97 Mar 9 '17 at 21:13
• regex101 refers to the full match of the regex, i.e., the whole substring it matches. That doesn't make the rrgex a full match though. – Dennis Mar 9 '17 at 21:20

# Jelly, length 13 cracked

cracked by ais523

[^ḊḢṚUmṖṪVv]*


Python regex (although I think all flavours are valid for this pattern).

An extension of my previously cracked entry:
I have banned modulo indexing, which I missed and Dennis cleverly used;
I have banned use of the two eval atoms, as they'd allow indirect usage of the banned atoms; and
I have removed the start and end match (^...$ since it is redundant since a full match is required). No: • Ḋ dequeue; • Ḣ head; • Ṛ reversal; • U upend; • m modulo indexing; • Ṗ pop; • Ṫ tail; • V eval; or • v dyadic eval For an input of 4 mine outputs:  1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13  ...because I formatted a list of lists as a grid with G. • Cracked. – user62131 Mar 10 '17 at 0:33 # Jelly, length 13 cracked cracked by Dennis (I forgot to keep m ... another incoming) [^/P-vḊ-ṫ€]*  Python regex (although I think all flavours are valid for this pattern). Severely increased the banned characters from my previous submission: / (reduce quick); from P (product) to  (monad from dyad quick); v (eval dyad); from Ḋ (dequeue) to ṫ (tail); and € (for each quick) For an input of 4 mine outputs:  1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13  ...because I formatted a list of lists as a grid with G. • Cracked. – Dennis Mar 10 '17 at 1:46 # Jelly, 15 bytes, Cracked Jelly seems to be quite popular in this challenge. So let's require the use of a totally different algorithm. The regex, PCRE interpreted in Jelly's encoding: [^œŒ]{12} Çœ?²s  Several PCRE matchers, such as pcregrep and even perl itself, will treat input by default as "unknown 8-bit character set compatible with ASCII"; therefore, they handle Jelly's encoding just fine. (That said, I suspect you need to use perl here as many regex matchers dislike newlines in the middle of the regex. I can't just write it as \n as that's a byte longer.) Result for an input of 4: [[1, 2, 3, 4], [8, 7, 6, 5], [9, 10, 11, 12], [16, 15, 14, 13]]  ## Crack This ended up being cracked due to a typo in the regex. I'll submit a fixed version. • Cracked. – Dennis Mar 10 '17 at 2:26 # 05AB1E, length 22 Cracked by Emigna This is probably too easy. Regex (PCRE) ^([E-Z0]*[ˆ>*}<]){7}$


Output

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


Original Solution

UXL0KVXLˆXGXN>*LYKZL0KVNFR}<>ˆ

• Cracked – Emigna Mar 10 '17 at 9:37
• Yup, it was definitely too easy. – mbomb007 Mar 10 '17 at 15:02

# tinylisp, regex length 10 (cracked)

You can test tinylisp code at Try it online!

### Regex

Any flavor should work the same, we'll call it Python flavor.

[( )1c-s]+


Basically this boils down to, "Solve the problem without using the library." If that turns out to be too easy, I may post a more restrictive one.

### Output

The solution is a function that takes a single integer argument and returns a list like this (for n=4):

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

• Cracked – kennytm Mar 12 '17 at 19:39

# Scala, length 40, Cracked

Regex (PCRE)

[\w\s]+[\w:]+[\w<\-\s{=;+*%\.}]+


Output (n=4)

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


Code

def x(y:Int)=for(u<-1 to y)yield{val q=for(z<-1 to y;r<- List(z+(y*(u-1))))yield r;if(u%2==1)q else q.reverse}


Ungolfed

def x(y:Int)=
for(u<-1 to y)
yield {
val q=for(z<-1 to y;r<- List(z+(y*(u-1)))) yield r;
if(u%2==1)
q
else
q.reverse
}


# PHP (Cracked)

### Output (n=4)

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


### PCRE (length=31)

^.{4}[-"$%&*+:;=?{}a-zMZ]+.{4}$


Here's another take on this challenge, this time using a whitelist approach.

The notable forbidden characters are (), [], _, digits and most of the upper case letters.

Looking forward to see what you come up with!

### THE ORIGINAL SOLUTION

This one was simpler. Since parenthesis were forbidden, you couldn't use function calls or language constructs like conditionals and loops which meant the infamous goto was the solution! >:D

• The only number my solution requires is 1, which I defined as +true.
• To format the output I used tab and newline characters, generated using bitwise operators on strings: "M" & "+" and "Z" & "*" Start by outputting 1
• Then, I looped through each number and calculated the difference between each column and it's complement, which I multiplied by 0 for odd rows and 1 for even rows and then added to the original number at that position.

So, for n=4, 5 would originally be on the first column (1) on an odd row.

The difference is -2 * $column +$n + 1 = 3, which means 5 becomes 5 + $difference *$multiplier = 5 + 3 * 1 = 8. So, the first column of row number 2 becomes 8;

A human readable version of my solution is

<?=

$one =$i = 1;
$multiplier =$one - $one;$n = $argv{$one};

loop:

++$i ==$n * $n +$one && die;

$column =$i % $n ?:$n;
$difference = -$column - $column +$n + $one; print "M"&"+";$i % $n ==$one && print "Z"&"*";

print $i +$difference * $multiplier; // if not at the end of the row, leave as is // else convert 0 to 1 and 1 to 0 to apply (or not) the above difference$multiplier = $i %$n ? $multiplier : -$multiplier + $one; goto loop ?>  My solution that matches the regex was: <?=$t=$i=+true;$f=$s=$t-$t;$n=$argv{$t};a:++$i==$n*$n+$t&&die;$b=$i%$n?:$n;$m=-$b-$b+$n+$t;print"M"&"+";$i%$n==$t&&print"Z"&"*";print$i+$m*$s;$s=$i%$n?$s:-$t*$s+$t;goto a?>

# Racket Intermediate Student with Lambda, regex length 3620 22, Cracked by kennytm

Produces a function that takes n as input and returns the answer as a list of lists.

^((?![nla][peo]|-).)*$ ### Output (list (list 1 2 3 4) (list 8 7 6 5) (list 9 10 11 12) (list 16 15 14 13))  • – kennytm Mar 13 '17 at 17:35 ## Actually, Python regex of length 193 (safe) ^[^\x00-\x08\x0d-\x13\x16-\x1d\x3f\xa6\xb6\xc1\xe9\xea\xfa\xff]{5}[\x00-\x08\x0d-\x13\x16-\x1d\x3f\xa6\xb6\xc1\xe9\xea\xfa\xff][^\x00-\x08\x0d-\x13\x16-\x1d\x3f\xa6\xb6\xc1\xe9\xea\xfa\xff]{6}$


This regex matches the CP437-encoded program source.

Output for n = 4:

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


Here is a Python 3 program to help with testing:

import re
pattern = re.compile(rb'[^\x00-\x08\x0d-\x13\x16-\x1d\x3f\xa6\xb6\xc1\xe9\xea\xfa\xff]{5}[\x00-\x08\x0d-\x13\x16-\x1d\x3f\xa6\xb6\xc1\xe9\xea\xfa\xff][^\x00-\x08\x0d-\x13\x16-\x1d\x3f\xa6\xb6\xc1\xe9\xea\xfa\xff]{6}')
code = b'code here'
print(pattern.match(code) and True or False)


Try it online!

# Actually, 12 bytes

;²R╡"?R"ß*♀ƒ


Try it online!

Explanation:

;²R╡"?R"ß*♀ƒ
;             duplicate input
²R           range(1, n**2+1)
╡          split into n-length chunks
"?R"ß*    "?R"*n
♀ƒ  for each chunk: reverse if index is odd (R) else do nothing (?)


# Go, length 17 cracked

Regex, PCRE:

### Original solution

(λ (n) (((((λ(f) ((λ (x) (x x)) (λ (x) (f (λ (y)((x x)y)))))) (λ (q) (λ(n) (λ(c) (λ(r) (cond [(< c 0)r][(= 1 1) (((q n) (- c 1))(cons(cond [(= 0((((λ (f) ((λ (x) (x x)) (λ (x) (f (λ (y)((x x)y)))))) (λ (r) (λ(a)(λ (b) (cond [(< a b)a][(= 1 1)((r(- a b)) b)])))))c)2))((((((λ (f) ((λ (x) (x x)) (λ (x) (f (λ (y)((x x)y)))))) (λ (r)(λ (s) (λ(e) (λ(t) (λ(a) (cond [(>= (* t s) (* t e)) a][(= 1 1)((((r s) (- e t)) t) (cons(- e t)a))])))))))(+ 1(* c n))) (+ 1(* c n)n)) 1) '())][(= 1 1) ((((((λ (f) ((λ (x) (x x)) (λ (x) (f (λ (y)((x x)y)))))) (λ (r) (λ(s) (λ(e) (λ(t) (λ(a) (cond [(>= (* t s) (* t e)) a][(= 1 1)((((r s) (- e t)) t) (cons(- e t)a))]))))))) (+ (* c n)n))(* c n))-1) '())]) r))]))))))n) (- n 1)) '()) )


Try it online! (Note that the TIO code is slightly different because I've wrapped the solution in (... 4) to call it.)

I picked ISL with Lambda over standard Racket on this and my other answer because I wanted to have short regexes and minimize loopholes. To do it, I started with this:

(define Y
(λ (f)
((λ (x) (x x))
(λ (x) (f (λ (y) ((x x) y)))))))

(define % (Y (λ (r) (λ (a) (λ (b)
(cond [(< a b) a]
[(= 1 1) ((r (- a b)) b)]))))))

(define range1 (Y (λ (r) (λ (s) (λ (e) (λ (t) (λ (a)
(cond [(>= (* t s) (* t e)) a]
[(= 1 1) ((((r s) (- e t)) t) (cons (- e t) a))]))))))))

(define g (Y (λ (q) (λ (n) (λ (c) (λ (r)
(cond
[(< c 0) r]
[(= 1 1)
(((q n) (- c 1))
(cons
(cond [(= 0 ((% c) 2))
((((range1 (+ 1 (* c n))) (+ 1 (* c n) n)) 1) '())]
[(= 1 1) ((((range1 (+ (* c n) n)) (* c n)) -1) '())])
r))])))))))

(define (f n)
(((g n) (- n 1)) '()))


(Look familiar? I translated this into tinylisp for a certain crack.) Y it the applicative-order Y combinator, taken from this blog post. All functions are curried to make using the Y combinator possible. Then I substituted the function bodies in for each reference to a function and turned f into a lambda. After that, all I had to do was play with spaces so that I could use .\W instead of .\W+ in my regex.

• – kennytm Mar 18 '17 at 8:46

# Java 8 (Safe)

## Regex (PCRE): 6 Bytes

[^yD]*


## Output n=4

Output separated by spaces

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


## Solution

The difficulty is getting to System.out without using the letter y. FileDescriptor is also forbidden because of D. Thus, reflection is heavily used to derive System.

class Snake {
public static void main(String[] args) throws Exception {
int n = Integer.parseInt(args[0]);
String[][] ret = new String[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int d = (i % 2 == 0) ? j + 1: n - j;
ret[i][j] = "" + (i * n + d);
}
}
String stem = "S" + (char)(1 + (int)'x') + "stem";
Class sClass = cl.loadClass("java.lang." + stem);
for (int i = 0; i < n; i++) {
String s = String.join(" ", ret[i]);
((java.io.PrintStream)sClass.getField("out").get(null)).println(s);
}
}
}


# Jelly, 16 bytes, Cracked

This is my previous submission with a typo/thinko in the regex fixed.

Jelly seems to be quite popular in this challenge. So let's require the use of a totally different algorithm. The regex, PCRE interpreted in Jelly's encoding:

[^œŒ]{12}
Çœ\?²s


Several PCRE matchers, such as pcregrep and even perl itself, will treat input by default as "unknown 8-bit character set compatible with ASCII"; therefore, they handle Jelly's encoding just fine. (That said, I suspect you need to use perl here as many regex matchers dislike newlines in the middle of the regex. I can't just write it as \n as that's a byte longer.)

Result for an input of 4:

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


## Crack

The crack again rather misses the point of the question, but I'll have to give up on this unless I can find a way to avoid cheating. (The newline and Ç was part of that, but @Dennis simply output via stdout rather than via the return value to avoid the issue. Is it acceptable to force a certain sort of I/O in cops-and-robbers challenges?)

• Cracked. – Dennis Mar 10 '17 at 2:41

# ><>, length 27 + 1 for single-line mode = 28

Regex (PCRE), using single-line mode :

^(?=(.*\n){3})[^.,\d]{131}$ My program takes the input from the stack with -v, uses no jumps, no digits, no commas and exactly three linefeeds and 131 chars. Output for n=4 : 1,2,3,4 8,7,6,5 9,10,11,12 16,15,14,13  # PHP (Cracked) start from the command line php -r <code> <args>  original code $a=array();foreach($r=range(0,($z=$argv{1})-1)as$g)foreach(($r)as$f)$a{$g}{$f}=$g%2?$g*$z+$z-$f:$g*$z+$f+1;echo(serialize($a));


## Regex (PCRE): 18 Bytes

^[$-2:-?a-}]{127}$


## Output n=4

a:4:{i:0;a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}i:1;a:4:{i:0;i:8;i:1;i:7;i:2;i:6;i:3;i:5;}i:2;a:4:{i:0;i:9;i:1;i:10;i:2;i:11;i:3;i:12;}i:3;a:4:{i:0;i:16;i:1;i:15;i:2;i:14;i:3;i:13;}}

• Eww what a messy output 😆 – kennytm Mar 13 '17 at 8:40
• @StewieGriffin It should clear also in other languages that is it the serializing of an array. php.net/manual/en/function.serialize.php I have use this output to short the regex and disallow the underscore totally – Jörg Hülsermann Mar 13 '17 at 12:03
• – kennytm Mar 13 '17 at 16:17

# Ruby (2nd submission)

The outside is mostly the same but my accidenal loophole is more restrictive now. I have many regrets

(\W?\W\w){4}..(?1){2}\W{8}(\w.\w)...\2(?1){5}\w*(?1)(.)\3


It's a lambda. Output is an array of arrays. Example with (n=4):

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

• I guess this is safe now :-) – Stewie Griffin May 1 '19 at 8:31
• @StewieGriffin damn, it's been so long that I had to change computers and no longer have the original code. Guess I might give a shot at cracking it myself some day lmao – Value Ink May 2 '19 at 21:12

# PHP >=5.4 (Safe)

Original Solution

<?while(+$g<$a=$argv{1})echo(join("\t",$g%2?range($g*$a+$a,$g++*$a+1):range($g*$a+1,$g++*$a+$a))),"\n"?>


start from the command line

php <file> <args>


## Regex (PCRE): 12 Bytes

[^ ;#/]{104}


## Output n=4

separator is \t

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


# PHP >=7.0 (Safe)

## Original Solution

<?php while($g<$a=$_GET{0})echo($g%2?"\u{202e}":""),join("\t",array_map($g%2?strrev:trim,range($g*$a+1,$g++*$a+$a))),"\n"?>


## Regex (PCRE): 30 Bytes

<\?php [^; >]{32}\\u[^; >]{82}>


Sorry a little mistake the week counts from now

Hint: Use a special unicode Character

## Output n=4

separator is \t. Looks like this

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


## Output n=4

only copy and paste from the output I use \$_GET and the document sents this header Content-Type: text/plain;charset=utf-8

1   2   3   4
‮5  6   7   8
9   10  11  12
‮31 41  51  61