Skip to content

Commit 8e5c8b3

Browse files
committed
Improve year 2022 day11
1 parent 82fb6ff commit 8e5c8b3

File tree

1 file changed

+40
-33
lines changed
  • year2022/src/main/java/dev/linl33/adventofcode/year2022

1 file changed

+40
-33
lines changed

year2022/src/main/java/dev/linl33/adventofcode/year2022/Day11.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package dev.linl33.adventofcode.year2022;
22

3+
import jdk.incubator.vector.FloatVector;
4+
import jdk.incubator.vector.VectorOperators;
5+
import jdk.incubator.vector.VectorSpecies;
36
import org.jetbrains.annotations.NotNull;
47

58
import java.io.BufferedReader;
69

710
public class Day11 extends AdventSolution2022<Long, Long> {
11+
private static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
12+
813
private static final int MAX_ITEMS = 50;
914
private static final int PART_1_ROUNDS = 20;
1015
private static final int PART_2_ROUNDS = 10_000;
@@ -89,14 +94,19 @@ public Long part2(@NotNull BufferedReader reader) throws Exception {
8994
var input = reader.lines().toArray(String[]::new);
9095
var monkeyCount = (input.length + 1) / 7;
9196

92-
var items = new int[MAX_ITEMS];
97+
if (monkeyCount > SPECIES.length()) {
98+
// shouldn't happen, don't want to handle it
99+
throw new IllegalArgumentException();
100+
}
101+
102+
var items = new float[MAX_ITEMS];
93103
var itemsPosition = new int[MAX_ITEMS];
94104
var itemsPointer = 0;
95105

96-
var offsets = new int[monkeyCount * monkeyCount];
97-
var multipliers = new int[monkeyCount * monkeyCount];
106+
var offsets = new float[monkeyCount];
107+
var multipliers = new float[monkeyCount];
98108

99-
var moduli = new int[monkeyCount];
109+
var moduli = new float[monkeyCount];
100110
var trueValues = new int[monkeyCount];
101111
var falseValues = new int[monkeyCount];
102112

@@ -121,49 +131,46 @@ public Long part2(@NotNull BufferedReader reader) throws Exception {
121131
var op = input[i + 2].charAt(23);
122132
if (op == '+') {
123133
var opVal = Integer.parseInt(input[i + 2], 25, input[i + 2].length(), 10);
124-
offsets[m * monkeyCount] = opVal;
125-
multipliers[m * monkeyCount] = 1;
134+
offsets[m] = opVal;
135+
multipliers[m] = 1;
126136
} else {
127137
if (input[i + 2].charAt(25) == 'o') {
128138
// handle new = old * old
129139
squareMonkey = m;
130140
} else {
131141
var opVal = Integer.parseInt(input[i + 2], 25, input[i + 2].length(), 10);
132-
multipliers[m * monkeyCount] = opVal;
142+
multipliers[m] = opVal;
133143
}
134144
}
135145
}
136146

137-
for (int i = 0; i < offsets.length; i++) {
138-
var monkeyIndex = (i / monkeyCount) * monkeyCount;
139-
var modulus = moduli[i % monkeyCount];
140-
offsets[i] = offsets[monkeyIndex] % modulus;
141-
multipliers[i] = multipliers[monkeyIndex] % modulus;
142-
}
143-
144147
var inspectionCounts = new int[monkeyCount];
145-
var worries = new int[monkeyCount];
146148

147-
for (int i = 0; i < itemsPointer; i++) {
148-
var item = items[i];
149-
for (int m = 0; m < monkeyCount; m++) {
150-
worries[m] = item % moduli[m];
151-
}
149+
var mask = SPECIES.indexInRange(0, monkeyCount);
150+
var modV = FloatVector.fromArray(SPECIES, moduli, 0, mask);
152151

152+
for (int i = 0; i < itemsPointer; i++) {
153+
var worriesVector = FloatVector.broadcast(SPECIES, items[i]);
153154
var pos = itemsPosition[i];
155+
154156
for (int round = 0; round < PART_2_ROUNDS; round++) {
155157
inspectionCounts[pos]++;
156-
if (pos == squareMonkey) {
157-
for (int j = 0, mulIndex = squareMonkey * monkeyCount; j < monkeyCount; j++, mulIndex++) {
158-
multipliers[mulIndex] = worries[j] % moduli[j];
159-
}
160-
}
161-
162-
for (int m = 0, opIndex = pos * monkeyCount; m < monkeyCount; m++, opIndex++) {
163-
worries[m] = (worries[m] * multipliers[opIndex] + offsets[opIndex]) % moduli[m];
164-
}
165158

166-
var posNext = worries[pos] == 0 ? trueValues[pos] : falseValues[pos];
159+
worriesVector = worriesVector
160+
.fma(
161+
pos == squareMonkey ? worriesVector : FloatVector.broadcast(SPECIES, multipliers[pos]),
162+
FloatVector.broadcast(SPECIES, offsets[pos])
163+
);
164+
165+
// compute mod with using a - (a/d) * d
166+
var tmp = worriesVector
167+
.div(modV)
168+
.convert(VectorOperators.F2I, 0)
169+
.convert(VectorOperators.I2F, 0)
170+
.mul(modV);
171+
worriesVector = worriesVector.sub(tmp);
172+
173+
var posNext = worriesVector.test(VectorOperators.IS_DEFAULT).laneIsSet(pos) ? trueValues[pos] : falseValues[pos];
167174
if (posNext > pos) {
168175
round--;
169176
}
@@ -175,8 +182,8 @@ public Long part2(@NotNull BufferedReader reader) throws Exception {
175182
}
176183

177184
private static long calculateLevel(int[] inspectionCounts) {
178-
var highest = -1L;
179-
var secondHighest = -1L;
185+
var highest = -1;
186+
var secondHighest = -1;
180187
for (int value : inspectionCounts) {
181188
if (value > secondHighest) {
182189
if (value < highest) {
@@ -188,6 +195,6 @@ private static long calculateLevel(int[] inspectionCounts) {
188195
}
189196
}
190197

191-
return highest * secondHighest;
198+
return Math.multiplyFull(highest, secondHighest);
192199
}
193200
}

0 commit comments

Comments
 (0)