Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Last active May 23, 2023 02:43
Show Gist options
  • Save hamza-cskn/67c241c099d26e933a7662ba906322ce to your computer and use it in GitHub Desktop.
Save hamza-cskn/67c241c099d26e933a7662ba906322ce to your computer and use it in GitHub Desktop.
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;
}
@hamza-cskn
Copy link
Author

hamza-cskn commented May 26, 2022

image
output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment