I remember on time I dreamed I was working in my 3D program continuing to create a model that I had been working on that day. Man, I was pissed when I woke up and realized I hadn't actually done the last half of the work in real life.
One strange thing that happened to me is when I had this dream (following some long and deep programming session) where the "reality rules" were very close to the logical spirit of programming. Suddenly if I had to reach a platform that was very high, I could just "imagine and build my own tools" to add new abilities to my being. Very difficult to explain. I could clone objects if needed. I could somehow "loop" things or actions. etc.
As far as I know I did not solve any programming problem with this dream though.
I was on a team of three building a compiler for a microprocessor. I was writing the code generator which included register allocation and other hairy stuff. We had to produce pretty good code, as assembler was being replaced by our compiled code. Well, register allocation can be pretty hairy, and we were under a deadline. I had stayed up very late working on the problem, but didn't really finish when I crashed. I was half dreaming nonsense most of the night, but had one flash of inspiration in the middle of the night that solved the core of the problem.
It was not exactly restful sleep. Not sure it was really sleep, I would say.
If I do enough coding late at night, I usually code review in my sleep and wake up knowing tons of things to change. I've learned to do my CS homework last thing before bed and wake up early enough to fix bugs.
More generally, write down ideas when they happen. An idea may be crap or brilliance, but it's worth nothing for sure if you don't remember it in the first place.
Both falling asleep and using the bathroom are similarly unusual because they don't really involve anything except staying still. So I'm not surprised that you think well in the bathroom.
I figured out the orbits in an inverse r^5 force field during a particularly productive nap in the winter of 1993. (IIRC, the orbits are closed, like those in an inverse r^2 field, but---bang!---they intersect with the center of force.)