[Community Puzzle] Auto pickup

https://www.codingame.com/training/medium/auto-pickup

Send your feedback or ask for help here!

Created by @vivax3794,validated by @IAmNoob,@Gevater_Tod4711 and @TBali.
If you have any issues, feel free to ping them.

Hello,

the input are:
line 1: the length of the packet
line 2: the packet to inspect

Perhaps there is a mistake for the test case 5? (line1 gives 48, but line2 len is 63).

2 Likes

It’s not a medium puzzle, it’s an easy puzzle.

Indeed, I noticed that too and corrected it. Thanks for the observation.

It is an easy puzzle. Besides, line one to state the string length seems to be unnecessary.

Hi,

I was struggling with the 3rd validator and I think that the statement is somehow incomplete.

3rd validator input is: “00100101110100011”.
At first, my algorithm was detecting the first occurrence of “101”, so the one that starts at index 5.

Then, I realized it was not a “valid packet” because then the length of the item id would be too large for the whole string.

So, from what I understand, that occurrence of “101” is discarded and we proceed to the next one, at index 9. That one is valid and that is how we get “00100011” as the answer.

I think that “invalid packet” rule should be specified in the statement, unless I misunderstood something.

Plus, I don’t understand how I am supposed to pass the last validator (“that is a long one”).
The input starts with “100100111100001001001011001010100111011110[…]”
Again, from what I understand, the first packet can be found at index 20 and has a length of “1001” so the item id is “010100111”.
Therefore, I find that my first response packet is: “0011001010100111”.
But the validator is expecting the first packet to be: “0010011101”…
How can it be? :frowning:

1 Like

Your algorithm is incorrect, no matter you are detecting the first or second or third occurrence of “101”.

There are 3 bits used as opcode so that there are at most 8 different opcodes. Only two of them are defined. You should always follow the data format to read the opcode bits and then size bits and then the payload. When the opcode is undefined, discard the payload. When it is defined, follow instruction to process.

2 Likes

I see. So I should read the first 3 bits and follow the instructions only if these 3 bits are “101”?
Your answer is a little bit abstract to me and I’m not sure if I understood correctly.
Anyway, thanks for your answer! :slight_smile:

EDIT: Oh, I think I figured out. Actually, no matter what the opcode is, it’s always followed by 4 bits defining the packet length and then by [packet length] bits. And I just have to take the packet into account if the opcode is known (in my case, “101”). Thanks!

Hi,
How did you pass the last validator ? I understand your comment and totally don’t understand why we are getting “0010011101” for the last validator. I don’t understand how java_coffe-cup’s answer helped you solve this problem…
Cheers !

Hi :slight_smile:
What java_coffee_cup made me understand is that you do have to take each opcode into account, get its packet length and its packet info, no matter the opcode is defined or not (= no matter it’s a “101” or not). Then, if it’s indeed a “101”, you eventually do stuff. Finally, no matter the opcode was defined or not, you discard (i.e. “move forward into the string”) the entire payload (opcode + length + info).

For example, with 100.1001.111000010.010.0101.10010.101.0011.101.111.0
(I inserted dots manually for better understanding)
Idx. 0: “100” = opcode
Idx. 3: “1001” = length (= 9)
Idx. 7: “111000010” = info
"100" is an undefined opcode so I don’t do anything
Idx. 16: “010” = opcode
Idx. 19: “0101” = length (= 5)
Idx. 23: “10010” = info
"010" is an undefined opcode so I don’t do anything
Idx. 28: “101” = opcode
Idx. 31: “0011” = length (= 3)
Idx. 35: “101” = info
"101" is a defined opcode so I print “001{length}{info}” = "0010011101"

Etc…
Does it help?

2 Likes

Hi,
Yep, understand it now ! Managed to get 100% thanks to your help, so thank you very much !
Cheers

1 Like