-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11Monkey.java
78 lines (66 loc) · 1.94 KB
/
Day11Monkey.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
71
72
73
74
75
76
77
78
import java.util.LinkedList;
public class Day11Monkey{
private LinkedList<Long> items;
private int operationAX2;
private int operationBX;
private int operationC;
private int testDivisor;
private int recipientPass;
private int recipientFail;
private boolean extraWorry;
private long inspectCount;
public Day11Monkey(int ax2, int bx, int c, int test, int pass, int fail, boolean worry){
items = new LinkedList<Long>();
this.operationAX2 = ax2;
this.operationBX = bx;
this.operationC = c;
this.testDivisor = test;
this.recipientPass = pass;
this.recipientFail = fail;
this.extraWorry = worry;
this.inspectCount = 0;
}
public int getTestDivisor(){
return this.testDivisor;
}
public void give(long i){
items.add(i);
}
public void inspectAll(Day11Monkey[] monkeys){
while(items.size() > 0){
inspect(monkeys);
}
}
public void inspect(Day11Monkey[] monkeys){
long lcm = 1;
for(int k=0;k<monkeys.length;k++){
if(monkeys[k] != null){ lcm *= monkeys[k].getTestDivisor(); }
}
long item = items.removeFirst();
long newItem = ((operationAX2 * item * item) + (operationBX * item) + (operationC));
newItem = (extraWorry ? (Long.remainderUnsigned(newItem,lcm)) : newItem / 3);
if(newItem % (long)testDivisor == 0){
monkeys[this.recipientPass].give(newItem);
}else{
monkeys[this.recipientFail].give(newItem);
}
inspectCount++;
}
public String describeBehavior(){
String z = "op: " + this.operationAX2 + "x^2 + " + this.operationBX + "x + " + this.operationC + "; ";
z += "test: " + this.testDivisor + "; ";
z += "pass: " + this.recipientPass + "; ";
z += "fail: " + this.recipientFail + "; ";
return z;
}
public String describeInventory(){
String z = "has: ";
for(long item : items){
z += item + " ";
}
return z;
}
public long getInspectCount(){
return this.inspectCount;
}
}