final algorithm for hackathon