-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11b.java
More file actions
211 lines (184 loc) · 4.82 KB
/
Day11b.java
File metadata and controls
211 lines (184 loc) · 4.82 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.util.function.Function;
// will find the lowest common demoninator of all test values
// and modulo worry level by lcd to manage it
public class Day11b {
public static String myRead(Scanner scanner) {
String line;
try {
line = scanner.nextLine().trim();
} catch (Exception e) {
line = null;
}
return line;
}
public static Scanner myScanner(File text) {
Scanner scanner;
try {
scanner = new Scanner(text);
} catch (Exception e) {
System.out.println("No Scanner");
return null;
}
return scanner;
}
public static long gcd(long a, long b) {
while (b > 0) {
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
private static long lcm(long a, long b) {
return a * (b / gcd(a, b));
}
public static void main(String[] args) {
File text = new File("/Users/axelteo/Desktop/AdventofCode/Day11input.txt");
Scanner scanner = myScanner(text);
if (scanner == null) {
return;
}
String line;
ArrayList<Long> testValues = new ArrayList<>();
ArrayList<Monkey> monkeys = new ArrayList<>();
while (myRead(scanner) != null) {
Monkey curr = new Monkey();
curr.setItems(myRead(scanner));
curr.setOp(myRead(scanner));
curr.setTest(line = myRead(scanner));
testValues.add(Long.valueOf(line.split(" ", 0)[3]));
curr.setPosNeg(myRead(scanner), myRead(scanner));
myRead(scanner);
monkeys.add(curr);
}
long testsLCM = 1;
for (long i: testValues) {
testsLCM = lcm(testsLCM, i);
}
System.out.println("LCM: " + testsLCM);
for (int i = 0; i < 10000; ++i) {
for (Monkey m: monkeys) {
m.inspectItems(testsLCM);
m.throwItems(monkeys);
}
}
for (Monkey tmp: monkeys) {
System.out.println("Monkey i:" + tmp.getInspections());
}
}
}
class Monkey {
ArrayList<Item> items;
Function<Long, Long> op;
Function<Long, Boolean> test;
int pos;
int neg;
int inspections;
public Monkey() {
this.items = null;
this.op = null;
this.test = null;
this.pos = -1;
this.neg = -1;
this.inspections = 0;
}
public int getInspections() {
return this.inspections;
}
public void setItems(String itemList) {
ArrayList<Item> itemArr = new ArrayList<>();
String[] arr = itemList.split(" ", 0);
int arrLength = arr.length;
for (int i = 2; i < arrLength; ++i) {
String tmp = "";
for (int j = 0; j < arr[i].length(); ++j) {
if (Character.isDigit(arr[i].charAt(j))) {
tmp += arr[i].charAt(j);
}
}
itemArr.add(new Item(Long.valueOf(tmp)));
}
this.items = itemArr;
}
public void setOp(String op) {
String[] ops = op.split(" ", 0);
if (ops[4].equals("+")) {
this.op = x -> x + Integer.valueOf(ops[5]);
} else if (ops[4].equals("*")) {
if (ops[5].equals("old")) {
this.op = x -> x * x;
} else {
this.op = x -> x * Integer.valueOf(ops[5]);
}
}
}
public void setTest(String test) {
String[] tests = test.split(" ", 0);
this.test = x -> x % Integer.valueOf(tests[3]) == 0;
}
public void setPosNeg(String pos, String neg) {
this.pos = Integer.valueOf(pos.split(" ", 0)[5]);
this.neg = Integer.valueOf(neg.split(" ", 0)[5]);
}
public void inspectItems(long testsLCM) {
for (Item i: this.items) {
i.inspect(this.op, testsLCM);
this.inspections += 1;
}
}
public void throwItems(ArrayList<Monkey> monkeys) {
ArrayList<Item> toPos = new ArrayList<>();
ArrayList<Item> toNeg = new ArrayList<>();
for (Item i: this.items) {
if (!i.isInspected()) {
continue;
}
i.thrown();
if (i.check(this.test)) {
toPos.add(i);
} else {
toNeg.add(i);
}
}
this.items.removeAll(toPos);
this.items.removeAll(toNeg);
monkeys.get(this.pos).catchItems(toPos);
monkeys.get(this.neg).catchItems(toNeg);
}
public void catchItems(ArrayList<Item> i) {
this.items.addAll(i);
}
@Override
public String toString() {
String tmp = "";
for (Item i: this.items) {
tmp += i.worryLvl + " ";
}
return "items: " + tmp + " pos: " + this.pos + " neg: " + this.neg;
}
}
class Item {
long worryLvl;
boolean inspected;
public Item(long worryLvl) {
this.worryLvl = worryLvl;
this.inspected = false;
}
public boolean isInspected() {
return this.inspected;
}
public Item inspect(Function<Long, Long> fn, long testsLCM) {
this.worryLvl = fn.apply(this.worryLvl) % testsLCM;
this.inspected = true;
return this;
}
public boolean check(Function<Long, Boolean> fn) {
return fn.apply(this.worryLvl);
}
public void thrown() {
this.inspected = false;
}
}