Skip to content

Commit 0246919

Browse files
committed
Improve year 2022 day9
1 parent 13de94a commit 0246919

File tree

1 file changed

+8
-3
lines changed
  • year2022/src/main/java/dev/linl33/adventofcode/year2022

1 file changed

+8
-3
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import java.nio.ByteBuffer;
99

1010
public class Day9 extends AdventSolution2022<Integer, Integer> implements ByteBufferAdventSolution<Integer, Integer>, NullBufferedReaderSolution<Integer, Integer> {
11-
private static final int HASH_TABLE_BUCKET_SIZE = 20;
12-
private static final int HASH_TABLE_BUCKETS = 1024;
11+
private static final int HASH_TABLE_BUCKET_SIZE = 10;
12+
private static final int HASH_TABLE_BUCKETS = 2048;
1313

1414
public static void main(String[] args) {
1515
new Day9().runAndPrintAll();
@@ -69,14 +69,19 @@ private static int countUniqueTailPositions(@NotNull ByteBuffer byteBuffer, int
6969
for (int i = 0; i < magnitude; i++) {
7070
var prevKnotX = positions[0];
7171
var prevKnotY = positions[knots];
72-
for (int x = 1, y = knots + 1; x < knots; x++, y++) {
72+
for (int x = 1, y = 1 + knots; x < knots; x++, y++) {
7373
var currKnotX = positions[x];
7474
var currKnotY = positions[y];
7575

7676
if (!isValid(prevKnotX, prevKnotY, currKnotX, currKnotY)) {
7777
prevKnotX = (positions[x] += Integer.signum(prevKnotX - currKnotX));
7878
prevKnotY = (positions[y] += Integer.signum(prevKnotY - currKnotY));
7979
} else {
80+
if (x == 1) {
81+
// entire rope is valid now
82+
break moveKnots;
83+
}
84+
8085
// all following knots will not move either
8186
continue moveKnots;
8287
}

0 commit comments

Comments
 (0)