I'm not sure which of these factors contributed most to getting out of my bad spot, but in no particular order:
1. Running. (Strength training is good for many other goals, but frankly handling heavy objects isn't safe when you're severely fucked up and can't see straight. I can run even when I'm pretty deep in the hole and it has an almost curative effect.)
2. Making friends.
3. Starting a project I was actually successful at.
4. Learning not to tie my self-worth quite so tightly to my performance. The notion of "human dignity" -- the idea that all people have worth, even the ones who have personal flaws or make mistakes -- is really important.