# To count the sum of all Unicode characters of a given input under an interesting constraint

Program: To count the sum of all Unicode characters of a given input.

(No need to display the output. The final answer can be calculated in any base no need to convert back to hexadecimal.)

Test case : XyZ= 0058+0079+005A=012B

Condition: If the program is inputted, then its output must be an integral multiple of 1729 (hex 06C1). (Sum of all characters (Unicode) in the code must be equal to an integral multiple of 1729.)

Winning criterion: code golf.

Sandbox

• In my opinion this would be more interesting with the irreducible code constraint, which would prevent trivially appending a comment to meet the "1729 multiple" constraint, which most solutions are doing. As it stands the "1729 multiple" constraint is kind of pointless, as it is so trivial to meet in most languages. – 79037662 Mar 25 at 13:48
• I wanted to answer this in Taxi but it can only handle ASCII up to 0x7F :( – Engineer Toast Mar 25 at 21:43
• Do 06C1 or 1729 have any special meaning? – Eric Duminil Mar 26 at 11:23
• It is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways. – Aatmaj Mar 26 at 11:35
• en.wikipedia.org/wiki/1729_(number) – Aatmaj Mar 26 at 11:35

## Sheer luck!

sum.fmap(fromEnum)


Try it online!

• +1, You are tooooo lucky........... – Wasif Mar 25 at 10:28
• @Wasif Check out the edit then ;) – Delfad0r Mar 25 at 10:29

# APL (Dyalog Unicode), 10 bytes (SBCS)

Anonymous tacit prefix function

+/⎕UCS ⍝õÿ


Try it online!

+ plus / reduction over ⎕UCS Universal Character Set code points.

⍝ is the comment symbol and the last two characters are fluff to reach 11×1729.

• Ok, that is clever, I hadn't considered using comments. :) I'm still a little confused on when you have to use brackets/parentheses (and when/if they count towards your score), and the byte count difference between Dyalog Unicode and Classic. – Andrew Ogden Mar 25 at 18:11
• Here's my attempt +⌿⍉⎕UCS⊢ – Andrew Ogden Mar 25 at 18:12
• @1_am_Jack That's amazing, however as-is, it isn't a proper solution. A slight reformulation fixes it: +⌿⍉⊢⎕UCS and you should post that! – Adám Mar 25 at 18:16
• I'd be happy to explain about byte counts, encodings, and parentheses, but I don't have time right now. Stop by in apl.chat one day, and I'll explain. – Adám Mar 25 at 18:17
• Thanks @Adám I'd appreciate the help. – Andrew Ogden Mar 25 at 18:49

# Ohm v2, 5 bytes

Σʾ


Try it online!

• As ʾ isn't in Ohm's code page, you can't claim this as 3 bytes encoded in that code page, as is usually standard for Ohm programs. Instead, this should be run as a UTF-8 file, and be counted as 5 bytes – caird coinheringaahing Mar 25 at 14:23
• @ChartZBelatedly fixed – Cinaski Mar 25 at 15:55

# Excel, 47 43 bytes

Saved 4 bytes thanks to Axuary

=+sum(unicode(mid(F1,sequence(len(F1)),1)))


After input, Excel will autoformat it to look like this:

=+SUM(UNICODE(MID(F1,SEQUENCE(LEN(F1)),1)))


Input is in the cell F1 and the formula can be in any other cell. Sum of codepoints is 3,458.

• 43 bytes: =+sum(unicode(mid(F1,sequence(len(F1)),1))) – Axuary Mar 26 at 12:26
• @Axuary It never even occurred to me to use anything but A1 for input, thanks! – Engineer Toast Mar 26 at 14:52

# APL (Dyalog Unicode), 8 bytes

+⌿⍉⊢⎕UCS

+⌿ ⍝ Column first summation
⍉ ⍝ Transpose (no-op)
⊢ ⍝ Identity function (no-op)
⎕UCS ⍝ Universal Character Set index values


Try it online!

# Zsh, 26 bytes

Replace ? with the byte \x06 below:

for	0	(${(s??)1})((B+=#0))  Try it online! Zsh is super lenient about the characters used in parameter ${(flags)}. The (s) flag expects a character, then the string to split on, then that character again. And it turns out control characters work perfectly well. (Although TIO wouldn't let me paste in \x06, so you get \x07 in the TIO link, sorry).

I was fortunate that my first program was right around 2000 points, only a couple hundred bytes away from 1 x 1729. Minimizing parameter name codepoints, replacing spaces with tabs, and finally the control characters in flags got me all the way down.

Here's a more standard program:

for char (${(s[])1}) # split$1 (first parameter)
((result += #char))   # math function returns last math result


# Python 3, 27 bytes

lambda x:sum(map(ord,x))#Ԏ


Try it online!

Program inputted unicode sum is 3458 which is 1729*2, and integral multiple of 0x6C1 or 1729

Trivial trick, base program unicode (lambda x:sum(map(ord,x))#) unicode sum is 2164, then 3458-2164 is 1294 and adding chr(1294) = Ԏ at the end results 3458!

• Since the challenge says "No need to display the output" I guess you could drop the lambda and use input() 24 bytes – Cinaski Mar 25 at 10:55
• Nice idea of adding extra compensate at the end – Aatmaj Mar 25 at 12:38
• lambda ٱ:sum(map(ord,ٱ)) is 26 bytes – tsh Apr 2 at 6:56

# Red, 39 bytes

func[s][n: 0 foreach c s[n: n + c]n];ƈ


Try it online!

2 x 1729

# Coconut, 16 bytes

Defines a function called ǰ. This sums to 1729.

ǰ=sum<..map$ord  Try it online! <.. is usually equivalent to .. (function composition), but has a slightly lower precedence, which allows map$ord to be evaluated as partial function application without any parentheses.

# JavaScript, 46 42 bytes

o=>[...o].map(z=>w+=z.codePointAt(),w=0)|w


Definitely not the best language for tasks involving character codes.

-4 bytes thanks to Arnauld

• Oops, it's 3454 not 3458, editing – ophact Mar 25 at 12:24
• +1! You can save two bytes dropping the 0 from charCodeAt, using single char var names and using .0 (or 0.) for the initial reduce value too! Try it online! – Dom Hastings Mar 25 at 13:21
• @Arnauld sorry I wasn't here earlier, I will edit your suggestions into the answer soon enough. I think I should learn how the assignment thing works like the way you did it – ophact Mar 25 at 15:39

# Java, 27 bytes

s->s.codePoints().sum()//ը


Try it online!

# Java, 20 bytes

ù->ù.chars().sum()


This method does not work for all Unicode characters.

Try it online!

• That's impressively concise for Java. – Eric Duminil Mar 26 at 11:07
• @EricDuminil Unless otherwise specified, we are allowed to write a method/function instead of the whole program. See Default for Code Golf: Input/Output methods. – iota Mar 30 at 16:26

# Japt, 11 bytes

'ЌU¬mc r+
'Ќ        // Throwaway one-letter constant to pad the code to desired length.
U¬      // Split the input to letters,
mc    // map to char codes
r+ // and reduce with summation.


Try it here.

# PowerShell, 30 bytes

"$args"|% T*y|%{$x+=+$_};$x#հ


Try it online!

# Jellyu, 4 bytes

؟OS


Try it online!

## How It Works

First, the u flag tells the interpreter to interpret this encoding as Unicode, rather the Jelly code page, otherwise the ؟ breaks the interpreter: Try it online!

When the program is actually run, ؟ is an unrecognised symbol, so breaks the parsing, forcing the parser to discard all characters up to the ؟ and begin parsing a new chain with OS

OS is fairly trivial as Jelly programs go: Ordinals then Sum

# 05AB1E, 4 bytes

Sums to 5*1729 = 8645.

ÇO,₃


Try it online!

Ç converts the input string to a list of unicode codepoints, O sums the list and , prints the result. ₃ pushes 95 to the stack, which is not printed implicitly since there was explicit output before.

This program was used to find a pair of characters (,₃) which are in the codepage and make the program sum to a multiple of 1729.

# Factor + math.unicode, 13 bytes

[ Σ dup .o ]


Try it online!

## Explanation:

It's a function that takes a sequence as input, prints its sum in octal, and returns the decimal sum as output.

• [ ... ] A quotation. An anonymous function that lives on the data stack until called or used by a combinator.
• Σ take the sum of a sequence (a string is a sequence of unicode code points)
• dup duplicate object on top of the data stack
• .o print as octal

# R, 27 bytes

sum(utf8ToInt(scan(,"ְ")))


Try it online!

The second parameter of scan needs to be a string, but can be any string. In code golf, we usually use the empty string, but here using the single character string made of Hebrew Point Sheva (U+05B0) "ְ" makes the sum 3458=1729*2.

# Charcoal, 11 bytes

﹪…%d⸿²ΣＥＳ℅ι


Try it online! Link is to verbose version of code. Explanation:

        Ｓ   Input string
Ｅ    Map over characters
ι Current character
℅  Ordinal value
Σ     Take the sum
﹪           Perform string formatting
%d⸿       Literal string %d\r
…   ²      Take first two characters
Implicitly print


Slightly less satisfying 9 byte version:

→⭆¹ΣＥＳ℅λ↖


Try it online! Link is to verbose version of code. Explanation:

→


Print the next value left-to-right (i.e. default direction).

⭆¹


Cast the expression to string.

ΣＥＳ℅λ


Sum the codepoints of the input string.

↖


Move the cursor.

# Ruby, 18 bytes

->ab{ab.bytes.sum}


Try it online!

x="->ab{ab.bytes.sum}";f=eval(x);f[x] #=> 1729
`