21
\$\begingroup\$

Given two positive integers, add them.

Both inputs and output are in Tally marks form, which can be expressed as:

$$ T(n) := \begin{cases} n,&& n\le 5\\ 5 || T(n-5),&& n>5 \end{cases} $$

and for n=1 to 15, T(n) are 1,2,3,4,5,51,52,53,54,55,551,552,553,554,555, respectively.

Shortest code win.

Test cases:

1 + 1 = 2
4 + 2 = 51
51 + 5552 = 55553
5 + 5 = 55
52 + 53 = 555

Note

  • Technically U+1D360-U+1D378 𝍠 𝍡 𝍢 𝍣 𝍤 𝍥 𝍦 𝍧 𝍨 𝍩 𝍪 𝍫 𝍬 𝍭 𝍮 𝍯 𝍰 𝍱 𝍲 𝍳 𝍴 𝍵 𝍶 𝍷 𝍸 are used, but U+0031-U+0035 1 2 3 4 5 should be more friendly to most languages, and you can get a font to make them display as you want XD
\$\endgroup\$
6
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – l4m2
    Apr 5 at 8:58
  • 1
    \$\begingroup\$ @JonathanAllan No U+0030(0) ever exist in IO \$\endgroup\$
    – l4m2
    Apr 5 at 10:36
  • 3
    \$\begingroup\$ How flexible is the input format? For e.g. 551+552, are we allowed to use a single argument "551552"? Or a pair of lists of digits such as [[5,5,1],[5,5,2]]? \$\endgroup\$
    – Arnauld
    Apr 5 at 15:41
  • 1
    \$\begingroup\$ @noodleman I assume not, since there is no 0 in the I or the O, according to a previous comment. \$\endgroup\$
    – DLosc
    Apr 5 at 16:28
  • 1
    \$\begingroup\$ Suggest a test case with inputs of different lengths \$\endgroup\$
    – att
    Apr 5 at 18:41

22 Answers 22

7
\$\begingroup\$

Retina 0.8.2, 15 bytes

.
$*
1{1,5}
$.&

Try it online! Link is to test suite that splits each test on = and runs the code on both halves although obviously the output for a single tally mark is the same as the input. Explanation:

.
$*

Convert everything to unary. Non-digits are simply deleted.

1{1,5}
$.&

Convert up to 5 1s to decimal at a time.

\$\endgroup\$
6
\$\begingroup\$

Jelly, 5 bytes

FSs5Ẉ

A monadic Link that accepts a list of summands as lists of integers from \$[1,5]\$ and yields a single list of integers from \$[1,5]\$.

Try it online! Or see the test-suite.

How?

FSs5Ẉ - Link: list of lists of integers, Summands  e.g.: [[5,2],[5,1]]
F     - flatten the Summands                             [5,2,5,1]
 S    - sum                                              13
  s5  - split into chunks of five (or less at right)     [[1..5],[6..10],[11,12,13]]
    Ẉ - length of each                                   [5,5,3]

A dyadic Link is also possible in five bytes with +Ss5Ẉ TIO / tests

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

Python, 45 43 bytes

-2 bytes thanks to @Jonathan Allan

lambda a,b:(c:=sum(a+b))//5*[5]+[c%5][:c%5]

Attempt This Online!

Takes input as lists of digits (as integers)

\$\endgroup\$
3
  • \$\begingroup\$ Oh, I was so close :p \$\endgroup\$ Apr 5 at 12:16
  • \$\begingroup\$ ...[:c%5] from mine saves two bytes. \$\endgroup\$ Apr 5 at 12:17
  • 1
    \$\begingroup\$ @JonathanAllan, yeah noticed that as soon I saw your answer \$\endgroup\$ Apr 5 at 12:20
4
\$\begingroup\$

Perl 5 + -F -MList::Util+sum -M5.10.0, 28 bytes

-3 bytes from @Xcali's suggestion!

say 5x(($b=sum@F)/5),$b%5||$

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Same logic, one byte shorter: Try it online! \$\endgroup\$
    – Xcali
    Apr 5 at 16:11
  • \$\begingroup\$ Nice I got -3 from that! Thanks! \$\endgroup\$ Apr 5 at 19:22
3
\$\begingroup\$

Vyxal, 36 bitsv2, 4.5 bytes

ɾf5ẇ@

Try it Online!

Bitstring:

001000011111011010110101101001110010

Very similar to the Jelly answer, but slightly different in the first half. Takes both tallies in a single list. Tallies are represented as lists of digits.

Explained

ɾf5ẇ@­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌­
ɾ      # ‎⁡range 1..N for each N in each tally
 f     # ‎⁢flatten that
  5ẇ   # ‎⁣split into chunks of length 5
    @  # ‎⁤and vectorise length
💎

Created with the help of Luminespire.

\$\endgroup\$
3
  • \$\begingroup\$ 5 + 5 needs to return 55 not 550. \$\endgroup\$ Apr 5 at 10:36
  • 1
    \$\begingroup\$ @JonathanAllan fixed \$\endgroup\$
    – lyxal
    Apr 5 at 11:01
  • \$\begingroup\$ Oh, I also took the range out to the front to avoid summing and the ɾ effectively needed by ẇ; but I have not Vyncoded :D (EDIT - I still think this should be classed as a different language...) \$\endgroup\$ Apr 5 at 11:15
3
\$\begingroup\$

Vyxal,  6  5 bytes

Note: now the same as Lyxal's Vyncoded answer which also ports my Jelly answer.

ɾf5ẇ@

Try it Online!

How?

Almost a straight port of my Jelly answer, just moving the implicit range that s does in Jelly out to a leading ɾ and using the flattened result.

ɾf5ẇ@                                           e.g. [[5,2],[5,1]]
ɾ     - range (vectorises)                           [[[1,2,3,4,5],[1,2]],[[1,2,3,4,5],[1]]]
 f    - flatten                                      [1,2,3,4,5,1,2,1,2,3,4,5,1]
  5ẇ  - split into chunks of five (or less at right) [[1,2,3,4,5],[1,2,1,2,3],[4,5,1]]
    @ - length of each                               [5,5,3]
\$\endgroup\$
3
\$\begingroup\$

R, 49 46 bytes

\(x,s=Reduce(sum,x))c(rep(5,s/5),if(s%%5)s%%5)

Attempt This Online!

Takes input as a list of vectors of digits. Outputs a vector of digits.

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

BitCycle, 35 bytes

?+?+
v< <   <
A\\\\\B^
v~v<<<
> C\!

Try it!

Explanation

The -u flag converts lists of numbers to unary, with 0 as a separator. So first, we take the two inputs (?) and use + devices to send the 0 bits off the playfield and send the 1 bits into the collector A. This takes the sum of all the numbers in the input.

Then the 1 bits loop between collectors A and B. Each time through the loop, the splitters \ redirect the first five bits downward. The first one (from the leftmost splitter) goes through the dupneg ~; the negated copy, 0, turns left and gets to the C collector first, followed by the unchanged copy and all the other 1 bits. Thus, C gets the sum, partitioned into groups of five 1 bits, with a 0 before each group.

This can't be output as-is because it has a leading separator 0, which would put a zero in front of the list. So before we output the contents of C, we use one more splitter to get rid of that first bit.

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

APL+WIN, 29 23 22 bytes

Prompts for a vector of integers and outputs a vector of integers:

(((⌊n÷5)⍴5),5|n←+/⎕)~0

Try it online! Thanks to Dyalog Classic

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

K (ngn/k), 15 bytes

5&(5<)-[;5]\+/,

Try it online!

Input two digit lists as x f y.

            +/, sum
      -[;5]     decrement by 5
  (5<)           while >5
           \     keep intermediate values
5&              >5 becomes 5
\$\endgroup\$
2
  • \$\begingroup\$ nice! are you familiar with null-reshapes like 0N 5#? idea: use & to make a list of zeroes, reshape it with 0N 5#, and count the zeroes in each row with #' \$\endgroup\$
    – ngn
    Apr 8 at 15:03
  • \$\begingroup\$ @ngn ah, I thought there was something like that but forgot how to do it. I did want to post an answer with this approach in any case, though. \$\endgroup\$
    – att
    Apr 8 at 19:46
2
\$\begingroup\$

JavaScript (ES6), 53 bytes

Expects two strings as (a)(b).

a=>b=>(g=x=>x>5?"5"+g(x-5):x)(eval([...a+b].join`+`))

Try it online!

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

Google Sheets, 82 80 78 bytes

=LET(s,SUM(SPLIT(REGEXREPLACE(A2&B2,,"9"),9)),l,MOD(s,5),REPT(5,s/5)&IF(l,l,))

enter image description here

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

Wolfram Language (Mathematica), 102 68 bytes

Saved 34 bytes thanks to @Greg Martin


Golfed version. Try it online!

Join[5~Table~#,#2~Table~Sign@#2]&@@Tr@Flatten@#~QuotientRemainder~5&

Ungolfed version. Try it online!

f[x_List] := 
 Module[{s = Total[Flatten[x]], quotient, remainder}, 
  quotient = Quotient[s, 5];
  remainder = Mod[s, 5];
  Flatten[{Table[5, {quotient}], 
    If[remainder != 0, remainder, Nothing]}]]
f[{1, 1}] // Print
f[{4, 2}] // Print
f[{{5, 5, 1}, {5, 5, 2}}] // Print
f[{5, 5}] // Print
f[{{5, 2}, {5, 3}}] // Print
\$\endgroup\$
2
  • \$\begingroup\$ I wonder whether one can find a shorter way to mimic the long-named QuotientRemainder function ... but for general golfy modifications of your method, here's a 68-byte version. \$\endgroup\$ Apr 7 at 5:14
  • \$\begingroup\$ This uses a different approach: Clip[Range[#~Total~2,1,-5]/5]*5& \$\endgroup\$ Apr 11 at 9:04
1
\$\begingroup\$

Retina, 44 bytes

2
11
3
111
4
1111
5
11111
;

1{5}
5
(1)+
$#1

First, converts everything into unary, deletes a semicolon I used as a seperator, replaces groups of five 1s by 5 and converts the remainder back to decimal.

Attempt This Online!

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

Charcoal, 11 bytes

⭆⪪⭆S×ψΣι⁵Lι

Try it online! Link is to verbose version of code. Takes input as two tally numbers separated by punctuation. Explanation:

   ï¼³        Input string
  ⭆         Map over characters and join
     ψ      Null byte
    ×       Repeated by
       ι    Current character
      Σ     Extract digit value if any
 ⪪          Split into groups of length
        ⁵   Literal integer `5`
⭆           Map over groups and join
          ι Current group
         L  Length
            Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Python 3.8, 47 bytes

lambda*t:(s:=sum(sum(t,[])))//5*[5]+[s%5][:s%5]

An unnamed function that accepts any number of summands each as a list of integers from \$[1,5]\$ and returns a list of integers from \$[1,5]\$.

Try it online!

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

Google Sheets, 90 bytes

=let(v,sum(split(regexreplace(A1&B1,"(.)","$1 ")," ")),rept(5,v/5)&iferror(1/mod(v,5)^-1))

Put the input in cells A1:B1 and the formula in C1.

tally.png

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

sed, 84

s/(.*) (5*)/\2\1/
s/4/22/g
s/3/21/g
s/2/11/g
s/1{5}/5/
s/1{4}/5/
s/1{3}/5/
s/1{2}/5/

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ s/11/2, s/21/3, s/31/4 saves 6 bytes. \$\endgroup\$
    – Neil
    Apr 6 at 0:00
1
\$\begingroup\$

Haskell + hgl, 13 bytes

cxS 5<<pl.*sm

Explanation

We sum both lists with pl.*sm. sm<<mp would also work just fine. Then we split the result into chunks of size 5 with cxS 5

Reflection

This is overall quite short. None of the components seem sensible to combine. The single exception here is that cxS should probably have pre-loaded variants for small number inputs. 2 especially is quite useful, but might as well go up to 10 as is usual. This would save 2 bytes.

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

Uiua SBCS, 13 bytes

⊜⧻\+=0◿5.⇡/+⊂

Takes two digit lists and returns a digit list.

Try it!

⊜⧻\+=0◿5.⇡/+⊂­⁡​‎⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌­
             ⊂  # ‎⁡prepend
           /+   # ‎⁢sum
          ⇡     # ‎⁣range
         .      # ‎⁤duplicate
       ◿5       # ‎⁢⁡modulo five (vectorizing)
     =0         # ‎⁢⁢mask of where this is equal to zero
   \+           # ‎⁢⁣cumulative sum
⊜⧻             # ‎⁢⁤lengths of contiguous equal numbers
\$\endgroup\$
1
\$\begingroup\$

Acc!!, 93 89 bytes

-4 bytes thanks to @DLosc

1
Count i while _%6 {
_-_%6+N%6*7
}
_/6-6
Count i while _/5%_ {
Write 53
_-5
}
Write 48+_

Try it online!

Inputs should be separated by a space. Exits with error when output is a multiple of 5.

\$\endgroup\$
2
  • \$\begingroup\$ @DLosc, I did have a working version, but then tab chaos struck and I posted the wrong one!; Fixed now \$\endgroup\$ Apr 19 at 2:13
  • 1
    \$\begingroup\$ Ah, makes sense. Here's 89 bytes. \$\endgroup\$
    – DLosc
    Apr 19 at 4:09
0
\$\begingroup\$

Core Maude 3.4, 88 bytes

mod A is gb LIST{Nat}. var A B : NzNat . cq A B =(A + B)if A < 5 . eq s_^5(A)= 5
A . endm

The tallies to add are input as a flat list of digits between 1 and 5, which is reduced in the module. The resulting output is another list of digits.

Example Session

Maude> select A .
Maude> reduce 1 1 .
result NzNat: 2
Maude> reduce 4 2 .
result NeList{Nat}: 5 1
Maude> reduce 5 1 5 5 5 2 .
result NeList{Nat}: 5 5 5 5 3
Maude> reduce 5 5 .
result NeList{Nat}: 5 5
Maude> reduce 5 2 5 3 .
result NeList{Nat}: 5 5 5

Ungolfed

mod A is
    gb LIST{Nat} .

    var A B : NzNat .

    cq A B = (A + B) if A < 5 .
    eq s_^5(A) = 5 A .
endm

For any two adjacent tally marks, if the left is less than 5 then the program will combine them. So 4 5 becomes 9. If any tally mark is greater than 5 then the program will break it apart. So 9 becomes 5 4. Any “unfull” tally marks will percolate down the list until they are combined at the end.

\$\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.