We present a new method to determine the star formation and metal enrichment histories of any resolved stellar system. This method is based on the fact that any observed star in a colour-magnitude diagram will have a certain probability of being associated with an isochrone characterised by an age t and metallicity [Fe/H] (i.e. to have formed at the time and with the metallicity of that isochrone). We formulate this as a maximum likelihood problem that is then solved with a genetic algorithm. We test the method with synthetic simple and complex stellar populations. We also present tests using real data for open and globular clusters. We are able to determine parameters for the clusters (t, [Fe/H]) that agree well with results found in the literature. Our tests on complex stellar populations show that we can recover the star formation history and age-metallicity relation very accurately. Finally, we look at the history of the Carina dwarf galaxy using deep BVI data. Our results compare well with what we know about the history of Carina.