Solving gapped Hamiltonians locally