This is especially hard to do if you're in a more senior position and tend to take more interest in solving better problems than small algorithms problems.
I once worked in an environment where such distractions are tightly monitored by management (they use some sort of tools to track what you browse, plus it's an open office space where everyone can see what you do). This helped me a lot in being more focused and stricter to myself. I guess regardless of what tools you use to block distractions, it's all down to your strong will to resist it. It helps much more if your task is what you love to do.
No one gets the perfect start, and there are gonna be painful change as you progress and realize past mistakes. That's part of the learning process. Regardless of the changes you're still improving and gaining experience, and that's never a bad thing. Try to look more on the positive side of things. Take small steps and satisfaction once u get it, then move forward.
I have seen plenty of jobs where requirements are to go for novel solutions, get preliminary results and then pass on the job of following up to another dept. I wouldn't say your case is bad, but just not a good fit. Nevertheless there should be balance in everything, getting fired twice in short time may not be a good sign.
I always find morning time more productive than late night, but you get the time pressure of having to finish soon (probably for work) compared to working at night where your sleep time is the only thing holding you back.