Copyright protection only comes into play when an actual copy has been made of a work.
If two people write a "hello world" program independently of each other, then both programs are protected by copyright and can be published with any license that the author wants.
But as both worked independently of each other, neither can claim that the other infringed on his copyright because no copies were made.
It doesn't even matter here if one has published his work before the other even started, to claim a copyright infringement, you have to prove that someone made a copy of your work.
There is one grey area here: If person A has read the code from person B and then creates something very similar, then there might be cases where person B could successfully argue that person A has effectively made a copy.
For such a claim to be successful, most likely the code of person B has to be of enough originality that it would be unlikely that person A had come up with his solution without seeing the work from person B. This would not be the case with a "hello world" type of program.