Added a working, generic solution to p1.