#Jelly, length 17 (safe)
#Jelly, length 17 (safe)
A solution:
’:2o1 Ḃ¬aẋ@0 ’r0;0ẋ$ẋ1Ŀ¬0¦;2ĿÆ¡œ?⁸²¤s⁸G The main trick here is to index into a lexicographically sorted list of the permutations of the natural numbers up to n2 (using œ? to avoid building the list of length n2!), and to split the result into chunks of length n. The aforementioned index is found by forming its representation in the factorial number system which is formulaic since the "unsliced" snake is created by permuting elements in a prescribed manner (this may be readily converted to a number with Æ¡).
The solution I present uses Ŀ to reference previous links as monads (replacing Ñ and Ç), but multiple $ in a row could be employed instead to "inline" these helper functions. It also uses r since Ḷ and R are banned.
’:2o1 - Link 1, periodic repetitions in the factorial base representation: n ’ - decrement n :2 - integer divide by 2 o1 - or 1 (keep one period in the cases n=1 and n=2) Ḃ¬aẋ@0 - Link 2, n zeros if n is even, else an empty list: n Ḃ - mod 2 ¬ - not ẋ@0 - 0 repeated n times a - and ’r0;0ẋ$ẋ1Ŀ¬0¦;2ĿÆ¡œ?⁸²¤s⁸G - Main link: n e.g. 6 ’r0 - inclusive range(n-1, 0) [5,4,3,2,1,0] 0ẋ$ - 0 repeated n times [0,0,0,0,0,0] ; - concatenate (makes one "period") [5,4,3,2,1,0,0,0,0,0,0,0] 1Ŀ - call link 1 as a monad 2 ẋ - repeat list [5,4,3,2,1,0,0,0,0,0,0,0,5,4,3,2,1,0,0,0,0,0,0,0] 0¦ - apply to index 0 (rightmost index): ¬ - not (make the last 0 a 1) [5,4,3,2,1,0,0,0,0,0,0,0,5,4,3,2,1,0,0,0,0,0,0,1] 2Ŀ - call link 2 as a monad [0,0,0,0,0,0] ; - concatenate [5,4,3,2,1,0,0,0,0,0,0,0,5,4,3,2,1,0,0,0,0,0,0,1,0,0,0,0,0,0] Æ¡ - convert from factorial base 45461852049628918679695458739920 ¤ - nilad followed by link(s) as a nilad ⁸ - left argument, n 6 ² - square 36 œ? - lexicographical permutation lookup [1,2,3,4,5,6,12,11,10,9,8,7,13,14,15,16,17,18,24,23,22,21,20,19,25,26,27,28,29,30,36,35,34,33,32,31] s⁸ - split into chunks of length n [[1,2,3,4,5,6],[12,11,10,9,8,7],[13,14,15,16,17,18],[24,23,22,21,20,19],[25,26,27,28,29,30],[36,35,34,33,32,31]] G - format as a grid #Jelly, length 17
#Jelly, length 17 (safe)
A solution:
’:2o1 Ḃ¬aẋ@0 ’r0;0ẋ$ẋ1Ŀ¬0¦;2ĿÆ¡œ?⁸²¤s⁸G The main trick here is to index into a lexicographically sorted list of the permutations of the natural numbers up to n2 (using œ? to avoid building the list of length n2!), and to split the result into chunks of length n. The aforementioned index is found by forming its representation in the factorial number system which is formulaic since the "unsliced" snake is created by permuting elements in a prescribed manner (this may be readily converted to a number with Æ¡).
The solution I present uses Ŀ to reference previous links as monads (replacing Ñ and Ç), but multiple $ in a row could be employed instead to "inline" these helper functions. It also uses r since Ḷ and R are banned.
’:2o1 - Link 1, periodic repetitions in the factorial base representation: n ’ - decrement n :2 - integer divide by 2 o1 - or 1 (keep one period in the cases n=1 and n=2) Ḃ¬aẋ@0 - Link 2, n zeros if n is even, else an empty list: n Ḃ - mod 2 ¬ - not ẋ@0 - 0 repeated n times a - and ’r0;0ẋ$ẋ1Ŀ¬0¦;2ĿÆ¡œ?⁸²¤s⁸G - Main link: n e.g. 6 ’r0 - inclusive range(n-1, 0) [5,4,3,2,1,0] 0ẋ$ - 0 repeated n times [0,0,0,0,0,0] ; - concatenate (makes one "period") [5,4,3,2,1,0,0,0,0,0,0,0] 1Ŀ - call link 1 as a monad 2 ẋ - repeat list [5,4,3,2,1,0,0,0,0,0,0,0,5,4,3,2,1,0,0,0,0,0,0,0] 0¦ - apply to index 0 (rightmost index): ¬ - not (make the last 0 a 1) [5,4,3,2,1,0,0,0,0,0,0,0,5,4,3,2,1,0,0,0,0,0,0,1] 2Ŀ - call link 2 as a monad [0,0,0,0,0,0] ; - concatenate [5,4,3,2,1,0,0,0,0,0,0,0,5,4,3,2,1,0,0,0,0,0,0,1,0,0,0,0,0,0] Æ¡ - convert from factorial base 45461852049628918679695458739920 ¤ - nilad followed by link(s) as a nilad ⁸ - left argument, n 6 ² - square 36 œ? - lexicographical permutation lookup [1,2,3,4,5,6,12,11,10,9,8,7,13,14,15,16,17,18,24,23,22,21,20,19,25,26,27,28,29,30,36,35,34,33,32,31] s⁸ - split into chunks of length n [[1,2,3,4,5,6],[12,11,10,9,8,7],[13,14,15,16,17,18],[24,23,22,21,20,19],[25,26,27,28,29,30],[36,35,34,33,32,31]] G - format as a grid