39
1
Introduction
Consider two non-empty integer arrays, say A = [0 3 2 2 8 4] and B = [7 8 7 2]. To perform alignment addition on them, we do the following:
Repeat each array enough times to have total length lcm(length(A), length(B)). Here lcm stands for lowest common multiple.
A -> [0 3 2 2 8 4][0 3 2 2 8 4] B -> [7 8 7 2][7 8 7 2][7 8 7 2]
Perform element-wise addition on the repeated arrays, and cut the result at every position where there's a cut in either of them.
A -> [0 3 2 2 8 4][0 3 2 2 8 4] B -> [7 8 7 2][ 7 8 7 2][7 8 7 2] -> [7 11 9 4][15 12][7 5][9 10 15 6]
This array of arrays is your result.
The task
Your inputs are two non-empty arrays of integers, and your output shall be the result of their alignment addition, as defined above. The inputs and output can be in any reasonable format. You don't have to worry about integer overflow when performing the addition.
Rules and scoring
You can write a full program or a function. The lowest byte count wins.
Test cases
[1] [4] -> [[5]]
[1,2,-3,-4] [15] -> [[16],[17],[12],[11]]
[0,-4] [2,1,0,-3] -> [[2,-3],[0,-7]]
[0,3,2,2,8,4] [7,8,7,2] -> [[7,11,9,4],[15,12],[7,5],[9,10,15,6]]
[18,17,16] [-1,-2,-3,-4] -> [[17,15,13],[14],[16,14],[15,13],[15],[16,14,12]]
[18,17,16,15] [-1,-2,-3,-4] -> [[17,15,13,11]]
[1,1,1,1,1] [6,5,6,5,6,5,6,2,1] -> [[7,6,7,6,7],[6,7,3,2],[7],[6,7,6,7,6],[7,3,2],[7,6],[7,6,7,6,7],[3,2],[7,6,7],[6,7,6,7,3],[2],[7,6,7,6],[7,6,7,3,2]]
[1,1,1,1,1,1] [6,5,6,5,6,5,6,2,1] -> [[7,6,7,6,7,6],[7,3,2],[7,6,7],[6,7,6,7,3,2]]
[1,1,1,1,1,1,1] [6,5,6,5,6,5,6,2,1] -> [[7,6,7,6,7,6,7],[3,2],[7,6,7,6,7],[6,7,3,2],[7,6,7],[6,7,6,7,3,2],[7],[6,7,6,7,6,7,3],[2],[7,6,7,6,7,6],[7,3,2],[7,6,7,6],[7,6,7,3,2],[7,6],[7,6,7,6,7,3,2]]
C doesn't have a way to know an array's length -- can I request the length of the array as an argument, or have it stored at the beginning of the array? – cat – 2016-12-17T16:47:53.457
1@cat You can take the lengths as extra arguments, if there's no other way of getting them. – Zgarb – 2016-12-17T17:11:10.127