-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandyHaversacksSevenTwo.java
More file actions
66 lines (53 loc) · 1.93 KB
/
HandyHaversacksSevenTwo.java
File metadata and controls
66 lines (53 loc) · 1.93 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
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 HandyHaversacksSevenTwo {
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 - 1] + " " + words[idx] + words[idx + 1];
containColors.add(bagColor);
}
}
map.put(mainColor, containColors);
input = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
count += traverse(map, map.get(desiredColor));
System.out.println(count);
}
private static int traverse(Map<String, List<String>> map, List<String> values) {
if (values.isEmpty()) {
return 0;
}
int bagTotal = 0;
for (String value : values) {
int quantity = Integer.parseInt(value.substring(0, 1));
int sum = 1 + traverse(map, map.get(value.substring(2)));
bagTotal += quantity * sum;
}
return bagTotal;
}
}