Correct way to add numbers to get lots of 8's
Bash + Common Linux utilities, 80
s=~0my3[_p^?{}s
h()(tr 0-9 $s<<<$1|xxd -p)
dc -e$[0x`h $1`|0x`h $2`]P|tr $s 0-9aQ
Note the ^?
in the source should be replaced with an ASCII 0x7f character.
The string s
is each 7 segment digit 0-9, a, Q
encoded with each segment corresponding to a bit of an ASCII char.
The h()
function transliterates the input number from decimal to the encoding specified by s
, then outputs the result as a raw hex string.
The two resulting raw hex strings are OR
ed together using regular bash arithmetic, then output by dc
's P
command as a bytestream. This bytestream is then transliterated back to decimal + a + Q and output.
Note also that when using the <<<
bash herestring construct in function h()
a newline is implicitly appended to the redirected string. This doesn't matter - it is simply translated to 0x0a
at the end of each hex string; when the two hex numbers are OR
ed together, the result is still 0x0a
in the last char which doesn't get transliterated and thus simply translates back to a newline which is output after the result.
Test output:
$ for testcase in \
> "12345 123" \
> "88888 42" \
> "0 23" \
> "1234 56789" \
> "4 7"; do
> ./7segadd.sh $testcase
> done
12389
88888
28
58a89
Q
$
Python 2, 155 bytes
def f(a,b):exec"a=[ord('?(u|j^_,♥~'[int(c)])for c in a];a=max(len(b)-len(a),0)*[0]+a;a,b=b,a;"*2;print`['214567q3a980'[(c|d)%13]for c,d in zip(a,b)]`[2::5]
Replace the ♥
with a DEL
character (0x7F).
Calling f("12345", "123")
prints 12389
.
JavaScript (ES6), 158 144 bytes
f=(s,t)=>t[s.length]?f(t,s):s[t.length]?f(s,' '+t):s.replace(/./g,(c,i)=>"540q9361278a"[(a[c]|a[t[i]])%13],a=[119,20,47,31,92,91,123,22,127,95])
Saved 14 bytes by shamelessly stealing @Lynn's %13
trick.
f=(s,t)=>t[s.length]?f(t,s):s[t.length]?f(s,' '+t):s.replace(/./g,(c,i)=>"540q9361278a"[(a[c]|a[t[i]])%13],a=[119,20,47,31,92,91,123,22,127,95])
;o.textContent=[...s="0123456789"].map(c=>f(c.repeat(10),s)).join`
`;
<pre id=o></pre>