7
\$\begingroup\$

Your task is to get two numbers, and output the digit sum of them. We'll define digit sum as the following:

  • take the decimal (base 10) digits of each of them, and pad with 0s the shortest number (e.g. [1,9], [0, 7])
  • sum the digits as vectors (e.g. [1, 16])
  • convert the number back from base 19, and output it (e.g. 1*19+16=35)

Input

You can take the input as two numbers, as a list containing both of them or in any other reasonable format (like an array of digits). You can assume the shortest number is padded with zeros so it fits the length of the longer one

Output

You can output the digit sum in any reasonable format.

Test cases

1, 2 -> 3
5, 10 -> 24
42, 32 -> 137
15717, 51358 -> 826819
1000, 1 -> 6860

explanation of 5, 10: the digits of these are [0, 5] and [1, 0]. Their sum is [1, 5], which when converted from base 19 results in 24.

Scoring

This is code golf, so the shortest answer wins, good luck!

\$\endgroup\$
4

22 Answers 22

8
\$\begingroup\$

J, 6 bytes

19#.+/

Try it online!

  • 19#. convert the following to base 19...
  • +/ elementwise sum of the input digit lists.
\$\endgroup\$
2
  • \$\begingroup\$ You can take a digit list as an input. \$\endgroup\$
    – user92069
    Commented May 4, 2020 at 11:17
  • \$\begingroup\$ Thanks, not much content at this point though. \$\endgroup\$
    – Jonah
    Commented May 4, 2020 at 11:20
6
\$\begingroup\$

Jelly, 4 bytes

ḅ19S

A monadic Link accepting a list of lists of digits which yields an integer.

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Python, 30 bytes

lambda a,b:int(a,19)+int(b,19)

An unnamed function accepting two strings of (base ten) digit characters which returns an integer.

Try it online!

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

Japt -mx, 6 3 bytes

Takes input as an array of digit arrays.

ì19

Try it

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

05AB1E, 4 bytes

-1 thanks to Command Master.

+19β

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 4 bytes \$\endgroup\$ Commented May 6, 2020 at 13:41
  • \$\begingroup\$ + vectorizes!? I was an idiot. \$\endgroup\$
    – user92069
    Commented May 6, 2020 at 13:43
3
\$\begingroup\$

Python 2, 46 bytes

f=lambda a,b:a+b and a%10+b%10+19*f(a/10,b/10)

Try it online!

Recursive function that sums each digit pair and convert them to base 19.

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

Wolfram Language (Mathematica), 24 bytes

Tr[#~FromDigits~19&/@#]&

Unnamed function taking as input a list that contains two lists of digits. The main point here is that converting to base 19 first and then adding means that padding on the left is unnecessary.

Try it online!

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

Bash, 54 53 19 bytes

echo $[19#$1+19#$2]

Try the test cases online!


The input numbers are passed as two arguments.

Output is on stdout.

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

Wolfram Language (Mathematica), 30 bytes

Total@PadLeft@#~FromDigits~19&

Try it online!

Takes input as list of digits as @my pronoun is monicareinstate suggested and saved 13 bytes (IntegerDigits)

-1 byte @LegionMammal978

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think you can take two lists of digits as input. \$\endgroup\$ Commented May 4, 2020 at 13:41
  • \$\begingroup\$ -1 byte: Total@PadLeft@IntegerDigits@#~FromDigits~19& \$\endgroup\$ Commented May 5, 2020 at 2:33
2
\$\begingroup\$

C (gcc), 67 65 bytes

Saved 2 bytes thanks to Mitchell Spector!!!

s;m;f(a,b){for(s=m=1;a+b;a/=10,b/=10,m*=19)s+=(a%10+b%10)*m;s--;}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 65 bytes \$\endgroup\$ Commented May 5, 2020 at 3:51
  • 1
    \$\begingroup\$ @MitchellSpector Nice, homogeneous init with a tweak at the end to make it the return value - thanks! :-) \$\endgroup\$
    – Noodle9
    Commented May 5, 2020 at 9:34
2
\$\begingroup\$

K (oK), 5 bytes

Solution:

19/+/

Try it online!

Explanation:

Takes input as two lists of digits. Assumes the shorter number is padded with 0s.

19/+/ / the solution
   +/ / sum
19/   / decode from base 19
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 8 bytes

IΣE²⍘S¹⁹

Try it online! Link is to verbose version of code. Takes input as two strings. Explanation:

    ⍘S¹⁹    Convert the input from base 19
  E²        Repeated twice
IΣ          Summed and output in decimal
\$\endgroup\$
1
\$\begingroup\$

dc, 47 bytes

?[0q]sZ[sydsxly+0=ZlxA~rlyA~4R+_3Rlfx19*+]dsfxp

Try it online!

Or verify all the test cases online.

Input is on stdin (the two numbers on one line, separated by a space).

Output is on stdout.


This program makes use of a nice facility in dc, where the single-character command ~ will compute both the quotient and the remainder in a division operation.

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

Java (JDK), 40 bytes

a->b->a.valueOf(""+a,19)+a.valueOf(b,19)

Try it online!

Takes input as a:Integer and b:String.

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

PHP, 75 bytes

for($n=strlen($a=$argv[1]);$n--;)$r+=($a[$i]+$argv[2][$i++])*19**$n;echo$r;

Try it online!

As often, getting arguments and dollars makes PHP the longest answer so far :D still i'm content with this

Takes input as strings already padded with zeroes

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

JavaScript (Babel Node), 43 bytes

f=(a,b)=>a+b&&a%10+b%10+19*f(a/10|0,b/10|0)

Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ But (a,b)=>(p=parseInt)(a,19)+p(b,19) is only 33 bytes... \$\endgroup\$
    – tsh
    Commented May 7, 2020 at 5:07
1
\$\begingroup\$

Pyth, 7 6 5 bytes

Takes input as two lists of digits. Assumes the shorter number is padded with 0's.

siR19

Try it online!

siR19
  R       (Right-) Map each digit array of the input to:
 i 19     The digit array converted from base 19
s         Take the sum
\$\endgroup\$
1
\$\begingroup\$

Ruby, 24 bytes

->a{a.sum{|x|x.to_i 19}}

Accepts an array of two strings.

Try it online!

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

Brachylog, 6 bytes

~ḃ₁₉ᵐ+

Try it online!

Takes input as a digit list, but the testing header converts from single integers for convenience.

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

Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes

z+19Ad

Attempt This Online!

Takes in the numbers as digit arrays.

Thunno, \$ 7 \log_{256}(96) \approx \$ 5.76 bytes

e19AdES

Attempt This Online!

Takes in the numbers as digit arrays.

Explanations

z+19Ad  # Implicit input
z+      # Add the digits in the same positions together
    Ad  # Convert from
  19    # base-19
e19AdES  # Implicit input
e    E   # Map over the input:
   Ad    #  Convert from
 19      #  base-19
      S  # Sum the list
         # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Nibbles, 4.5 bytes

`@19!$_+

Attempt This Online!

`@ Convert from base
19  19
!   zip
$    first input
_    second input
+    addition
\$\endgroup\$
0
\$\begingroup\$

Python 3, 77 72 bytes

lambda x,y:sum(n*19**x for x,n in enumerate([*map(sum,zip(x,y))][::-1]))

Try it online!

Not one of the shortest approaches, but it works.

-5 bytes thanks to @ovs

\$\endgroup\$
1
  • \$\begingroup\$ You can use map for 72 bytes: enumerate([*map(sum,zip(x,y))][::-1]). \$\endgroup\$
    – ovs
    Commented May 4, 2020 at 17:24

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.