One thing I have found helpful in this sort of situation is to start by writing the exploratory code as a self-contained test.
So a test that has no production code it is testing.
Preferably in a language with really fast turnaround, a "scripting" language.
After a while, I have a self-contained test that does something, and does it reliably.
I can then refactor the test by extracting the production code.