Skip to content

Commit 24f41bc

Browse files
committed
feat: solve day 14
1 parent 08a7a03 commit 24f41bc

File tree

6 files changed

+166
-28
lines changed

6 files changed

+166
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [Day 11 - Plutonian Pebbles](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day11)
1717
- [Day 12 - Garden Groups](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day12)
1818
- [Day 13 - Claw Contraption](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day13)
19-
- [Day 14](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day14)
19+
- [Day 14 - Restroom Redoubt](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day14)
2020
- [Day 15](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day15)
2121
- [Day 16](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day16)
2222
- [Day 17](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day17)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Day 14:
1+
# Day 14: Restroom Redoubt
22

33
[https://adventofcode.com/2024/day/14](https://adventofcode.com/2024/day/14)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.adventofcode.flashk.day14;
2+
3+
import com.adventofcode.flashk.common.Collider2D;
4+
import com.adventofcode.flashk.common.Vector2;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
public class RestroomRedoubt {
10+
11+
private final int rows;
12+
private final int cols;
13+
14+
private final List<Robot> robots;
15+
16+
public RestroomRedoubt(List<String> inputs, int rows, int cols) {
17+
this.rows = rows;
18+
this.cols = cols;
19+
robots = inputs.stream().map(Robot::new).toList();
20+
}
21+
22+
public int solveA(int seconds) {
23+
24+
int q1Robots = 0;
25+
int q2Robots = 0;
26+
int q3Robots = 0;
27+
int q4Robots = 0;
28+
29+
Collider2D quadrant1 = new Collider2D(new Vector2(0,0), new Vector2((cols/2)-1, (rows/2)-1));
30+
Collider2D quadrant2 = new Collider2D(new Vector2((cols/2)+1,0), new Vector2(cols-1, (rows/2)-1));
31+
Collider2D quadrant3 = new Collider2D(new Vector2(0,(rows/2)+1), new Vector2((cols/2)-1,rows-1));
32+
Collider2D quadrant4 = new Collider2D(new Vector2((cols/2)+1,(rows/2)+1), new Vector2(cols-1,rows-1));
33+
34+
for(Robot robot : robots) {
35+
robot.move(seconds, rows, cols);
36+
// quadrant1.collidesWith(robot.getPosition()) was not working. Possible reasons:
37+
// - collidesWith(Vector2) is only checking if the point is part of a line instead of an area.
38+
// - Didn't test diagonal test cases
39+
if(quadrant1.collidesWith(new Collider2D(robot.getPosition()))) {
40+
q1Robots++;
41+
} else if(quadrant2.collidesWith(new Collider2D(robot.getPosition()))) {
42+
q2Robots++;
43+
} else if(quadrant3.collidesWith(new Collider2D(robot.getPosition()))) {
44+
q3Robots++;
45+
} else if(quadrant4.collidesWith(new Collider2D(robot.getPosition()))) {
46+
q4Robots++;
47+
}
48+
}
49+
50+
return q1Robots * q2Robots * q3Robots * q4Robots;
51+
}
52+
53+
public int solveB() {
54+
int seconds = 0;
55+
long count = -1;
56+
57+
do {
58+
seconds++;
59+
for (Robot robot : robots) {
60+
robot.move(rows, cols);
61+
count = robots.stream().map(Robot::getPosition).distinct().count();
62+
}
63+
} while (count != robots.size()) ;
64+
65+
paint();
66+
67+
return seconds;
68+
69+
}
70+
71+
private void paint() {
72+
for(int row = 0; row < rows; row++) {
73+
for(int col = 0; col < cols; col++) {
74+
final int x = col;
75+
final int y = row;
76+
Optional<Robot> robot = robots.stream()
77+
.filter(r -> r.getPosition().getX() == x)
78+
.filter(r -> r.getPosition().getY() == y)
79+
.distinct()
80+
.findFirst();
81+
82+
83+
if(robot.isPresent()) {
84+
System.out.print("#");
85+
} else {
86+
System.out.print(".");
87+
}
88+
}
89+
System.out.println();
90+
}
91+
}
92+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.adventofcode.flashk.day14;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
import lombok.Getter;
5+
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
public class Robot {
10+
11+
12+
private static final Pattern ROBOT_PATTERN = Pattern.compile("p=(-?\\d*),(-?\\d*) v=(-?\\d*),(-?\\d*)");
13+
14+
@Getter
15+
private Vector2 position;
16+
private final Vector2 direction;
17+
18+
public Robot(String input) {
19+
super();
20+
21+
Matcher matcher = ROBOT_PATTERN.matcher(input);
22+
23+
if(matcher.find()) {
24+
25+
String px = matcher.group(1);
26+
String py = matcher.group(2);
27+
String vx = matcher.group(3);
28+
String vy = matcher.group(4);
29+
30+
position = new Vector2(Integer.parseInt(px), Integer.parseInt(py));
31+
direction = new Vector2(Integer.parseInt(vx), Integer.parseInt(vy));
32+
} else {
33+
throw new IllegalArgumentException("Invalid string");
34+
}
35+
36+
}
37+
38+
public void move(int seconds, int rows, int cols) {
39+
for(int i = 0; i < seconds; i++) {
40+
move(rows, cols);
41+
}
42+
}
43+
44+
public void move(int rows, int cols) {
45+
int x1 = position.getX() + direction.getX();
46+
int y1 = position.getY() + direction.getY();
47+
int x2 = (cols + x1) % cols;
48+
int y2 = (rows + y1) % rows;
49+
position = new Vector2(x2,y2);
50+
}
51+
52+
53+
}

src/test/java/com/adventofcode/flashk/day14/Day14Test.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import org.junit.jupiter.api.Disabled;
65
import org.junit.jupiter.api.DisplayName;
76
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
87
import org.junit.jupiter.api.Order;
@@ -14,71 +13,65 @@
1413
import com.adventofcode.flashk.common.test.constants.TestFilename;
1514
import com.adventofcode.flashk.common.test.constants.TestFolder;
1615
import com.adventofcode.flashk.common.test.constants.TestTag;
17-
import com.adventofcode.flashk.common.test.utils.PuzzleTest;
1816
import com.adventofcode.flashk.common.test.utils.Input;
1917

2018
import static org.junit.jupiter.api.Assertions.assertEquals;
2119

2220
@DisplayName(TestDisplayName.DAY_14)
2321
@TestMethodOrder(OrderAnnotation.class)
24-
@Disabled // TODO Remove comment when implemented
25-
public class Day14Test extends PuzzleTest {
22+
class Day14Test {
2623

2724
private static final String INPUT_FOLDER = TestFolder.DAY_14;
2825

26+
private static final int MAX_ROWS_SAMPLE = 7;
27+
private static final int MAX_COLS_SAMPLE = 11;
28+
29+
private static final int MAX_ROWS_INPUT = 103;
30+
private static final int MAX_COLS_INPUT = 101;
31+
2932
@Test
3033
@Order(1)
3134
@Tag(TestTag.PART_1)
3235
@Tag(TestTag.SAMPLE)
3336
@DisplayName(TestDisplayName.PART_1_SAMPLE)
34-
public void testSolvePart1Sample() {
37+
void part1SampleTest() {
3538

3639
// Read input file
3740
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
3841

39-
assertEquals(0L,0L);
42+
RestroomRedoubt restroomRedoubt = new RestroomRedoubt(inputs, MAX_ROWS_SAMPLE, MAX_COLS_SAMPLE);
43+
44+
assertEquals(12L,restroomRedoubt.solveA(100));
4045
}
4146

4247
@Test
4348
@Order(2)
4449
@Tag(TestTag.PART_1)
4550
@Tag(TestTag.INPUT)
4651
@DisplayName(TestDisplayName.PART_1_INPUT)
47-
public void testSolvePart1Input() {
52+
void part1InputTest() {
4853

4954
// Read input file
5055
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
56+
RestroomRedoubt restroomRedoubt = new RestroomRedoubt(inputs, MAX_ROWS_INPUT, MAX_COLS_INPUT);
5157

52-
System.out.println("Solution: ");
53-
assertEquals(0L,0L);
58+
assertEquals(214109808,restroomRedoubt.solveA(100));
5459

5560
}
5661

57-
@Test
58-
@Order(3)
59-
@Tag(TestTag.PART_2)
60-
@Tag(TestTag.SAMPLE)
61-
@DisplayName(TestDisplayName.PART_2_SAMPLE)
62-
public void testSolvePart2Sample() {
63-
64-
// Read input file
65-
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
66-
67-
assertEquals(0L,0L);
68-
}
6962

7063
@Test
71-
@Order(4)
64+
@Order(3)
7265
@Tag(TestTag.PART_2)
7366
@Tag(TestTag.INPUT)
7467
@DisplayName(TestDisplayName.PART_2_INPUT)
75-
public void testSolvePart2Input() {
68+
void part2InputTest() {
7669

7770
// Read input file
7871
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
72+
RestroomRedoubt restroomRedoubt = new RestroomRedoubt(inputs, MAX_ROWS_INPUT, MAX_COLS_INPUT);
7973

80-
System.out.println("Solution: ");
81-
assertEquals(0L,0L);
74+
assertEquals(7687, restroomRedoubt.solveB());
8275

8376
}
8477

src/test/resources/inputs

0 commit comments

Comments
 (0)