Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
Fen is a magician Elf. He can cast spells on an array of numbers to produce another number or array of numbers.
One of his inventions is the Wick spell. He loves to use this spell because "once this spell is applied, a combination of prefix sum
spells and change number
spells becomes magically cheaper".
Task
Implement Fen's Wick spell.
- Given an array of positive integers
A
of lengthn
, - Replace each item at 1-based index
i
with the sum of lastj
elements, wherej == 2 ** (trailing zeros of i in binary)
.
For example, if the given array is A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
,
Wick(A) = [
1, # 1
3, # 1+2
3, # 3
10, # 1+2+3+4
5, # 5
11, # 5+6
7, # 7
36, # 1+2+3+4+5+6+7+8
9, # 9
10, # 9+1
]
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
[] -> []
[999] -> [999]
[3,1,4] -> [3,4,4]
[3,1,4,1,5,9,2,6] -> [3,4,4,9,5,14,2,31]