Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HelloWorld{
- public static void main(String []args){
- StringInput jrlcodein = new StringInput("GVQT4GSS2UXEE");
- int jrlserver = (int)readVariableJourneyCodeValue(jrlcodein);
- long jrlid = getJourneyCodeValue(jrlcodein.readString());
- System.out.println(jrlid);
- }
- private final static int[] BASE32_INT;
- public final static char[] BASE32 = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".toCharArray();
- static {
- BASE32_INT = new int['Z' + 1];
- for(int i=0; i<BASE32_INT.length; i++) {
- BASE32_INT[i] = -1;
- }
- for(int i=0; i<BASE32.length; i++) {
- BASE32_INT[BASE32[i]] = i;
- }
- }
- protected static int getBase32Value(char c) {
- int value;
- try {
- value = BASE32_INT[c];
- } catch (ArrayIndexOutOfBoundsException ex) {
- value = -1;
- }
- if (value < 0)
- throw new IllegalArgumentException("Invalid base 32 character: " + c);
- return value;
- }
- public static long readVariableJourneyCodeValue(StringInput str) {
- long id = 0;
- for (int i=0; str.remaining()>0; i+=4) {
- int v = getBase32Value((char)str.readChar());
- id |= (v & 0xf) << i;
- if((v & 0x10) == 0)
- break;
- }
- return id;
- }
- public static long getJourneyCodeValue(String code) {
- long id = 0;
- for (int i = 0; i < code.length(); i++) {
- id <<= 5;
- id |= getBase32Value(code.charAt(i));
- }
- return id;
- }
- }
- class StringInput {
- private String str;
- private int next;
- public StringInput(String str) {
- this.str = str;
- }
- public synchronized int readChar() {
- if (next >= str.length())
- return -1;
- return str.charAt(next++);
- }
- public synchronized int read(char[] cbuf, int off, int len) {
- if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return 0;
- }
- if (next >= str.length())
- return -1;
- int n = Math.min(str.length() - next, len);
- str.getChars(next, next + n, cbuf, off);
- next += n;
- return n;
- }
- public synchronized String readString() {
- int n = next;
- next += remaining();
- if(n == 0)
- return str;
- else
- return str.substring(n);
- }
- public int remaining() {
- return str.length() - next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement