This sounds very much like me. I beat it by finding a routine that works for me, a routine that helped me become more disciplined... and it took a while to beat, so don't expect immediate results. Also, while this worked for me, it may not work for you, everyone [despite those telling you that you're not a unique snowflake] is different and different things work for each of us so take what is helpful from my routine and ignore the rest, YMMV.

First step: Finding your zone. This is a matter of conditioning and conditioning doesn't come easily. Find something that puts you in a zone of focus. It doesn't matter what the focus is on, it doesn't matter what that zone is - the key is finding focus and maintaining it for longer and longer periods until you can easily maintain focus for a number of hours at a time [I'm told there are drugs that do this very effectively, but I personally try to avoid drugs wherever possible]. Whatever it is should be something that doesn't allow you to lose your focus or give up, even when the going gets tough and every instinct in you is telling you to throw in the towel.

For me it was cycling - totally unrelated to programming. It was better for me than being in a gym because if I decide to stop pedaling, I stop going anywhere. In a gym, this means just getting off the bike and I'm no further behind. If you're 10 miles from home and stop pedaling, you're 10 miles from home and the only way back home is to get back on your bike and start pedaling again. Sitting on the side of the road procrastinating isn't going to help, nor is it going to be in any way enticing to sit there. After 40 miles, you could be exhausted and realizing you still have a 5 mile climb ahead of you before you can put your bike down; there's no way around it, you just have to get on and do it.

I've set up a playlist on my phone of music that runs around 130-140 bpm [that's approximately the speed of club music that makes you wanna dance] and will run without looping for the duration of my ride. I listen to the same playlist every ride. You might get sick of it, but you listen anyway. After a few weeks of agony and wanting to throw in the towel but doing it anyway you start to find peace in the pain and it becomes a meditation, you don't even hear the music consciously any more but the beat drives you forward, you find the zone and it's just you, the pain and the road and before you know it the circuit is over. For me this process took a number of weeks, but now, if I put that playlist on, even if I'm not on my bike, my brain snaps into that zone. It's been conditioned to focus when that playlist comes on, just like Pavlov's dog salivating when the bell rings. [This requires ongoing maintenance]

Step 2: Remove necessary distractions. When I say necessary, I mean unavoidable things that will need doing and will break your focus when they are required to be done because you didn't take the preemptive strike of killing them off first. Things like important phone calls to your accountant or the bank. If you can't focus while you have a messy desk, clean your desk, get your coffee, eat your breakfast, do all of these unavoidable things first.

Step 3: Understand the problem intimately, ensure that you can recite it inside outside, upside down and backwards. You don't want to have to keep going back to ensure you understand the problem, this will break your focus. If you need to pester someone to help you understand the problem, pester them until you have fully digested every nuance of what it is you're trying to achieve.

Step 4: Understand the solution to that problem intimately. Ensure that you understand every single step that will take you from where are right now to where you need to be. Any pieces you don't understand, go back and reread step 4. If you keep having to come back and figure stuff out, this will break your focus. Again, pester whoever you need to in order to completely understand the path to the solution.

Steps 3 and 4 are my biggest triggers for procrastination. If I don't understand the problem or the solution to that problem well enough, I can't maintain focus. It might take me days to get down to it if I let myself skip either of these steps - so I don't.

Step 5: Everything else can wait: No Facebook, no Hacker News, no Blinkfeed, no Quora, no email, no phone calls, no text messages, no whatever else it is you like to waste time with. Put them all aside and have the discipline to stay away from them until you're finished this step. Now, do whatever it was from Step 1 that puts you into your focused zone. Get on with completing the steps you've laid out in Step 4.

Step 6: You're done, go reward yourself with all those other distractions that you put aside to complete Step 5. Congratulations.