Getting into a state of flow is the only way I can make progress when writing complex software. I also think it's possible to structure your life in a way that maximizes the amount of time you can spend in a state of flow each day. Here's what I try to do to engineer flow for myself.
Sleep is the single most important thing that affects my productivity. I would rather have a good night's rest than any other thing on this list. I have a hard rule where I can't take any caffeine after 4PM. If I make myself coffee at 3:45PM and I forget about it until 4:05PM, then I save it in the fridge for the next day. The marginal boost of evening efficiency isn't worth tragically destroying my productivity the next day.
On days where I'm over-stimulated, taking 0.3-0.9 mg Melatonin seems to help. I also think it's important to sleep in a cold room, the sweet spot seems to be about 60-65º F.
Unfortunately the human brain was never meant to write code for multiple hours a day, and the fact we've repurposed our primate brains to do so is honestly mind-boggling. To get around this, I consume a lot of caffeine. If you're reading this, then I'm sure you do too. In an ideal world, we'd all be caffeine-free while also still being able to solve complex problems. Unfortunately we do not live in that world.
The biggest downside to caffeine is the inverse correlation between the amount of coffee I drink and my sleep quality. It's important that I find a delicate balance between being stimulated while also not over-caffeinating. If I had to pick between caffeine and sleep, sleep wins every time.
You can dramatically improve your productivity by lowering the CO2 levels in the room where you work. By dramatically improve, I mean on the order of 20% or more. A study compared the cognitive performance of people working in a room with CO2 levels at 1,000 ppm (for reference, the ambient outdoor CO2 level is just 400 ppm) and people working in a room at with 600 ppm CO2. The people in high CO2 room scored around 20% lower on cognitive tests than those in the room with low CO2.
Until I got an air quality monitor, I assumed the room I worked in had low levels of CO2 since I opened the window often. It turned out my ambient CO2 levels were around 1,200 ppm – even higher than the levels used in the study! To fix this, I almost always keep a window cracked while I work which brings the ambient CO2 levels to around ~450 ppm.
I suck at exercising. Seriously. Every time I exercise after not moving for a few days I always think to myself "this feels amazing, why don't I do it more often?"
Honestly just figuring out a way to move my body and elevate my heart rate works wonders. Ideally it's lifting weights, but it doesn't have to be. I've found playing Beat Saber for an hour just as invigorating as going on a run. I also just read a headline that said "Not exercising worse for your health than smoking, diabetes and heart disease". There's really no reason to not exercise, and I empathize with you if you're also an extreme hyperbolic time discounter.
I must work alone, or at least with other people who can also work quietly for an entire day. Being in an environment where nobody can disturb me is important because I lose my train of thought easily. Personally I find this to be the largest underrated benefit of remote work.
Uninstall video games
Valorant is my drug of choice, and it's a near-perfect match for brain type. I've learned from experience that I can spend the greater chunk of a waking day clicking on people's heads, so I set a Schelling fence for myself and just deleted the Windows partition on my computer entirely. Other people likely have better self-control than I do, but a video game once consumed the better part of a decade so I personally prefer to avoid them at all costs.
I have the unfortunate habit of aimlessly scrolling through Twitter whenever I get stuck on a hard problem, which is sort of like sticking a rod in my bike's spokes while I'm trying to ride it up a steep hill. I'll just lose the momentum I had and have to regain context all over again. To avoid this trap, I use Freedom and block all distracting websites.
Make a day plan
I have to make a list of the things I want to do before I start my day otherwise I won't work on my A+ problem. Letting my curiosity set my day's direction can be intellectually satisfying, but when I need to Get Things Done, using a list is a must. My tool of choice is Sunsama.
I think deep house is my ideal genre for long periods of intense focus. Here's one of my favorite mixes (it's a live set on a hot air balloon!) I also use noise cancelling headphones and can't focus without them. Even if I'm in a quiet room, I find the insanity-inducing silence of noice cancellation to be wonderful.
I avoid sugar and processed foods out of habit, however I do that more for health reasons than productivity. I haven't really found a direct correlation between the food I eat and my productivity.
Pick the right problem
Finally, no matter how productive I am, none of it matters unless I'm working on the right problem. The countless astronomers who dedicated their lives to understanding the geocentric model of the solar system did little to progress science. Copernicus was far more productive because heliocentrism was correct.
Be sure what you're working on isn't geocentrism.