-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.java
75 lines (73 loc) · 1.37 KB
/
solution.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
import java.util.Collections;
import java.util.HashMap;
public class Turing {
private static HashMap<Integer, Boolean> tape = new HashMap<>();
private static String state = "a";
private static int pos = 0;
public static void main(String[] args) {
for(int i = 0; i < 12172063; i++) {
if(tape.get(pos) == null) tape.put(pos, false);
boolean value = tape.get(pos);
switch(state) {
case "a":
if(value) {
tape.put(pos, false);
pos--;
state = "c";
} else {
tape.put(pos, true);
pos++;
state = "b";
}
break;
case "b":
if(value) state = "d";
else state = "a";
tape.put(pos, true);
pos--;
break;
case "c":
if(value) {
tape.put(pos, false);
state = "c";
} else {
tape.put(pos, true);
state = "d";
}
pos++;
break;
case "d":
tape.put(pos, false);
if(value) {
pos++;
state = "e";
} else {
pos--;
state = "b";
}
break;
case "e":
tape.put(pos, true);
if(value) {
pos--;
state = "f";
} else {
pos++;
state = "c";
}
break;
case "f":
tape.put(pos, true);
if(value) {
pos++;
state = "a";
} else {
pos--;
state = "e";
}
break;
}
}
System.out.print(Collections.frequency((tape.values()), true));
}
}