Ladder game algorithm in C