A numerical method of inferring the budget history of a glacier from its advance and retreat