What if you're trying to solve a block and someone else solves at first, was all your time wasted?
No. There is no progress towards solving a block. Each hash is like a lottery ticket. It either instantly wins or it instantly loses. Regardless of if someone else solves a block your "losing" hashes are still losers.