@Korkin/

CodeQuest2018-19

Java

No description

fork
loading
Files
  • Main.java
  • CORRECT_OUTPUT.txt
  • Main.class
  • Prob19.in.txt
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.*;
import java.io.*;

class Main {

  // CodeQuest 2018 Problem 19

  // Problem packet available at: 
  // http://bit.ly/2HUHszO

  static int[][] rotors = {{1,3,6,0,5,4,8,7,9,2},{0,3,5,2,6,9,1,4,8,7},{5,9,1,7,3,8,0,2,4,6},{1,6,5,2,9,0,7,4,3,8}};
  static int[] reflection = {3,6,8,0,5,4,1,9,2,7};
  static int[][] rotorInvs = {{5,0,3,8,7,2,1,6,9,4},{6,2,7,4,8,0,9,3,5,1},{0,6,3,1,7,2,4,9,8,5},{3,0,9,1,5,4,2,7,6,8}};

  static int rotation1 = 0;
  static int rotation2 = 0;
  static int rotation3 = 0;

  public static int cipher(int in, int[] rotorNums){
    if(in+rotation1>=10)
      in-=10;
    int a = Math.abs(rotors[rotorNums[0]-1][in-rotation1]-10+rotation1);
    if(a+rotation2>=10)
      a-=10;
    int b = Math.abs(rotors[rotorNums[1]-1][a-rotation2]-10+rotation2);
    if(b+rotation3>=10)
      b-=10;
    int c = Math.abs(rotors[rotorNums[2]-1][b-rotation3]-10+rotation3);
    int e = reflection[c];
    if(e+rotation3>=10)
      e-=10;
    int g = Math.abs(rotorInvs[rotorNums[2]-1][e-rotation3]-10+rotation3);
    if(g+rotation2>=10)
      g-=10;
    int h = Math.abs(rotorInvs[rotorNums[1]-1][g-rotation2]-10+rotation2);
    if(h+rotation1>=10)
      h-=10;
    int ans = Math.abs(rotorInvs[rotorNums[0]-1][h-rotation1]-10+rotation1);
    rotation1++;
    if(rotation1==10){
      rotation1=0;
      rotation2++;
    }
    if(rotation2==10){
      rotation2=0;
      rotation3++;
    }
    if(rotation3==10){
      rotation3=0;
    }
    return ans;
  }

  public static void main(String[] args)  throws FileNotFoundException {
    Scanner scan = new Scanner(new File("Prob19.in.txt"));

    // Read # of test cases.
    int T = scan.nextInt();
    scan.nextLine();

    // Loop through all test cases.
    for(int zz = 0; zz < T; zz++) {
      int[] rotorsUsed = new int[3];
      rotorsUsed[0] = scan.nextInt();
      rotation1 = scan.nextInt();
      scan.nextLine();
      rotorsUsed[1] = scan.nextInt();
      rotation2 = scan.nextInt();
      scan.nextLine();
      rotorsUsed[2] = scan.nextInt();
      rotation3 = scan.nextInt();
      scan.nextLine();
      String base = scan.nextLine();
      String encrypted = "";
      for(int i = 0; i<base.length(); i++){
        String digit = base.substring(i,i+1);
        int encrypt = cipher(Integer.parseInt(digit), rotorsUsed);
        encrypted+=encrypt;
      }
      System.out.println(encrypted);
    }
  }
}