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 likes this magic so much that he's cast it on every single array in sight. Now he needs help reversing his own magic...
Task
Reverse Fen's Wick spell. To recap, the Wick spell works as follows:
- 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 input 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
]
In this task, you need to reverse it, so given Wick(A) = [1, 3, 3, 10, 5, 11, 7, 36, 9, 10]
as input, your program should output A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
[] -> []
[999] -> [999]
[3,4,4] -> [3,1,4]
[3,4,4,9,5,14,2,31] -> [3,1,4,1,5,9,2,6]