Source permutation

14

1

A permutation of a set \$S = \{s_1, s_2, \dotsc, s_n\}\$ is a bijective function \$\pi: S \to S\$. For example, if \$S = \{1,2,3,4\}\$ then the function \$\pi: x \mapsto 1 + (x + 1 \mod 4)\$ is a permutation:

$$ \pi(1) = 3,\quad \pi(2) = 4,\quad \pi(3) = 1,\quad \pi(4) = 2 $$

We can also have permutations on infinite sets, let's take \$\mathbb{N}\$ as an example: The function \$\pi: x \mapsto x-1 + 2\cdot(x \mod 2)\$ is a permutation, swapping the odd and even integers in blocks of two. The first elements are as follows:

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

Challenge

Your task for this challenge is to write a function/program implementing any1 permutation on the positive natural numbers. The score of your solution is the sum of codepoints after mapping them with the implemented permutation.

Example

Suppose we take the above permutation implemented with Python:

def pi(x):
    return x - 1 + 2*(x % 2)

Try it online!

The character d has codepoint \$100\$, \$\texttt{pi}(100) = 99\$. If we do this for every character, we get:

$$ 99,102,101,31,111,106,39,119,42,57,9,31,31,31,31,113,102,115,118,113,109,31,119,31,46,31,50,31,44,31,49,41,39,119,31,38,31,49,42 $$

The sum of all these mapped characters is \$2463\$, this would be the score for that function.

Rules

You will implement a permutation \$\pi\$ either as a function or program

  • given an natural number \$x\$, return/output \$\pi(x)\$
  • for the purpose of this challenge \$\mathbb{N}\$ does not contain \$0\$
  • the permutation must non-trivially permute an infinite subset of \$\mathbb{N}\$
  • your function/program is not allowed to read its own source

Scoring

The score is given by the sum of all codepoints (zero bytes may not be part of the source code) under that permutation (the codepoints depend on your language2, you're free to use SBCS, UTF-8 etc. as long as your language supports it).

The submission with the lowest score wins, ties are broken by earliest submission.


  1. Except for permutations which only permute a finite subset of \$\mathbb{N}\$, meaning that the set \$\{ x | \pi(x) \neq x \}\$ must be infinite.

  2. If it improves your score, you can for example use a UTF-8 encoded Jelly submission instead of the usual SBCS.

ბიმო

Posted 2019-03-10T16:41:15.187

Reputation: 15 345

1@JoKing "zero bytes may not be part of the source code" re: the second question – ASCII-only – 2019-03-11T01:11:53.137

Related. – Peter Taylor – 2019-03-11T11:16:58.107

Answers

6

Jelly, score  288 250 212  199

-38 thanks to Erik the Outgolfer!

C-*+

Swaps even with odd.

The score is \$67+45+44+43=199\$ - see self-scoring here.

Try it online!

Jonathan Allan

Posted 2019-03-10T16:41:15.187

Reputation: 67 804

Apparently, Leaky Nun's -*ạ has a score of 300... however, -*_@ has a score of 250. Maybe I should post that as my own, although it's the same permutation.

– Erik the Outgolfer – 2019-03-10T17:42:08.313

Ah nice observation by Leaky Nun, so -*N+ scores 212 – Jonathan Allan – 2019-03-10T17:52:30.750

It wasn't an observation, it was a self-answer to his (now pretty old) challenge. ;-)

– Erik the Outgolfer – 2019-03-10T17:52:58.850

3C-*+ sound like some future variation of C++. – val says Reinstate Monica – 2019-03-11T05:50:08.143

5

JavaScript (ES6), Score =  276  268

$=>(--$^40)+!0

Try it online!

Arnauld

Posted 2019-03-10T16:41:15.187

Reputation: 111 334

but 54^54 is 0 – Jonathan Allan – 2019-03-10T17:56:12.317

@JonathanAllan Thanks for notifying. I somehow missed that part. Should be correct now. – Arnauld – 2019-03-10T18:04:48.977

4

Perl 6, Score: 201

*-!0+^40+!0

Try it online!

Port of Arnauld's answer. This benefits from xor (+^) having the same precedence as - and +, and the use of a Whatever lambda to reduce overall characters. Other than that, I couldn't find a way of representing it differently that got a better score.

Perl 6, Score 804 702

{{(++$∉ords(q[!$%()+-2?[]_doqrsx{}∉])??++$+22-$++%2-$++%2!!++$)xx$_}()[-!$+$_]}

Try it online!

The first quine-y type answer here, and I think it scores pretty well.

This produces the sequence \$23,22,25,24...\$ from the question body with the range \$1,2,3,4...21\$ inserted at the indexes of the unique sorted codepoints of the code. For example, the 30th through 35th elements of the sequence are \$50, 53, 52, 1, 55, 54\$ since the 33rd codepoint is ! and that's the lowest codepoint in my code.

Jo King

Posted 2019-03-10T16:41:15.187

Reputation: 38 234

O_o that's a massive improvement – ASCII-only – 2019-03-11T06:52:54.580

4

Python 2 score: 742 698 694 points

lambda a:a^96or~~96

Try it online!

-44 points thanks to Ørjan Johansen; -4 points thx to xnor.

Chas Brown

Posted 2019-03-10T16:41:15.187

Reputation: 8 959

Ørjan Johansen: Fixed (and it saved me 44 bytes!) – Chas Brown – 2019-03-14T23:48:50.573

Good save (although that's not bytes) – Ørjan Johansen – 2019-03-14T23:50:15.237

That's some generous crediting! – Ørjan Johansen – 2019-03-14T23:51:46.987

Wouldn't have edited if you hadn't pointed out the flaw :) – Chas Brown – 2019-03-14T23:53:11.340

It's cheaper to replace the space before 96 with ~~. – xnor – 2019-03-15T00:27:49.733

2

Retina 0.8.2, 6 bytes, score 260

T`O`RO

Try it online! Link includes self-scoring footer. Simply swaps digits 1 and 9 and 3 and 7 in the decimal representations, so that numbers that contain no digits coprime to 10 are unaffected.

Neil

Posted 2019-03-10T16:41:15.187

Reputation: 95 035

2

C# (Visual C# Interactive Compiler), 22 bytes, Score 247 245

A=>A>65?A-1+A%2*2:66-A

Try it online!

Simple, if less than 66, return 66 minus input, else use the formula in the question that swaps even and odd numbers.

Embodiment of Ignorance

Posted 2019-03-10T16:41:15.187

Reputation: 7 014

What's wrong with the previous version A=>A<66?66-A:A? – Jo King – 2019-03-11T02:35:20.960

2@JoKing Except for permutations which only permute a finite subset of $\mathbb{N}$, meaning that the set ${ x | \pi(x) \neq x }$ must be infinite. The set isn't infinite, since the only values in the set are 1-65, since above 65, all values of A map to A – Embodiment of Ignorance – 2019-03-11T02:37:17.807

Oh darn, I didn't notice that. That means my own answer is invalid too :( – Jo King – 2019-03-11T02:40:55.813

2

TI-BASIC, 9 bytes, score 1088 1051 1000

Ans-cos(π2fPart(2⁻¹Ans

Swaps even with odd. Even maps to Ans-1 and odd maps to Ans+1.

TI-BASIC is tokenized, so this program will have the following hex-values:

Ans   -    cos(  π    2    fPart(  2   ⁻¹  Ans
72    71   C4    AC   32   BA      32  0C  72

Thus the score is: \$113+114+195+171+49+185+49+11+113=1000\$

Output test program:

For(I,1,10
I
Ans-cos(π2fPart(2⁻¹Ans
Disp Ans
Pause
End

Which outputs:

2
1
4
3
6
5
8
7
10
9

Notes:

  • TI-BASIC's token values can be found here.

  • Pause is used in the output program to better see the permutation, as the calculator only has 8 lines. Press [ENTER] to view the next permutation.

Tau

Posted 2019-03-10T16:41:15.187

Reputation: 1 935

1

Charcoal, 13 bytes, score 681

⁻⁺²³²ι⊗﹪⊖ι²³³

Try it online! Link is to self-scoring version with header to map over an array of byte codes. (Charcoal has a custom code page so I've manually inserted the correct byte codes in the input.) Works by reversing ranges of 233 numbers, so that 117, 350, 583 ... are unchanged. Explanation:

     ι          Value
 ⁺              Plus
  ²³²           Literal 232
⁻               Minus
         ι      Value
        ⊖       Decremented
       ﹪        Modulo
          ²³³   Literal 233
      ⊗         Doubled

Neil

Posted 2019-03-10T16:41:15.187

Reputation: 95 035

1

Haskell, score 985

(\((.),(-))->(.)*200+mod(-39+(-))200+1).(\(*)->divMod((*)-1)200)

Try it online!

Joseph Sible-Reinstate Monica

Posted 2019-03-10T16:41:15.187

Reputation: 556

I don't think this is a permutation, since adding a*200 has no effect modulo 200 so a doesn't matter. – xnor – 2019-03-10T22:34:45.570

@xnor Good catch. That's supposed to be outside of the mod. At some point while golfing, I must have messed up order of operations. Fixed now, thanks! – Joseph Sible-Reinstate Monica – 2019-03-10T22:52:13.877

I think there's another issue that 40 maps to 0 but the challenge requires positive integers, which makes approaches like this tricky. – xnor – 2019-03-10T23:15:18.897

@xnor Fixed. (Sigh, there goes my score.) – Joseph Sible-Reinstate Monica – 2019-03-10T23:27:55.640

1Using sum[1|...] over if..then..else should help. – ბიმო – 2019-03-11T00:09:43.930

@ბიმო That helped a lot, thanks! – Joseph Sible-Reinstate Monica – 2019-03-11T00:14:03.857

looks a bit better :P – ASCII-only – 2019-03-11T00:20:34.557

Btw. what happened to 161? – ბიმო – 2019-03-11T00:22:00.560

@ASCII-only I'm not convinced that adding a \\ and causing a warning makes it look better :P – Joseph Sible-Reinstate Monica – 2019-03-11T00:22:01.530

@ბიმო I originally either subtracted 39 or added 161 to the remainder. Now, instead, I always subtract 39 and add 1 to the quotient in the cases when I would have added 161 before. – Joseph Sible-Reinstate Monica – 2019-03-11T00:23:12.077

better? :P – ASCII-only – 2019-03-11T00:23:47.047

@ASCII-only Sure, edited. – Joseph Sible-Reinstate Monica – 2019-03-11T00:25:15.777

I meant that your function doesn't map to 161, so it's not a permutation. – ბიმო – 2019-03-11T00:26:31.363

@ბიმო Ah, I thought you were asking where the constant in my code went. Okay, fixing... – Joseph Sible-Reinstate Monica – 2019-03-11T00:27:45.200

@ბიმო Fixed. (Interestingly, fixing that improved my score.) – Joseph Sible-Reinstate Monica – 2019-03-11T00:35:09.943

1208? – ASCII-only – 2019-03-11T00:46:03.420

@ASCII-only Yep, nice savings! – Joseph Sible-Reinstate Monica – 2019-03-11T00:47:30.123

1018? – ASCII-only – 2019-03-11T01:02:27.987

btw, you don't need bytecount since that doesn't matter – ASCII-only – 2019-03-11T01:09:29.467

1

Let us continue this discussion in chat.

– ASCII-only – 2019-03-11T01:18:18.317

1

05AB1E, score: 488 in 05AB1E's code page

È·<-

Swaps odd and even like the example function.

Will try to improve the score from here.

Try it online with input in the range [1, 100] or Try it online with the codepoints.

Explanation:

È     # Check if the (implicit) input is even (1 if truthy; 0 if falsey)
 ·    # Double (2 if truthy; 0 if falsey)
  <   # Decrease by 1 (1 if truthy; -1 if falsey)
   -  # Subtract it from the (implicit) input (and output implicitly)

Kevin Cruijssen

Posted 2019-03-10T16:41:15.187

Reputation: 67 575

0

Brainfuck, 47 bytes, score 2988

,[-<+<+>>]<[->[>+<[-]]+>[<->-]<<]>[-<<++>>]<<-.

Try it online!

I used the permutation given in the introduction. Since this is bijection you can use it as a simple symmetric cipher similar to ROT13 or Atbash. My solution works on unbounded cells. However, by restricting yourself to 8-bit cells, you could save 2 points by replacing [-] with [+].

orthoplex

Posted 2019-03-10T16:41:15.187

Reputation: 339