12
\$\begingroup\$

This is my attempt to pair with .

You need to write a full program or function that sums all codepoints of the input string. If there is no input, you can output any number, including 0.

Rules

  • The input will always be in printable ASCII.
  • The sum of the codepoints of your source must be exactly 2540.

    • If your language uses its own code page, you should use it to calculate your program's codepoints.
    • If you used Unicode in your code you have to use Unicode bytes to calculate your program's length.
  • Null bytes (which don't contribute to your codepoint sum) are banned.

  • The program must not work with any consecutive substring removed.
  • The conversion of codepoints are required.

  • This is . Your score is the length of your source code, the shorter being better.

\$\endgroup\$
  • \$\begingroup\$ Does it need to be a complete program? Some of the answers are functions. \$\endgroup\$ – Ad Hoc Garf Hunter Apr 14 at 2:28
  • \$\begingroup\$ If I'm using 05AB1E, do I have to count it in the SBCS? \$\endgroup\$ – PkmnQ Apr 14 at 2:42
  • 3
    \$\begingroup\$ Is there any reason you picked 2540? Just curious. \$\endgroup\$ – Surculose Sputum Apr 14 at 3:00
  • 6
    \$\begingroup\$ Seems there're some other problems with pristine-programming that requires the program to not work. \$\endgroup\$ – newbie Apr 14 at 5:51
  • 2
    \$\begingroup\$ Some answers are accepting an array of integer code-points while most are performing conversion from strings, are the former acceptable or is the conversion to code-points a requirement? \$\endgroup\$ – Jonathan Allan Apr 14 at 23:03

21 Answers 21

9
\$\begingroup\$

Python 3, 11 10 9 bytes

Thanks @MariaMiller for finding the right Unicode character, saving 1 byte!

ࠂ,=sum,

Try it online!

This is essentially just sum, padded with extra characters to reach the sum of 2540. Usage is ࠂ(s) where s is a byte string (which acts like both string and integer array). Feels kinda cheaty, but ¯\_(ツ)_/¯.

The first character in source code is the Unicode character with codepoint 2050 (Samaritan letter Gaman). This character might not be displayable depending on your browser.


The previous solution is longer but has nice Unicode characters:

11 bytes

ϕ,ϴ=sum,9

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 9 bytes \$\endgroup\$ – Steffan Apr 20 at 13:59
12
\$\begingroup\$

Haskell, 25 bytes

b~zw=sum(fromEnum`map`zw)

Try it online!

The ~ is not an infix operator, but a marker for a lazy pattern match on argument zw of b, while conveniently being the largest-valued ASCII character at 126. The infix-ized `map` is also used because the backtick has a large ASCII value of 96. With both of these, we can avoid any spaces or other whitespace, which have low ASCII values.

The dense 24-byter

z~zz=sum$fromEnum`map`zz

comes just short in its sum of 2525, 15 too small. Its average ASCII value is 105.21, with the only values below 97 (for a) being = at 61, $ at 36, and E at 69. An improvement would like involve finding an alternative for one of these.

(Non-ASCII characters can surely do better by having higher character values, but I'm not doing that because this is more interesting.)

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Brain-Flak, 32 bytes

<[[[{({}()<>)<>}<>({{}[()]})]]]>

Try it online!

Explanation

First the observations I made my first time around (that solution and its explanation is below) continue to be important here. We need and even number of () pairs for a valid answer.

This time however we are going to use a starting program that already has an even number of ()s.

{({}()<>)<>}<>({{}[()]})

This program first increments every element by 1 then calculates the sum of 1 less than every element. If we look at all possible ways to delete from this without causing a bracket mismatch here are what they do:

{(()<>)<>}<>({{}[()]}) # Never halts
{({}<>)<>}<>({{}[()]}) # Sums 1 less than every element
{({}())<>}<>({{}[()]}) # One more than above
{({}()<>)}<>({{}[()]}) # Never halts
{({}()<>)<>}({{}[()]}) # No output
{({}()<>)<>}<>({[()]}) # Never halts
{({}()<>)<>}<>({{}[]}) # Complex output still incorrect
{(<>)<>}<>({{}[()]})   # Never halts
{({})<>}<>({{}[()]})   # Sums 1 less than every element
{({}()<>)<>}<>({{}})   # Sums 1 more than every element
{()<>}<>({{}[()]})     # Sums 1 less than every element
{({}()<>)<>}<>({})     # Adds 1 to every element
{<>}<>({{}[()]})       # Sums 1 less than every element
{({}()<>)<>}<>()       # Adds 1 to every element
{}<>({{}[()]})         # Outputs 0
{({}()<>)<>}<>         # Adds 1 to every element
<>({{}[()]})           # Outputs 0
{({}()<>)<>}           # Outputs nothing
({{}[()]})             # Sums 1 less than every element

So this is a good starting place. To make it the correct sum I use the same method I outlined for the first attempt.

Now we just need the proper combination of [..]s <..>s and ((..){}){}s to hit 2540. Unfortunately while [..]s would be ideal seeing as they have the highest codepoint average I can't seem to get it to work with any of them present.

This time we are luckier and the winning combination is <[[[..]]]>.

Brain-Flak, 34 bytes

<<<<<<<<(((({{}})){}){}){}>>>>>>>>

Try it online!

Explanation

The code that does the task is ({{}}). But we need to pad it to 2540. The main issue is that apart from () every pair has an even total. This means we need and even number of () pairs, and at the same time our starting code uses only 1 () pair.

On top of this unlike [] or <> () pairs are not so easy to add. The one way we can do that is to wrap the entire program in (..){}, so to rectify our issue we alter the base program to

(({{}})){}

Now we just need the proper combination of [..]s <..>s and ((..){}){}s to hit 2540. Unfortunately while [..]s would be ideal seeing as they have the highest codepoint average I can't seem to get it to work with any of them present. The one that works is the one used above.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Is there a way to make {...{foo<>}...}<> work as padding? \$\endgroup\$ – Nitrodon Apr 16 at 15:10
  • \$\begingroup\$ I started with {({{}})(<>)}<> (with an extra () to make things even) and ran a program which determined that there was no way to add {}s, <>s and []s which resulted in the correct amount \$\endgroup\$ – Ad Hoc Garf Hunter Apr 16 at 19:04
  • \$\begingroup\$ @Nitrodon I realized that I can also add ()s in even amounts so I ran it again and got <<[[[[{{({{}})(((<>)))}}]]]]>><>, which is still 32 bytes. \$\endgroup\$ – Ad Hoc Garf Hunter Apr 16 at 19:12
4
\$\begingroup\$

C# (Visual C# Interactive Comρiler), 13 bytes

I had to abuse the IO so that C# can be comρetitive once in its entire existence. Takes char codes as ints as inρut. To make this seem less terrible, this acceρts any IEnumerable<int> instead of only an array.

ρ=>ρ.Sum();

Try it online!

Alternatively, for less byte savings (30 bytes):
This includes the most descriptive variable name ever on this website.

strS=>strS.Select(p=>+p).Sum()

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Did you just mispell Compiler into Comriler? Also comretitive, accerts (ρ is rho in greek.) \$\endgroup\$ – user92069 Apr 14 at 5:00
  • 3
    \$\begingroup\$ Nope, the homoglyph attack is intentional. \$\endgroup\$ – my pronoun is monicareinstate Apr 14 at 5:00
  • 1
    \$\begingroup\$ If I remove the last ;, I get a lambda that just works as well. But it shouldn't be working according to the rules. \$\endgroup\$ – Olivier Grégoire Apr 15 at 15:42
3
\$\begingroup\$

05AB1E, 17 bytes

žĆs"þþþþþx"g6QôkO

Try it online!

This was made by Kevin Cruijssen.

Explanation

žĆ                # Push codepage
  s               # Swap
   "þþþþþx"       # Push a string of length 6
           g      # Get the length of the string (6)
            6Q    # And compare it with 6 (True -> 1)
              ô   # Split into chunks
               k  # Index into the codepage
                O # Sum

Original idea, 63 48 36 29 bytes

-15 bytes by using 1! and replacing the !.

-12 bytes by dropping the factorial entirely, and using instead of 1.

-7 bytes by using тн and replacing the н. I'm not sure if this is allowed though, because with no input, it just outputs 49.

тžĆ"ʒʒʒʒʒʒʒʒʒʒKþþþ"gè.VôžĆskO

Try it online!

Explanation

т                             # Push 100
 žĆ                           # Push the codepage
   "ʒʒʒʒʒʒʒʒʒʒKþþþ"           # Push a string of length 14
                   g          # Get the length of the string
                    è         # Index into the codepage (н)
                     .V       # Run н (first digit of 100)
                       ô      # Split into chunks of 1
                        žĆ    # Push codepage
                          s   # Swap with input
                           k  # Find each char in codepage
                            O # Sum
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Java (JDK), 25 bytes

Ƌ->Ƌ.codePoints().sum()

Try it online!

All alternatives I tried failed:

s->s.chars().sum() // Function<String,Integer>
s->s.sum()         // Function<IntStream,Integer>
java.util.stream.IntStream::sum
...
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I just came up with an identical answer only to scroll down and find yours here. :) \$\endgroup\$ – David Conrad Apr 15 at 20:38
  • \$\begingroup\$ For the IntStream, 13 bytes \$\endgroup\$ – Steffan Apr 20 at 15:05
  • \$\begingroup\$ @MariaMiller Thanks, but l->l.sum() (without the semi-colon) is also a valid lambda, so that answer is invalid because the rules say that there may not be a substring of the answer that also works. \$\endgroup\$ – Olivier Grégoire Apr 20 at 15:14
  • \$\begingroup\$ Okay, here's another one that doesn't violate that rule \$\endgroup\$ – Steffan Apr 24 at 3:26
2
\$\begingroup\$

Perl 5, 29 bytes

for(split//,<>){$u+=ord}say$u

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Python 3, 26 bytes

lambda ŏ:sum(ŏ.encode())

Try it online!


Python 2, 28 bytes

lambda	eZ:sum(bytearray(eZ))

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Hopefully it's fixed now :\ \$\endgroup\$ – dingledooper Apr 14 at 2:24
  • \$\begingroup\$ Looks like the sum of this code is 2450? The alternative seems to work out to the specified 2540. \$\endgroup\$ – Xcali Apr 14 at 3:18
  • \$\begingroup\$ I don't think your non-printable answer sums to 2540, since Ì has code point 204. I replaced it the correct code point 335. TIO. \$\endgroup\$ – Surculose Sputum Apr 14 at 4:31
  • \$\begingroup\$ Thanks for that, @Surclose Sputum. IDK why I keep messing up my answer :( \$\endgroup\$ – dingledooper Apr 14 at 4:35
  • 1
    \$\begingroup\$ That's because your function only works with ASCII characters. For UTF-8 encoding, an ASCII character is encoded into 1 byte that is the same as its codepoint. However, a non-ASCII character is encoded into multiple bytes. Here is a demonstration. \$\endgroup\$ – Surculose Sputum Apr 14 at 4:48
2
\$\begingroup\$

PHP, 35 bytes

FOR(;$zz=$argn[$u++];)$a+=ORD($zz);

Try it online!

OK I'm cheating a little bit here, but the shorter code I found for PHP having too much codepoints sum already (2549), I'll interpret the question in a litteral sense:

  • "program or function that sums all codepoints of the input string" -> it is not said I have to display the result, above code actually sums it :D (yeah I know, implicit rules.. well!)
  • "you can output any number, including 0" -> well I can, but if not forced to, I won't :P
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

brainfuck, 41 39 bytes

(I haven't tested exhaustively)
Uses 3 strategies for wasting the codepoint sum: repeating all < and >, nesting the innermost [ and ] unneccessarily, and adding and later removing the same number to/from the output.

Runs in an interpreter with large cells and wrapping/bidirectional memory, which TIO isn't :(. Outputs by charcode.

++++++[[[[[[[<<<+>>>-]]]]]],]<<<------.
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Python 2, 28 bytes

lambda	zva:sum(map(ord,zva))

Try it online!

Uses a tab after the lambda.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Your code contains 2 newlines, which could be removed. The actual sum without newlines is 2520. "The program must not work with any consecutive substring removed". \$\endgroup\$ – G B Apr 14 at 7:19
  • \$\begingroup\$ @GB Nice catch, fixed. \$\endgroup\$ – xnor Apr 14 at 13:57
2
\$\begingroup\$

Ruby -nl, 28 26 bytes

-2 bytes from GB.

p $_.chars.sum{|ay|ay.ord}

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ -2 bytes: use p instead of $> and name your variable 'ya' \$\endgroup\$ – G B Apr 14 at 7:24
2
\$\begingroup\$

Wolfram Language (Mathematica), 28 bytes

A[J_]:=Tr@ToCharacterCode[J]

Try it online! Defines a named function A that takes a string as input and returns the sum of its ASCII codepoints. ToCharacterCode converts a character to its codepoint (and outputs a list of codepoints when fed a string of characters) and Tr sums them.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 15 bytes

üüd←⎕UCS⍞⋄+/üüd

Try it online!

A full program that takes a single line from STDIN as input.

The ASCII characters are shuffled around within the default codepage (along with APL symbols and accented characters), and many useful characters appear at the high half (character value > 128). Accented characters are valid to use in an identifier, and ü has the highest character value among them.

This code achieves "The program must not work with any consecutive substring removed" by separating the Unicode conversion ⎕UCS and sum +/ into two statements.

The character alone is over nine thousand (pun intended) in Unicode, so APL can't compete using Unicode scoring.

How it works

üüd←⎕UCS⍞⋄+/üüd
        ⍞        ⍝ Take a line of input from stdin
    ⎕UCS         ⍝ Convert to Unicode codepoints
üüd←             ⍝ Assign to variable
         ⋄       ⍝ Statement separator
          +/üüd  ⍝ Sum
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Python, 30 bytes

Works in Python 2 and 3.

lambda abZ:(sum(map(ord,abZ)))

Try it online!

Alternative 30-byter:

lambda aN,b=ord:sum(map(b,aN))

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 28 bytes

'zzzzzzzzzzzh'+{}/]{+}*1446-

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Red, 37 bytes

func[-][!: 0 forall -[!: ADD ! -/1]!]

Try it online!

Nothing original. - is the input string, ! is the sum. For each character in the input string I add its value to the sum. Red is case insensitive, so I use ADD instead of add (and isntead of the + operator) in order to match 2540. forall iterates over the entire series (list) and at each iteration returns the remaining series - just like cdr in LISP or rest in Racket. That's why I use /1 to obtain the first element in the series.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 28 bytes

Ă=>eval(Buffer(Ă).join`+`)

Try it online!

Submission only works on ASCII. Although it source code contains non-ASCII.

This one based on code by Arnauld.


JavaScript (Node.js), 31 bytes

s=>Buffer(s).map(c=>w+=c,w=0)|w

Try it online!


Another trivial one.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 15 bytes

IΣES⁺⊗⊗⊗⊗LPP⌕γι

Try it online! Link is to verbose version of code. Only works on printable ASCII, so I can't feed it its own source code, even if I could create it in the right code page. Explanation:

   S            Input string
  E             Map over characters
              ι Current character
            ⌕   Find index in
             γ  Printable ASCII
    ⁺           Plus
          PP    Literal string `PP`
         L      Length
     ⊗⊗⊗⊗       Doubled four times
 Σ              Take the sum
I               Cast to string
                Implicitly print

Hex dump in Charcoal's code page:

C9 91 C5 D3 AB 9E 9E 9E 9E CC 50 50 9B E7 E9
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 42 bytes

(LAMBDA(%)(APPLY'+(MAP'CONS'CHAR-CODE %)))

This was found by trial and error using the following test: basically the code is printed to string, with some spaces removed (but not all, otherwise it parses badly), then it is read back to lisp and evaluated with its own representation.

(let ((string (remove #\space
                      (princ-to-string
                       '(lambda(%)(apply'+(map'cons'char-code %))))
                      :count 6)))
  (values string
          (funcall (eval (read-from-string string)) string)))

This returns both the code as string, and its sum:

"(LAMBDA(%)(APPLY'+(MAP'CONS'CHAR-CODE %)))"
2540

Usually you call map as follows, (map 'list function sequence), where list is the type of result you want to build with map. Any Lisp type can be given, but obviously it should be a sequence. Here I used cons (lists are made of cons cells) to change the count, but the consequence if that is that there will be an error if the input is an empty sequence, since it cannot be expressed as a cons cell.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 19 13 12 bytes

->΃{΃.sum}

Try it online!

The weird char probably isn't displayed, so it's ASCII 899.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Oops, now I'm at the top of the ones that aren't cheaty. \$\endgroup\$ – Steffan Apr 19 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy