When doing a hard but bounded challenge, there's a set of techniques that I use. I like to call them desperation management, although perhaps you'd like to think about hope or willpower for a more positive framing. They're most useful for physical exhaustion, but often applicable to mental labor as well. Moreover, at least for me, the willpower is depleted before other resources. Most of the time I only use these on myself, but also occasionally for motivating others.
These techniques don't really work when you cannot estimate how much work is left. In the worst case, you don't even know if you're making any progress, for instance when attempting to prove a theorem. I've never been good with those, and I wonder how much of it is the lack of suitable mental tools to keep up willpower.
The most fundamental technique is progress tracking. Often you start energetic and that carries you for a while. Sometimes that gets you to the goal, and there's no need for desperation management. The impulse to check how much you've done already is often the first sign of exhaustion. I just looked the word count of this text after writing that, and 116 words definitely caused some negative feelings. But only a bit. Usually the first time you do it doesn't matter that much.
It's important to not constantly look at the progress. Time slows down when you're watching the clock, and distances longer when you keep staring at the map. Pick natural points to check it, so there's no meta-progress to look at. Look at the clock only when reaching a new hilltop. Check the distance left at most every fifteen minutes, and only check the clock when you feel fifteen minutes might have already passed. Try to do this as rarely as possible, as the mere act of looking strains you.
Intermediate goals help with this. There are two different kinds of these: "halfway there" and "just a bit more" sum them up pretty well. The first kind is sort of automatic for me at least, but it still helps.
Pairing up an intermediate goal with some reward tends to work well. Often the only one you can think of is resting a couple of minutes, which works but might slow you down quite a bit if done too often. Weirdly enough, one of the smaller rewards I've found quite useful is a permission to check the progress.
There's a dark arts technique related to this, something that I use quite often myself. I promise myself some reward after reaching a goal, and then move the goalpost a bit further, often repeatedly. It works despite, or because of, I recognize I'm doing this.
One case worth discussing more is using limited external resources as rewards. When hiking a difficult trail with limited amount of water, it sometimes makes sense to motivate yourself with the water, instead of drinking it in a hydration-optimal way. At least for me rationing resources is also an interesting puzzle, keeping your mind off the main effort is typically helpful.
Distractions in general are quite useful. Looking at the scenery or just telling stories inside your head work well for outdoorsy activities. For mental labor, I like energetic music and walking around. And for both, sugary snacks. If the challenge was not self-imposed, thoughts of revenge or at least unfairness seem to help too, though I'm not sure if that's actually true.
And now I'm halfway through my blogpost a day challenge. It feels quite desperate, and I've felt many times that it's not worth doing anymore, as I don't have the energy to produce the quality I'd like. But wasn't that one of my original goals, to combat perfectionism? To easy-mode through it, if necessary? I think so. Just a bit longer, at least.