🐍🪜
Snakes & Ladders
Week 40
You are rummaging through some old boxes and find the classic children's game Snakes and Ladders (also called Chutes and Ladders) that you haven't played for 20 years
If you are unfamiliar, here is a picture of a typical board and the rules:
- It's a multi-player game where each player takes turn rolling the dice once - they move forward by the score of the dice (i.e. if they are on square 1 and roll 4, they will move to square 5). For this challenge, you will just be considering the rolls of an individual player
- The dice is a typical 1-6 dice
- The players start in the bottom left (square 1) and the game ends when a player reaches the end square - i.e. 100 on the 10x10 board above
- Note how the square order alternates - left to right (1-10) in the bottom row, then right to left (start on square 11 at right of board and move left to square 20) in the next row
- If a player lands at the base (bottom) of a ladder, they instantly climb up - e.g. if a player was on square 25, rolled a 3, they would land at the base of square 28 and climb to square 84
- If a player lands at the head of a snake, they fall back down to the end of the tail - e.g. if a player was on square 52, rolled a 2 to square 54, they would fall back to square 34
- You need the exact score to finish - e.g. if you're on square 97 and roll a 4, you stay on square 97 (we shouldn't include this as a valid move in our output, otherwise you can have infinite output) - you would have to e.g. directly roll a 3 (or equivalently roll a 2 and 1 in either order in subsequent turns to exactly reach 100)
- In our board, you can assume that you won't get a snake/ladder chain (i.e. multiple jumps/falls with a single roll) - e.g you won't have a square which contains the top of 1 ladder and the base of another ladder, or the top of a ladder and head of a snake etc. In other words, there will be at most 1 snake or ladder for each roll.
The goal of this challenge is to output every possible sequence of valid rolls (i.e. ignore those that are too high and would result in your piece overshooting the end square) that would result in your piece ending on the winning square
For example, suppose we have the following 3x3 board representation (array A):
As stated in the rules above, the squares would be numbered in alternate directions like this:
Looking at the "array A" modifier example above, a positive number is a ladder (i.e. jump forward n places), a negative number is a snake (i.e. fall back n places) and a 0 represents an empty square
We shouldn't have any snake falls in our output - since we can get to that same square with less rolls by simply landing on a sequence of normal/ladder moves. This also makes it easier, since we shouldn't have to worry about getting stuck in a path loop in our code.
The output should be as follows - note the format [current square]+[dice roll]^[jump amount]->[new pos] - the jump amount is of course only if they land on a ladder, otherwise it should be omitted. The entire path is displayed on one line and each roll is separated by a space. The number of valid paths should also be shown:
For example, 1+1^1->3 3+5->8 8+1->9 represents 3 separate rolls:
- Roll 1: start on square 1, roll a 1 (lands on square 2) and jump 1 - end at square 3
- Roll 2: start on square 3, roll a 5 - end at square 8
- Roll 3: start on square 8, roll a 1 - land on the final square 9
Note: the order of the paths is not important - e.g. you could have had 1+5^3->9 as your first line
Instead of the 3x3 board above, you should use this 6x6 board for the real challenge:
Good luck - paste your answer below:
Hints
Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts
| Release Day | Hint |
|---|---|
| 2 |