Numerical simulation of the final stages of terrestrial planet formation