Repository navigation
Expand file tree
/
Copy pathRegistrars_7.java
More file actions
127 lines (111 loc) · 5.02 KB
/
Copy pathRegistrars_7.java
File metadata and controls
127 lines (111 loc) · 5.02 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
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
package adventofcode;
import java.util.*;
/**
* Why Did you create this class? what does it do?
*/
@SuppressWarnings("Duplicates") public class Registrars_7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> list = new ArrayList<>();
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
if (s.equals("1")) {
break;
}
list.add(s);
}
System.out.println(solve2(list));
}
private static int solve(List<String> list) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String[] a = s.split(" ");
if (!map.containsKey(a[0]))
map.put(a[0], 0);
if (!map.containsKey(a[4]))
map.put(a[4], 0);
if (a[5].equals(">") && map.get(a[4]) > Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("<") && map.get(a[4]) < Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals(">=") && map.get(a[4]) >= Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("<=") && map.get(a[4]) <= Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("==") && map.get(a[4]) == Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("!=") && map.get(a[4]) != Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
}
}
int max = 0;
for (String s1 : map.keySet()) {
max = Math.max(max, map.get(s1));
}
return max;
}
private static int solve2(List<String> list) {
Map<String, Integer> map = new HashMap<>();
int max = 0;
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String[] a = s.split(" ");
if (!map.containsKey(a[0]))
map.put(a[0], 0);
if (!map.containsKey(a[4]))
map.put(a[4], 0);
if (a[5].equals(">") && map.get(a[4]) > Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("<") && map.get(a[4]) < Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals(">=") && map.get(a[4]) >= Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("<=") && map.get(a[4]) <= Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("==") && map.get(a[4]) == Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
} else if (a[5].equals("!=") && map.get(a[4]) != Integer.parseInt(a[6])) {
if (a[1].equals("inc"))
map.put(a[0], map.get(a[0]) + Integer.parseInt(a[2]));
else
map.put(a[0], map.get(a[0]) - Integer.parseInt(a[2]));
}
max = Math.max(max, map.get(a[0]));
}
return max;
}
}