Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. public class HelloWorld{
  2.  
  3. public static void main(String []args){
  4. StringInput jrlcodein = new StringInput("GVQT4GSS2UXEE");
  5. int jrlserver = (int)readVariableJourneyCodeValue(jrlcodein);
  6. long jrlid = getJourneyCodeValue(jrlcodein.readString());
  7. System.out.println(jrlid);
  8. }
  9. private final static int[] BASE32_INT;
  10. public final static char[] BASE32 = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".toCharArray();
  11. static {
  12. BASE32_INT = new int['Z' + 1];
  13. for(int i=0; i<BASE32_INT.length; i++) {
  14. BASE32_INT[i] = -1;
  15. }
  16. for(int i=0; i<BASE32.length; i++) {
  17. BASE32_INT[BASE32[i]] = i;
  18. }
  19. }
  20.  
  21. protected static int getBase32Value(char c) {
  22. int value;
  23. try {
  24. value = BASE32_INT[c];
  25. } catch (ArrayIndexOutOfBoundsException ex) {
  26. value = -1;
  27. }
  28. if (value < 0)
  29. throw new IllegalArgumentException("Invalid base 32 character: " + c);
  30. return value;
  31. }
  32. public static long readVariableJourneyCodeValue(StringInput str) {
  33. long id = 0;
  34. for (int i=0; str.remaining()>0; i+=4) {
  35. int v = getBase32Value((char)str.readChar());
  36. id |= (v & 0xf) << i;
  37. if((v & 0x10) == 0)
  38. break;
  39. }
  40. return id;
  41. }
  42. public static long getJourneyCodeValue(String code) {
  43. long id = 0;
  44. for (int i = 0; i < code.length(); i++) {
  45. id <<= 5;
  46. id |= getBase32Value(code.charAt(i));
  47. }
  48. return id;
  49. }
  50.  
  51. }
  52.  
  53. class StringInput {
  54.  
  55. private String str;
  56. private int next;
  57.  
  58. public StringInput(String str) {
  59. this.str = str;
  60. }
  61.  
  62. public synchronized int readChar() {
  63. if (next >= str.length())
  64. return -1;
  65. return str.charAt(next++);
  66. }
  67.  
  68. public synchronized int read(char[] cbuf, int off, int len) {
  69. if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) {
  70. throw new IndexOutOfBoundsException();
  71. } else if (len == 0) {
  72. return 0;
  73. }
  74. if (next >= str.length())
  75. return -1;
  76. int n = Math.min(str.length() - next, len);
  77. str.getChars(next, next + n, cbuf, off);
  78. next += n;
  79. return n;
  80. }
  81.  
  82. public synchronized String readString() {
  83. int n = next;
  84. next += remaining();
  85. if(n == 0)
  86. return str;
  87. else
  88. return str.substring(n);
  89. }
  90.  
  91. public int remaining() {
  92. return str.length() - next;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement