-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask13.java
174 lines (159 loc) · 5.56 KB
/
Task13.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.Scanner;
import java.util.ArrayList;
public class Task13
{
public static void main(String[] args)
{
Scanner read = new Scanner(System.in);
ArrayList<String> suspects = new ArrayList<>();
ArrayList<String> weapons = new ArrayList<>();
ArrayList<String> locations = new ArrayList<>();
String suspect = read.nextLine();
suspect = suspect.substring(15,suspect.length()-2);
String weapon = read.nextLine();
weapon = weapon.substring(14,weapon.length()-2);
String location = read.nextLine();
location = location.substring(16,location.length()-2);
for(String i : suspect.split("\\] \\["))
{
suspects.add(i);
}
for(String i : weapon.split("\\] \\["))
{
weapons.add(i);
}
for(String i : location.split("\\] \\["))
{
locations.add(i);
}
class Rule{
String rq1,rq2,re;
public Rule(String condition1, String condition2, String result)
{
rq1 = condition1;
rq2 = condition2;
re = result;
}
public boolean eval()
{
boolean rq1s = (suspects.contains(rq1));
boolean rq1w = (weapons.contains(rq1));
boolean rq1l = (locations.contains(rq1));
boolean rq2s = (suspects.contains(rq2));
boolean rq2w = (weapons.contains(rq2));
boolean rq2l = (locations.contains(rq2));
boolean rq1e = (rq1s || rq1w || rq1l);
boolean rq1f = (((suspects.size()==1) && rq1s) || ((weapons.size()==1) && rq1w) || ((locations.size()==1) && rq1l));
boolean rq2e = (rq2s || rq2w || rq2l);
boolean rq2f = (((suspects.size()==1) && rq2s) || ((weapons.size()==1) && rq2w) || ((locations.size()==1) && rq2l));
boolean pos = false;
boolean result = false;
if((!(rq1e)) && (!(rq2e)))
{
pos = true;
}
if(rq1f && rq2f)
{
result = true;
pos = true;
}
String clue = re;
if(pos)
{
if(result)
{
if(suspects.contains(clue))
{
suspects.clear();
suspects.add(clue);
}
else
if(weapons.contains(clue))
{
weapons.clear();
weapons.add(clue);
}
else
if(locations.contains(clue))
{
locations.clear();
locations.add(clue);
}
}
else
{
if(suspects.contains(clue)) suspects.remove(clue);
else if(weapons.contains(clue)) weapons.remove(clue);
else if(locations.contains(clue)) locations.remove(clue);
}
}
if(suspects.size()==1&&weapons.size()==1&&locations.size()==1) return true;
else return false;
}
}
ArrayList<Rule> rules = new ArrayList<>();
String in = read.nextLine();
while(!in.contains("[yes]")&&!in.contains("[no]"))
{
in = in.substring(2,in.length() - 2);
rules.add(new Rule(in.substring(0,in.indexOf("], [")),in.substring(in.indexOf("], [")+4,in.indexOf("] => [")) , in.substring(in.indexOf("] => [")+6)));
in = read.nextLine();
}
int cluenum = 1;
while(true)
{
in = in.substring(2, in.length()-2);
boolean pos = false;
String clue = "";
if(in.startsWith("no]"))
{
pos = false;
clue = in.substring(5);
}
if(in.startsWith("yes]"))
{
pos = true;
clue = in.substring(6);
}
if(pos)
{
if(suspects.contains(clue))
{
suspects.clear();
suspects.add(clue);
}
else
if(weapons.contains(clue))
{
weapons.clear();
weapons.add(clue);
}
else
if(locations.contains(clue))
{
locations.clear();
locations.add(clue);
}
}
else
{
if(suspects.contains(clue)) suspects.remove(clue);
else if(weapons.contains(clue)) weapons.remove(clue);
else if(locations.contains(clue)) locations.remove(clue);
}
for(int iii=0;iii<3;iii++)
{
for(Rule r : rules)
{
if(r.eval())
{
System.out.println("{["+cluenum+"] ["+suspects.get(0)+"] ["+weapons.get(0)+"] ["+locations.get(0)+"]}");
return;
}
}
}
cluenum++;
in = read.nextLine();
}
}
}