// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class UniqueTickets {
    private final static int[] w = { 29, 23, 19, 17, 13, 11, 7, 5, 3, 2 };
    private static int chksum(int v) {
        int res = 0;
        int tmp = v;
        for(int i = 0; i < 10; i++) {
            res += w[i]*(tmp % 10);
            tmp /= 10;
        }
        return 99 - res % 100;
    }
    private static String toFixString(int v, int w) {
        String tmp = Integer.toString(v);
        return "0000000000".substring(0, w - tmp.length()) + tmp;
    }
    public static String seqToUid(int seq) {
        String res = "";
        res += ((3 * seq + 1) % 7);
        res += toFixString((1234 + seq % 10000) % 10000, 4);
        res += toFixString(chksum(seq), 2);
        res += toFixString((1234 + seq / 10000) % 10000, 4);
        res += (seq % 7);
        return res;
    }
    public static int uidToSeq(String uid) {
        int part1 = Integer.parseInt(uid.substring(0, 1));
        int part2 = Integer.parseInt(uid.substring(1, 5));
        int part3 = Integer.parseInt(uid.substring(5, 7));
        int part4 = Integer.parseInt(uid.substring(7, 11));
        int part5 = Integer.parseInt(uid.substring(11, 12));
        int res = ((8766 + part4) % 10000) * 10000 + ((8766 + part2) % 10000);
        if(part1 != (3 * res + 1) % 7) throw new IllegalArgumentException("Invalid ticket");
        if(part3 != chksum(res)) throw new IllegalArgumentException("Invalid ticket");
        if(part5 != res % 7) throw new IllegalArgumentException("Invalid ticket");
        return res;
    }
    public static void main(String[] args) {
        for(int i = 0; i < 100; i++) {
            System.out.println(uidToSeq(seqToUid(i)) + " " + seqToUid(i));
        }
    }
}
