Last active
May 23, 2023 02:43
-
-
Save hamza-cskn/67c241c099d26e933a7662ba906322ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int getSnakeSlot(int slot) { | |
final int horizontalLength = 2; | |
final int verticalLength = 5; | |
final int patternLength = 2 * (horizontalLength + verticalLength); | |
final int patternNo = slot / (patternLength + 1); // 10 -> 1, 8/10 -> 1, 12/10 -> 2 | |
int slotNoInPattern = slot % (patternLength + 1); // 8/10 -> 8, 12/10 -> 2 | |
final boolean isInReversedPattern = slotNoInPattern > patternLength / 2; | |
int roadTakenHorizontal, roadTakenVertical; | |
if (isInReversedPattern) { | |
slotNoInPattern -= patternLength / 2; // 6 -> 1 | |
roadTakenHorizontal = Math.min(horizontalLength, slotNoInPattern) + horizontalLength; | |
roadTakenVertical = verticalLength - Math.max(0, slotNoInPattern - horizontalLength); | |
} else { | |
roadTakenHorizontal = Math.min(horizontalLength, slotNoInPattern); | |
roadTakenVertical = Math.max(0, slotNoInPattern - horizontalLength); | |
} | |
return roadTakenHorizontal + patternNo * horizontalLength * 2 + roadTakenVertical * 9; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output