The physics and maths of keeping elections fair and represen