Modular broadcasting
Charcoal, 25 23 bytes
AθIE⌈EθLιE⌈EθL§λ⁰ΣE觧νιλ
Try it online! Link is to verbose version of code. Takes input as a 3-dimensional array. Explanation:
Aθ
Input everything.
θ Input
E Map over arrays
ι Current array
L Length
⌈ Maximum
E Map over implicit range
θ Input
E Map over arrays
λ Current array
§ ⁰ First element
L Length
⌈ Maximum
E Map over implicit range
θ Input
E Map over arrays
ν Current array
§ ι Cyclically index using outer loop index
§ λ Cyclically index using inner loop index
Σ Sum
I Cast to string
Implicitly print on separate lines and paragraphs
MATL, 25 24 bytes
,iZy]vX>XKx,@GK:KP:3$)]+
Try it online!
Finally! It only took a week for the Language of the Month-inspired challenge to be answered by the Language of the Month!
My guess is that it's not quite as short as possible, but I'm happy enough because my initial version was over 40 bytes. edit: I was right, Luis found another byte to squeeze out!
,iZy] # do twice: read input and find the size of each dimension
vX> # find the maximum along each dimension
XKx # save this into clipboard K and delete from stack. Stack is now empty.
, # do twice:
@G # push the input at index i where i=0,1.
# MATL indexes modularly, so 0 corresponds to the second input
K: # push the range 1...K[1]
KP: # push the range 1...K[2]
3$) # use 3-input ) function, which uses modular indexing
# to expand the rows and columns to the appropriate broadcasted size
] # end of loop
+ # sum the now appropriately-sized matrices and implicitly display
Python 3, 127 126 125 bytes
golfed a byte by changing sum(m)
to m+n
One more byte thanks to @Jonathan Frech
lambda y:[[m+n for n,m,j in Z(l)]for*l,i in Z(y)]
from itertools import*
Z=lambda y:zip(*map(cycle,y),range(max(map(len,y))))
Takes input as a list of two 2-dimensional arrays.
- The
Z
lambda takes two arrays as input and returns an iterator yielding an index and merged values from both arrays, until the index reaches the largest array's length. The index variable isn't useful to me and costs me bytes, but I don't know how to do without it... (related) - The main lambda just takes the input arrays and calls
Z
on the outer and inner arrays. The innermost values are added together.
Try it online!
Using itertools.cycle
feels a bit like cheating but I think I've been punished enough by the sheer import statement length :)
I'm sure this could be golfed some more, especially the iteration method that leaves these useless i
and j
variables. I would be grateful on any tips on how to golf that, I'm probably missing something obvious.