Instead of being a skillful warrior capable of slaying Hydras (see here and here), this time you are a warrior that has no prior knowledge on how to kill one or which weapons to use against the creature.
In this problem, whenever you cut a single head off, two will grow in the same place. Since you don't have the mechanism to cut many heads off simultaneously, the number of heads will only grow. In this case, our Hydra can start with N
(N ⩾ 1) heads. Let's call the first encounter a generation and we will represent the heads from the first generation as 0, the heads created after the first blow as 1, and so on.
Input
You will be given an integer N
representing how many heads the Hydra initially have and a list of size N
containing in which index (in the examples I will use 0-indexed format) you will cut a head off. You can always assume the indexes given are valid - remember that the list (i.e.: the heads) will grow as you cut heads off.
Example
Input: N = 4
and [0,4,2,5]
Generation 0 - Attack index 0
0 0 0 0 => 1 1 0 0 0
^ ^ ^
Generation 1 - Attack index 4
1 1 0 0 0 => 1 1 0 0 2 2
^ ^ ^
Generation 2 - Attack index 2
1 1 0 0 2 2 => 1 1 3 3 0 2 2
^ ^ ^
Generation 3 - Attack index 5
1 1 3 3 0 2 2 => 1 1 3 3 0 4 4 2
^ ^ ^
Last generation
1 1 3 3 0 4 4 2
As you can see, the indexes given are related to the list of the previous generation.
Output
You are required to output the last generation.
Test cases
N = 1 and [0] => [1,1]
N = 2 and [0,0] => [2,2,1,0]
N = 2 and [0,1] => [1,2,2,0]
N = 2 and [1,0] => [2,2,1,1]
N = 2 and [1,1] => [0,2,2,1]
N = 4 and [0,4,2,5] => [1,1,3,3,0,4,4,2]
N = 6 and [0,0,0,0,0,0] => [6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0]
N = 6 and [5,6,7,8,9,10] => [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 6]
N = 10 and [1,7,3,12,9,0,15,2,2,10] => [6, 6, 9, 9, 8, 1, 3, 3, 0, 0, 10, 10, 2, 5, 5, 0, 0, 4, 7, 7]
This is code-golf so shortest answer in bytes wins!
N
(...) and a list of sizeN
(But I missed that part as well when I first read the challenge.) Therefore,N
is simply useless. \$\endgroup\$N
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by givingN
instead of them relying onarray.size()
or similar. \$\endgroup\$