-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandyHaversacksSevenOne.java
More file actions
75 lines (63 loc) · 2.21 KB
/
HandyHaversacksSevenOne.java
File metadata and controls
75 lines (63 loc) · 2.21 KB
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.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HandyHaversacksSevenOne {
private static String desiredColor = "shinygold";
private static int count = 0;
public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("handyhaversacks.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Map<String, List<String>> map = new HashMap<>();
try {
String input = reader.readLine();
while (input != null) {
String[] words = input.split(" ");
String mainColor = words[0] + words[1];
List<String> containColors = new ArrayList<>();
if (!(words[4].equals("no"))) {
for (int idx = 5; idx <= words.length; idx += 4) {
String bagColor = words[idx] + words[idx + 1];
containColors.add(bagColor);
}
}
map.put(mainColor, containColors);
input = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
traverse(map, null, desiredColor);
System.out.println(count);
}
private static boolean traverse(Map<String, List<String>> map, List<String> values, String term) {
if (values == null) {
for (String key : map.keySet()) {
values = map.get(key);
count = traverse(map, values, term) ? count + 1 : count;
}
} else {
boolean success = false;
for (String value : values) {
if (value.equals(term)) {
return true;
}
success = traverse(map, map.get(value), term);
if (success) {
return true;
} else {
continue;
}
}
}
return false;
}
}