MATL, 12 bytes
tnEXyPY+c!Us
Input is a string. Try it online!
###Explanation
Explanation
Consider input '123'
as an example.
The code duplicates the input (t
) and builds an identity matrix (Xy
) of size twice the input length (nE
):
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
then flips it upside down (P
):
0 0 0 0 0 1
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 0
1 0 0 0 0 0
The input string, interpreted as ASCII codes of the digits, is equivalent to the numeric row vector
49 50 51
Full-size two-dimensional convolution (Y+
) of the above vector and matrix gives
0 0 0 0 0 49 50 51
0 0 0 0 49 50 51 0
0 0 0 49 50 51 0 0
0 0 49 50 51 0 0 0
0 49 50 51 0 0 0 0
49 50 51 0 0 0 0 0
Interpreting those numbers back as ASCII codes (c
) gives the following char matrix, with char 0 represented as space:
123
123
123
123
123
123
Transposition (!
) transforms this into
1
12
123
123
123
123
23
3
Interpreting each row as a number (U
) gives the numeric column vector
1
12
123
123
123
123
23
3
and summing it (s
) gives the final result, 531
.