7
\$\begingroup\$

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

\$\endgroup\$
7
  • 9
    \$\begingroup\$ 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. \$\endgroup\$
    – 79037662
    Commented Mar 25, 2021 at 13:48
  • 1
    \$\begingroup\$ I wanted to answer this in Taxi but it can only handle ASCII up to 0x7F :( \$\endgroup\$ Commented Mar 25, 2021 at 21:43
  • 1
    \$\begingroup\$ Do 06C1 or 1729 have any special meaning? \$\endgroup\$ Commented Mar 26, 2021 at 11:23
  • 2
    \$\begingroup\$ It is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways. \$\endgroup\$
    – Aatmaj
    Commented Mar 26, 2021 at 11:35
  • 1
    \$\begingroup\$ en.wikipedia.org/wiki/1729_(number) \$\endgroup\$
    – Aatmaj
    Commented Mar 26, 2021 at 11:35

19 Answers 19

23
\$\begingroup\$

Haskell, 20 18 bytes

Sheer luck!

sum.fmap(fromEnum)

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ +1, You are tooooo lucky........... \$\endgroup\$
    – Wasif
    Commented Mar 25, 2021 at 10:28
  • 4
    \$\begingroup\$ @Wasif Check out the edit then ;) \$\endgroup\$
    – Delfad0r
    Commented Mar 25, 2021 at 10:29
6
\$\begingroup\$

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.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 25, 2021 at 18:11
  • 1
    \$\begingroup\$ Here's my attempt +⌿⍉⎕UCS⊢ \$\endgroup\$ Commented Mar 25, 2021 at 18:12
  • 1
    \$\begingroup\$ @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! \$\endgroup\$
    – Adám
    Commented Mar 25, 2021 at 18:16
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Adám
    Commented Mar 25, 2021 at 18:17
  • 1
    \$\begingroup\$ Thanks @Adám I'd appreciate the help. \$\endgroup\$ Commented Mar 25, 2021 at 18:49
6
\$\begingroup\$

Ohm v2, 5 bytes

`Σʾ

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Mar 25, 2021 at 14:23
  • \$\begingroup\$ @ChartZBelatedly fixed \$\endgroup\$
    – Cinaski
    Commented Mar 25, 2021 at 15:55
6
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 43 bytes: =+sum(unicode(mid(F1,sequence(len(F1)),1))) \$\endgroup\$
    – Axuary
    Commented Mar 26, 2021 at 12:26
  • \$\begingroup\$ @Axuary It never even occurred to me to use anything but A1 for input, thanks! \$\endgroup\$ Commented Mar 26, 2021 at 14:52
5
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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!

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

Red, 39 bytes

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

Try it online!

2 x 1729

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

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.

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

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

\$\endgroup\$
3
  • \$\begingroup\$ Oops, it's 3454 not 3458, editing \$\endgroup\$
    – user100690
    Commented Mar 25, 2021 at 12:24
  • \$\begingroup\$ +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! \$\endgroup\$ Commented Mar 25, 2021 at 13:21
  • \$\begingroup\$ @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 \$\endgroup\$
    – user100690
    Commented Mar 25, 2021 at 15:39
3
\$\begingroup\$

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That's impressively concise for Java. \$\endgroup\$ Commented Mar 26, 2021 at 11:07
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 30, 2021 at 16:26
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

PowerShell, 30 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Jelly u, 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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 11 bytes

﹪…%d⸿²ΣES℅ι

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

        S   Input string
       E    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:

→⭆¹ΣES℅λ↖

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.

ΣES℅λ

Sum the codepoints of the input string.

Move the cursor.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 18 bytes

->ab{ab.bytes.sum}

Try it online!

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

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.