-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8p2.java
61 lines (51 loc) · 1.6 KB
/
Day8p2.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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static Scanner sc = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b,a%b);
}
public static long lcm(long a, long b) {
return (a/gcd(a, b)) * b;
}
public static void solve() throws IOException {
String line;
int n = 3;
int ans = 0;
int idx = 0;
String dir = br.readLine();
br.readLine();
HashMap<String, String[]> map = new HashMap<>();
while ((line = br.readLine()) != null) {
String[] temp = line.split(" ");
map.put(temp[0], new String[]{temp[2].substring(1, 4), temp[3].substring(0, 3)});
idx++;
}
ArrayList<Long> arr = new ArrayList<>();
for(String curr : map.keySet()) {
ans = 0;
if(curr.charAt(2)=='A') {
while(curr.charAt(2) != 'Z') {
if(dir.charAt(ans%dir.length())=='L') curr = map.get(curr)[0];
else curr = map.get(curr)[1];
ans++;
}
arr.add((long)ans);
}
}
long y = arr.get(0);
for(long x : arr) {
y = lcm(y, x);
}
out.println(y);
}
public static void main (String[] args) throws IOException {
solve();
out.close();
}
}