Write the shortest program or function that mimics the addition in this XKCD strip:
Input
Two positive decimal integers containing only the digits 150
.
Output
The positive decimal integer result of adding the two inputs together in the manner shown above. This output should also only have the digits 150
.
Specification
Input and Output
Input and output are numbers only containing digits 150
.
Roman Numerals
This challenge will use Standard Form numerals:
Thousands | Hundreds | Tens | Units | |
---|---|---|---|---|
1 | M | C | X | I |
2 | MM | CC | XX | II |
3 | MMM | CCC | XXX | III |
4 | MMMM | CD | XL | IV |
5 | MMMMM | D | L | V |
6 | MMMMMM | DC | LX | VI |
7 | MMMMMMM | DCC | LXX | VII |
8 | MMMMMMMM | DCCC | LXXX | VIII |
9 | MMMMMMMMM | CM | XC | IX |
Note the subtractive notation on multiples of 40
and 90
, and additive notation on multiples of 1000
.
Worked Example
Consider a test case below, 1010111 + 101051 = 1050110 (23 + 26 = 49)
.
Convert the inputs to Roman numerals, then to Arabic numerals:
1010111 => XXIII => 23
101051 => XXVI => 26
Perform the addition:
23 + 26 => 49
Convert the output back to Roman numerals, then for each Roman numeral substitute its Arabic numeral value:
49 => XLIX => 10 50 1 10 => 1050110
Test Cases
A + B = C (explanation)
=========
1 + 1 = 11 (1 + 1 = 2)
11 + 11 = 15 (2 + 2 = 4 == IV)
15 + 5 = 110 (4 + 5 = 9 == IX)
101010 + 1 = 1010101 (30 + 1 = 31)
1010 + 111 = 1010111 (20 + 3 = 23)
1010 + 15 = 101015 (20 + 4 = 24)
1010 + 5 = 10105 (20 + 5 = 25)
1010111 + 101051 = 1050110 (23 + 26 = 49)
101015 + 51 = 101010 (24 + 6 = 30)
1010015 + 15 = 101005111 (94 + 4 = 98)
100010010010101015 + 1000100010010010010505 = 100010001000500501010110 (1234 + 2345 = 3579)
100010010010101015 + 1000100010001000100010001000100050010010050105 = 100010001000100010001000100010001000100100010100110 (1234 + 8765 = 9999)
Other Notes
As seen in the comic, Randall is doing a simple substitution of Roman numerals with their decimal equivalents:
I => 1
V => 5
X => 10
Presumably, this will be extended for all Roman numerals (up to 1000):
L => 50
C => 100
D => 500
M => 1000
For more information, see the ExplainXKCD wiki page.
This is code-golf, so shortest code wins!
100010001000100010001000100010001010010050105
isMMMMMMMMXCCLXV
, but it should beMMMMMMMMDCCLXV
(1000100010001000100010001000100050010010050105
). \$\endgroup\$"1010+15"
) \$\endgroup\$