Skip to content
Snippets Groups Projects
Commit 8b1b9ebc authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Completed Year 2022 Day 9

parent fc6bff03
No related branches found
No related tags found
No related merge requests found
...@@ -267,11 +267,11 @@ The subproblems are ...@@ -267,11 +267,11 @@ The subproblems are
### Part 2 ### Part 2
... The subproblems are essentially the same as before, substituting "the head" and "tail" with "a knot" and "the next knot"
### Refactoring ### Refactoring
... We'll replace the explicit `head` and `tail` with an array of `Position`s.
## Day 10 ## Day 10
......
...@@ -9,45 +9,67 @@ import java.util.stream.IntStream; ...@@ -9,45 +9,67 @@ import java.util.stream.IntStream;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Day9 extends Puzzle { public class Day9 extends Puzzle {
@SuppressWarnings("CommentedOutCode")
public Day9(boolean isProductionReady) { public Day9(boolean isProductionReady) {
super(isProductionReady); super(isProductionReady);
// sampleData = """
// R 4
// U 4
// L 3
// D 1
// R 4
// D 1
// L 5
// R 2""";
sampleData = """ sampleData = """
R 4 R 5
U 4 U 8
L 3 L 8
D 1 D 3
R 4 R 17
D 1 D 10
L 5 L 25
R 2"""; U 20""";
} }
@Override @Override
public long computePart1(List<String> data) { public long computePart1(List<String> data) {
List<Character> movements = extractIndividualMovements(data); List<Character> movements = extractIndividualMovements(data);
int totalDistanceTravelled = movements.size(); int totalDistanceTravelled = movements.size();
boolean[][] visitedLocations = manipulateRope(movements, totalDistanceTravelled, 2);
return countVisitedLocations(visitedLocations);
}
@Override
public long computePart2(List<String> data) {
List<Character> movements = extractIndividualMovements(data);
int totalDistanceTravelled = movements.size();
boolean[][] visitedLocations = manipulateRope(movements, totalDistanceTravelled, 10);
return countVisitedLocations(visitedLocations);
}
private static boolean[][] manipulateRope(List<Character> movements,
int totalDistanceTravelled,
int numberOfKnots) {
boolean[][] visitedLocations = new boolean[2 * totalDistanceTravelled][2 * totalDistanceTravelled]; boolean[][] visitedLocations = new boolean[2 * totalDistanceTravelled][2 * totalDistanceTravelled];
Arrays.stream(visitedLocations).forEach(row -> Arrays.fill(row, false)); Arrays.stream(visitedLocations).forEach(row -> Arrays.fill(row, false));
Position head = new Position(totalDistanceTravelled, totalDistanceTravelled); Position[] knots = new Position[numberOfKnots];
Position tail = new Position(totalDistanceTravelled, totalDistanceTravelled); Arrays.fill(knots, new Position(totalDistanceTravelled, totalDistanceTravelled));
visitedLocations[tail.row][tail.column] = true; visitedLocations[knots[numberOfKnots - 1].row][knots[numberOfKnots - 1].column] = true;
for (Character movement : movements) { for (Character movement : movements) {
head = switch (movement) { knots[0] = switch (movement) {
case 'R' -> new Position(head.row, head.column + 1); case 'R' -> new Position(knots[0].row, knots[0].column + 1);
case 'L' -> new Position(head.row, head.column - 1); case 'L' -> new Position(knots[0].row, knots[0].column - 1);
case 'U' -> new Position(head.row + 1, head.column); case 'U' -> new Position(knots[0].row + 1, knots[0].column);
case 'D' -> new Position(head.row - 1, head.column); case 'D' -> new Position(knots[0].row - 1, knots[0].column);
default -> throw new IllegalStateException("Unrecognized movement: " + movement); default -> throw new IllegalStateException("Unrecognized movement: " + movement);
}; };
tail = tail.moveToward(head); for (int i = 1; i < numberOfKnots; i++) {
visitedLocations[tail.row][tail.column] = true; knots[i] = knots[i].moveToward(knots[i-1]);
} }
return countVisitedLocations(visitedLocations); visitedLocations[knots[numberOfKnots - 1].row][knots[numberOfKnots - 1].column] = true;
} }
return visitedLocations;
@Override
public long computePart2(List<String> data) {
return 0;
} }
private record Position(int row, int column) { private record Position(int row, int column) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment