Self-discipline. Impulses and social pressure are more like brightly lit billboards than ditches. You don’t have to fall for them.
Personally, as a nearly-life-long sugar junkie, I know that if I'm around a pile of cookies, it is difficult to resist, and will wear me down until I eat one, and then another, and then another...
On the other hand, if there is no pile of cookies, there's no temptation or possibility of that happening.
I have had much more success with adjusting my environment than with trying to exercise willpower.