The trick is leaving good notes to yourself outlining the thought process, what was tried, what should be tried next.
Then you read your notes in the morning with a fresh mind and <click!>
If you just slam the laptop shut in frustration, you lose all progress.