-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay11.java
65 lines (55 loc) · 2.03 KB
/
Day11.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
package com.sbaars.adventofcode.year22.days;
import com.sbaars.adventofcode.common.map.LongCountMap;
import com.sbaars.adventofcode.year22.Day2022;
import java.util.List;
import static com.sbaars.adventofcode.util.DataMapper.readString;
public class Day11 extends Day2022 {
public Day11() {
super(11);
}
public static void main(String[] args) {
new Day11().printParts();
}
public record Monkey(int n, List<Long> items, char op, String add, long divisible, int ifTrue, int ifFalse) {
}
@Override
public Object part1() {
return solution(20, true);
}
private long solution(int cycles, boolean decreasingWorry) {
List<Monkey> monkeys = dayStream("\n\n").map(String::trim).map(s -> readString(s, """
Monkey %i:
Starting items: %ln
Operation: new = old %c %s
Test: divisible by %n
If true: throw to monkey %i
If false: throw to monkey %i""", Monkey.class)).toList();
LongCountMap<Integer> times = new LongCountMap<>();
long gcd = monkeys.stream().mapToLong(e -> e.divisible).reduce((a, b) -> a * b).getAsLong();
for (int i = 0; i < cycles; i++) {
for (Monkey m : monkeys) {
while (!m.items.isEmpty()) {
long item = m.items.remove(0);
long worryLevel = applyOperator(item, m.op, m.add) / (decreasingWorry ? 3 : 1);
boolean test = worryLevel % m.divisible == 0;
monkeys.get(test ? m.ifTrue : m.ifFalse).items.add(worryLevel % gcd);
times.increment(m.n);
}
}
}
long[] sorted = times.values().stream().mapToLong(e -> e).sorted().toArray();
return sorted[sorted.length - 1] * sorted[sorted.length - 2];
}
private long applyOperator(long item, char op, String add) {
long itemValue = add.equals("old") ? item : Long.parseLong(add);
return switch (op) {
case '*' -> item * itemValue;
case '+' -> item + itemValue;
default -> throw new IllegalStateException();
};
}
@Override
public Object part2() {
return solution(10000, false);
}
}