-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11a.java
149 lines (116 loc) · 4.57 KB
/
Day11a.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
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
package aoc22;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class Day11a {
private static String filename = "./src/aoc22/input11.txt";
// private static String filename = "./src/aoc22/example11.txt";
private final List<Monkey> monkeys = new ArrayList<>();
public Day11a(List<String> input) {
parse(input);
for (int i = 0; i < 20; i++) {
performRound();
}
monkeys.stream().forEach(System.out::println);
System.out.println("Monkey business detected: " + determineMonkeyBusiness());
}
private int determineMonkeyBusiness() {
monkeys.sort((one, two) -> two.inspections - one.inspections);
return monkeys.get(0).inspections * monkeys.get(1).inspections;
}
private void performRound() {
for (Monkey monkey : monkeys) {
for (Item item : monkey.items) {
item.worryLevel = monkey.operation.perform(item.worryLevel);
item.worryLevel = monkeyBored(item.worryLevel);
if (monkey.divisible(item.worryLevel)) {
throwTo(monkey.firstChoice, item.worryLevel);
} else {
throwTo(monkey.secondChoice, item.worryLevel);
}
monkey.inspections++;
}
monkey.items.clear();
}
}
private void throwTo(int choice, long worryLevel) {
monkeys.get(choice).items.add(new Item(worryLevel));
}
private long monkeyBored(long worryLevel) {
return worryLevel / 3;
}
private void parse(List<String> input) {
for (int i = 0; i < input.size(); i += 7) {
Monkey monkey = new Monkey(parseDivideBy(input.get(i + 3)), parseOperation(input.get(i + 2)),
parseChoice(input.get(i + 4)), parseChoice(input.get(i + 5)));
for (String item : parseItems(input.get(i + 1))) {
monkey.items.add(new Item(Integer.parseInt(item.trim())));
}
monkeys.add(monkey);
}
}
private int parseChoice(String choiceLine) {
return Integer.parseInt(choiceLine.charAt(choiceLine.length() - 1) + "");
}
private Operation parseOperation(String operationLine) {
String[] operations = operationLine.substring(operationLine.indexOf('=') + 2).split(" ");
return switch (operations[1]) {
case "*" -> worryLevel -> worryLevel * toValue(operations[2], worryLevel);
case "+" -> worryLevel -> worryLevel + toValue(operations[2], worryLevel);
default -> throw new IllegalArgumentException("Unexpected value: " + operations[1]);
};
}
private long toValue(String valueOrOld, long worryLevel) {
return switch (valueOrOld) {
case "old" -> worryLevel;
default -> Integer.parseInt(valueOrOld);
};
}
private int parseDivideBy(String divideByLine) {
return Integer.parseInt(divideByLine.substring(divideByLine.indexOf("by ") + 3));
}
private String[] parseItems(String itemsLine) {
return itemsLine.substring(itemsLine.indexOf(':') + 2).split(",");
}
public static void main(String[] args) throws IOException {
new Day11a(Files.readAllLines(Path.of(filename)));
}
private class Monkey {
private final List<Item> items = new ArrayList<>();
private final int divideBy;
private final Operation operation;
private final int firstChoice;
private final int secondChoice;
private int inspections;
public Monkey(int divideBy, Operation operation, int firstChoice, int secondChoice) {
this.divideBy = divideBy;
this.operation = operation;
this.firstChoice = firstChoice;
this.secondChoice = secondChoice;
}
public boolean divisible(long worryLevel) {
return worryLevel % divideBy == 0;
}
@Override
public String toString() {
return "Monkey [items=" + items + ", divideBy=" + divideBy + ", firstChoice=" + firstChoice
+ ", secondChoice=" + secondChoice + ", inspections=" + inspections + "]";
}
}
private class Item {
private long worryLevel;
public Item(long worryLevel) {
this.worryLevel = worryLevel;
}
@Override
public String toString() {
return worryLevel + "";
}
}
@FunctionalInterface
private interface Operation {
long perform(long worryLevel);
}
}