-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day15p2.java
70 lines (63 loc) · 2.2 KB
/
Day15p2.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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static Scanner sc = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static void solve() throws IOException {
String line;
int n = 100;
long ans = 0;
String[] arr = br.readLine().split(",");
ArrayList<ArrayList<String[]>> boxes = new ArrayList<>();
for(int i = 0; i < 256; i++) {
boxes.add(new ArrayList<>());
}
for(String s : arr) {
int box = 0;
for(int i = 0; i < Math.max(s.indexOf("-"), s.indexOf("=")); i++) {
char c = s.charAt(i);
box += (int)c;
box *= 17;
box = box%256;
}
if(s.indexOf("-") != -1) {
String label = s.substring(0, s.length()-1);
int idx = 0;
while(idx < boxes.get(box).size()) {
if(boxes.get(box).get(idx)[0].equals(label)) {
boxes.get(box).remove(idx);
break;
}
idx++;
}
} else {
String label = s.split("=")[0];
String focal = s.split("=")[1];
int idx = 0;
while(idx < boxes.get(box).size()) {
if(boxes.get(box).get(idx)[0].equals(label)) {
boxes.get(box).set(idx, new String[]{label, focal});
break;
}
idx++;
}
if(idx == boxes.get(box).size()) {
boxes.get(box).add(new String[]{label, focal});
}
}
}
for(int i = 0; i < boxes.size(); i++) {
for(int j = 0; j < boxes.get(i).size(); j++) {
ans += (i+1)*(j+1)*Long.parseLong(boxes.get(i).get(j)[1]);
}
}
out.println(ans);
}
public static void main (String[] args) throws IOException {
solve();
out.close();
}
}