-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11.java
More file actions
165 lines (144 loc) · 4.75 KB
/
Day11.java
File metadata and controls
165 lines (144 loc) · 4.75 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
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Day11 {
public static void main(String[] args) {
new Day11();
}
public Day11() {
ArrayList<String> input = ReadInput.read("res/input11.txt");
partOne(input);
partTwo(input);
}
private void partOne(ArrayList<String> input) {
ArrayList<Monkey> monkeys = parseInput(input);
for(int round = 0; round < 20; round++) {
for(Monkey monkey : monkeys) {
for(int i = 0; i < monkey.items.size(); i++) {
monkey.totalInspects++;
monkey.performOperation(i);
monkey.postOperationDivision(i);
long item = monkey.items.get(i);
if(item % monkey.divisibleBy == 0) {
monkeys.get(monkey.throwTrue).items.add(item);
}
else {
monkeys.get(monkey.throwFalse).items.add(item);
}
}
monkey.items = new ArrayList<Long>();
}
}
Collections.sort(monkeys, new Comparator<Monkey>() {
public int compare(Monkey m1, Monkey m2) {
if(m1.totalInspects == m2.totalInspects)
return 0;
return m1.totalInspects < m2.totalInspects ? -1 : 1;
}
});
Collections.reverse(monkeys);
System.out.println(monkeys.get(0).totalInspects * monkeys.get(1).totalInspects);
}
private void partTwo(ArrayList<String> input) {
ArrayList<Monkey> monkeys = parseInput(input);
long GCF = 1;
for(Monkey m : monkeys)
GCF *= m.divisibleBy;
for(int round = 0; round < 10000; round++) {
for(Monkey monkey : monkeys) {
for(int i = 0; i < monkey.items.size(); i++) {
monkey.totalInspects++;
monkey.items.set(i, monkey.items.get(i) % GCF == 0 ? GCF : monkey.items.get(i) % GCF);
monkey.performOperation(i);
long item = monkey.items.get(i);
if(item % monkey.divisibleBy == 0) {
monkeys.get(monkey.throwTrue).items.add(item);
}
else {
monkeys.get(monkey.throwFalse).items.add(item);
}
}
monkey.items = new ArrayList<Long>();
}
}
Collections.sort(monkeys, new Comparator<Monkey>() {
public int compare(Monkey m1, Monkey m2) {
if(m1.totalInspects == m2.totalInspects)
return 0;
return m1.totalInspects < m2.totalInspects ? -1 : 1;
}
});
Collections.reverse(monkeys);
long res = monkeys.get(0).totalInspects * monkeys.get(1).totalInspects;
System.out.println(res);
}
private ArrayList<Monkey> parseInput(ArrayList<String> input) {
ArrayList<Monkey> res = new ArrayList<Monkey>();
for(int i = 0; i < input.size(); i+=7) {
String itemsString = input.get(i+1).trim();
String operationString = input.get(i+2).trim();
String divisibleString = input.get(i+3).trim();
String throwTrueString = input.get(i+4).trim();
String throwFalseString = input.get(i+5).trim();
ArrayList<Long> items = new ArrayList<Long>();
for(String item : itemsString.split(":")[1].split(",")) {
items.add(Long.parseLong(item.trim()));
}
Monkey.Operation operation = operationString.contains("*") ? Monkey.Operation.MUL : Monkey.Operation.ADD;
long operationValue;
boolean operationOnOld;
if(operationString.split(" ")[5].equals("old")) {
operationValue = -1;
operationOnOld = true;
}
else {
operationValue = Long.parseLong(operationString.split(" ")[5]);
operationOnOld = false;
}
long divisibleBy = Long.parseLong(divisibleString.split(" ")[3]);
int throwTrue = Integer.parseInt(throwTrueString.split(" ")[5]);
int throwFalse = Integer.parseInt(throwFalseString.split(" ")[5]);
res.add(new Monkey(items, operation, operationValue, operationOnOld, divisibleBy, throwTrue, throwFalse));
}
return res;
}
private class Monkey {
public long totalInspects;
public long opValue, divisibleBy;
public int throwTrue, throwFalse;
private boolean operationOnOld;
public ArrayList<Long> items;
private Operation operation;
public enum Operation {
MUL,
ADD
};
public Monkey(ArrayList<Long> items, Operation op, long opValue, boolean operationOnOld, long divisibleBy, int throwTrue, int throwFalse) {
this.items = items;
this.operation = op;
this.opValue = opValue;
this.operationOnOld = operationOnOld;
this.divisibleBy = divisibleBy;
this.throwTrue = throwTrue;
this.throwFalse = throwFalse;
this.totalInspects = 0;
}
public void performOperation(int index) {
if(this.operation == operation.MUL) {
if(this.operationOnOld)
this.items.set(index, this.items.get(index) * this.items.get(index));
else
this.items.set(index, this.items.get(index) * this.opValue);
}
else {
if(this.operationOnOld)
this.items.set(index, this.items.get(index) + this.items.get(index));
else
this.items.set(index, this.items.get(index) + this.opValue);
}
}
public void postOperationDivision(int index) {
this.items.set(index, this.items.get(index) / 3);
}
}
}