A trick my mother taught me young that works as you discovered
as well : think of a problem as you fall asleep and an answer
will be ready for you just as you wake up. You don't get very
much time to capture it as the awakening process progresses so
get it out of your head in some legible format so you can be
reminded of it later that day.