7
\$\begingroup\$

Cops thread

Robbers,

Write a program in the cop's language using the cop's P string and your own S string, and the amount of each of them specified in the cop's post. Your S string may not exceed the cop's S string in bytes.

The program

Using only strings P and S, write a program which takes a list of 5 or more non-repeating positive integers, and returns them reordered such that the maximum of the list is in the center, the elements before it are in ascending order, and the elements after it are in descending order. You can output any list that meets these requirements. You may assume the list has an odd number of elements.

Examples (your outputs may differ):

input: [1,2,3,4,5]
output: [1,2,5,4,3]

input: [7,6,5,4,3,2,1]
output: [2,4,6,7,5,3,1]

input: [1,3,5,7,9,2,4,6,8]
output: [1,2,3,7,9,8,6,5,4]

For posterity, please specify your exact S string and its byte count in your answer, and link the cop which you are cracking.

Scoring

Most cracks wins.

\$\endgroup\$
6
  • \$\begingroup\$ Do robbers need to use exactly the same number of their S string as the cops used of their secret one? Or can they use at most that number? \$\endgroup\$ Commented Jan 19, 2022 at 14:31
  • \$\begingroup\$ @DominicvanEssen see i wasn't sure about that either but nobody told me to change it in the sandbox. I suppose it's not too late to change it if more people agree about it \$\endgroup\$ Commented Jan 19, 2022 at 14:36
  • 1
    \$\begingroup\$ Either way around could be Ok, although somehow it seems more intuitive (to me) for the bytes-of-S and copies-of-S to both be maxima... \$\endgroup\$ Commented Jan 19, 2022 at 14:43
  • \$\begingroup\$ I'd advocate for a rule requiring that the number of bytes be the same as the one specified in the cop's answer, as in my opinion it would make it harder. It took me quite a while to make my answer, but it was worth it because I exactly followed the cop's specifications. \$\endgroup\$
    – ophact
    Commented Jan 19, 2022 at 15:38
  • 2
    \$\begingroup\$ @ThisFieldIsRequired In most languages, any answer can be made arbitrary longer by inserting whitespace or using longer variable names. So I doubt that would make the challenge any harder or more interesting. \$\endgroup\$
    – Arnauld
    Commented Jan 19, 2022 at 16:56

9 Answers 9

4
\$\begingroup\$

Crack, 39 bytes

;_=(x,y)=>x[y.toString(32)]((x,y)=>y-x)

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 3, 21 + 33(32) bytes

def f(i):m=len(i)//2;i.sort();return i[:m]+i[:m-1:-1]

Try it online!

Cracking this one. The S string is 32 bytes (1 shorter than expected)

i.sort();return i[:m]+i[:m-1:-1]
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 63 bytes

x=>x.sort((a,b)=>a-b).map((e,i)=>i<L?e:x[L*3-i],L=x.length/2|0)

Try it online!

Cracks this one.

The S-string is 37 bytes long:

(e,i)=>i<L?e:x[L*3-i],L=x.length/2|0)

My function just keeps the first half (rounded down), and reverses the rest. For instance, given [4,5,1,3,2], it first sorts [1,2,3,4,5] (part of l4m2's cop post), then it keeps the first half rounded down, which is the first 5/2 | 0 = 2 elements. It reverses the rest, giving [1,2,5,4,3] which indeed satisfies the question's requirements.

EDIT: @Arnauld has made me aware of a potential byte-saving, which I will not accept (nor will I accept other byte-savings) because I was deliberately aiming for a solution with a length exactly matching the expected one.

\$\endgroup\$
2
  • \$\begingroup\$ /2|0 could be >>1, which means that you may have found a solution that is shorter than the expected one. \$\endgroup\$
    – Arnauld
    Commented Jan 19, 2022 at 15:51
  • \$\begingroup\$ @Arnauld Indeed. However, I was deliberately aiming for a solution of the exact same length as the expected one. I'll edit that in. \$\endgroup\$
    – ophact
    Commented Jan 19, 2022 at 15:51
2
\$\begingroup\$

JavaScript, 60 bytes

Crack of the 2nd version of l4m2's challenge.

x=>x.sort((a,b)=>a-b).map(e=>x[i+=2]?e:b.pop(),i=0,b=[...x])

Try it online!

The secret string is 33 bytes long (shorter than the expected solution of 36 bytes):

=>x[i+=2]?e:b.pop(),i=0,b=[...x])
\$\endgroup\$
1
  • \$\begingroup\$ Simple change for 3rd version if same char limit x=>x.sort((a,b)=>a-b).map(x=>b[i+=2]?e:b.pop(),i=0,b=[...x]) so I need to reduce it \$\endgroup\$
    – l4m2
    Commented Jan 19, 2022 at 16:50
2
\$\begingroup\$

J, 10 bytes, cracks Jonah's answer

(,|.)/@/:~

Try it online!

/:~ sorts the list and (,|.)/ reduces the sorted list by reverse intermediate result, prepend new value.

\$\endgroup\$
4
  • \$\begingroup\$ Nice. I had [:,~`,//:~ and realized too late that [ would have made a more interesting P. \$\endgroup\$
    – Jonah
    Commented Jan 19, 2022 at 21:15
  • 2
    \$\begingroup\$ Oh god, ,~`,/@/:~ also works for 9, would have been better too. \$\endgroup\$
    – Jonah
    Commented Jan 19, 2022 at 21:17
  • \$\begingroup\$ @Jonah I have no clue how those two work, so I definitely wouldn't have them ;) edit: Cyclic gerunds is not something I've seen before \$\endgroup\$
    – ovs
    Commented Jan 19, 2022 at 21:18
  • \$\begingroup\$ Salt in the wound :) \$\endgroup\$
    – Jonah
    Commented Jan 19, 2022 at 21:18
2
\$\begingroup\$

Bash, 29 bytes

sort -n|sed '2~2p;G;1~2h;$!d'

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Removing 1! makes an extra newline at the end, also allowed in rule I guess \$\endgroup\$
    – l4m2
    Commented Jan 20, 2022 at 11:56
  • \$\begingroup\$ Well done. This feels too nitpicky to matter but my 30 byte solution didn't have a trailing newline. \$\endgroup\$
    – Jonah
    Commented Jan 20, 2022 at 14:48
  • \$\begingroup\$ @Jonah Would you either make answer public or add restriction? \$\endgroup\$
    – l4m2
    Commented Jan 20, 2022 at 14:56
  • \$\begingroup\$ Sure. Seems close enough that it's not worth making a new restriction. But maybe you can shave another couple bytes off it: sort -rn|sed -n '1!G;$p;x;n;H' \$\endgroup\$
    – Jonah
    Commented Jan 20, 2022 at 15:53
2
\$\begingroup\$

Pip, 19 bytes, cracks DLosc's answer

FiRSNalPBi&R:llR:ll

Attempt This Online!

The secret 4-byte 'S' string is R:ll. The full code works with either one or (as required here) two copies of 'S'.

The public 'P' string:

FiRSNalPBi&     
Fi              # For each i in...
  R             # Reversed...
   SN           # Sorted by number...
     a          # 'a' (input);
       PBi      #    Push i to the Back of...
      l         #    'l'...
          &     #    ...and do something secret...

The secret 'S' string:

           R:ll 
                #    ...the secret thing to do each step is:
           R:l  #    Reverse 'l'
              l # And finally output 'l'

If we use the 'S' string twice, we simply reverse 'l' one more time at the end before outputting it.

\$\endgroup\$
1
  • \$\begingroup\$ Oh, interesting! This isn't quite what I had in mind, and it doesn't work the way you think: R:llR:ll parses as R:l (reverse l in place) followed by lR:ll (in l, replace each number from l with the corresponding number from l, in place). The latter expression returns l unchanged, which is then output. \$\endgroup\$
    – DLosc
    Commented Jan 26, 2022 at 16:54
2
\$\begingroup\$

Pip, 15 bytes, cracks DLosc's 2nd answer

SN:a;R:a&@UWRFa

Attempt This Online!

The secret 7-byte 'S' string is &@UWRFa.

This was either very sneaky, or I haven't found the intended crack.

The task could be accomplished with an 11-byte [Pip] program - SN:a;@UWRFa - which numerically sorts (SN) the input a, reflects it (RF; appending to itself), unweaves it (UW; splitting into two lists of odd & even indexed elements), and then takes the first list (@).
However, the 'P' string appears to reverse the sorted input in-place, which completely messes-up this approach (which would now put the smallest item in the centre instead of the largest).
But wait... there's no semicolon finishing this command... we can append to it...
So we append an & (logical AND) followed directly by our @UWRFa code: this appears to result in both expressions being evaluated independently, returning the result only of the second (our code).

So the reversing of the input is never actually used, and (I think) was just a decoy...

\$\endgroup\$
1
  • \$\begingroup\$ Oh wow, that is completely different from my intended solution (and quite clever, I might add)! \$\endgroup\$
    – DLosc
    Commented Jan 30, 2022 at 22:35
1
\$\begingroup\$

Python 3, 42 bytes

def f(a):a.sort();return a[1::2]+a[-1::-2]

Try it online!

41 bytes

def f(a):a.sort();return a[::2]+a[-2::-2]

Try it online!

Trivial

\$\endgroup\$
1
  • \$\begingroup\$ Neat solution! Added as an alternate to my original one \$\endgroup\$
    – jezza_99
    Commented Jan 24, 2022 at 18:33

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.