|
8 | 8 | import java.nio.ByteBuffer; |
9 | 9 |
|
10 | 10 | 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; |
13 | 13 |
|
14 | 14 | public static void main(String[] args) { |
15 | 15 | new Day9().runAndPrintAll(); |
@@ -69,14 +69,19 @@ private static int countUniqueTailPositions(@NotNull ByteBuffer byteBuffer, int |
69 | 69 | for (int i = 0; i < magnitude; i++) { |
70 | 70 | var prevKnotX = positions[0]; |
71 | 71 | 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++) { |
73 | 73 | var currKnotX = positions[x]; |
74 | 74 | var currKnotY = positions[y]; |
75 | 75 |
|
76 | 76 | if (!isValid(prevKnotX, prevKnotY, currKnotX, currKnotY)) { |
77 | 77 | prevKnotX = (positions[x] += Integer.signum(prevKnotX - currKnotX)); |
78 | 78 | prevKnotY = (positions[y] += Integer.signum(prevKnotY - currKnotY)); |
79 | 79 | } else { |
| 80 | + if (x == 1) { |
| 81 | + // entire rope is valid now |
| 82 | + break moveKnots; |
| 83 | + } |
| 84 | + |
80 | 85 | // all following knots will not move either |
81 | 86 | continue moveKnots; |
82 | 87 | } |
|
0 commit comments