Sounds like you caught a drift of the Tetris Effect.
Right now I'm getting seriously into Factorio, and I had this experience a few nights ago. I half-woke up and kept having these thought-hallucinations about the best method to use to optimize the way my resource refiners were feeding into my main bus, because I needed to make X% more sleep-cogs in order to go back to sleep.
It's actually kind of infuriating when it happens, sometimes it keeps me in that half-awake state for as long as thirty minutes. Been happening to me since I was a kid.