generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Day 14: | ||
# Day 14: Restroom Redoubt | ||
|
||
[https://adventofcode.com/2024/day/14](https://adventofcode.com/2024/day/14) |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/adventofcode/flashk/day14/RestroomRedoubt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.adventofcode.flashk.day14; | ||
|
||
import com.adventofcode.flashk.common.Collider2D; | ||
import com.adventofcode.flashk.common.Vector2; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class RestroomRedoubt { | ||
|
||
private final int rows; | ||
private final int cols; | ||
|
||
private final List<Robot> robots; | ||
|
||
public RestroomRedoubt(List<String> inputs, int rows, int cols) { | ||
this.rows = rows; | ||
this.cols = cols; | ||
robots = inputs.stream().map(Robot::new).toList(); | ||
} | ||
|
||
public int solveA(int seconds) { | ||
|
||
int q1Robots = 0; | ||
int q2Robots = 0; | ||
int q3Robots = 0; | ||
int q4Robots = 0; | ||
|
||
Collider2D quadrant1 = new Collider2D(new Vector2(0,0), new Vector2((cols/2)-1, (rows/2)-1)); | ||
Collider2D quadrant2 = new Collider2D(new Vector2((cols/2)+1,0), new Vector2(cols-1, (rows/2)-1)); | ||
Collider2D quadrant3 = new Collider2D(new Vector2(0,(rows/2)+1), new Vector2((cols/2)-1,rows-1)); | ||
Collider2D quadrant4 = new Collider2D(new Vector2((cols/2)+1,(rows/2)+1), new Vector2(cols-1,rows-1)); | ||
|
||
for(Robot robot : robots) { | ||
robot.move(seconds, rows, cols); | ||
// quadrant1.collidesWith(robot.getPosition()) was not working. Possible reasons: | ||
// - collidesWith(Vector2) is only checking if the point is part of a line instead of an area. | ||
// - Didn't test diagonal test cases | ||
if(quadrant1.collidesWith(new Collider2D(robot.getPosition()))) { | ||
q1Robots++; | ||
} else if(quadrant2.collidesWith(new Collider2D(robot.getPosition()))) { | ||
q2Robots++; | ||
} else if(quadrant3.collidesWith(new Collider2D(robot.getPosition()))) { | ||
q3Robots++; | ||
} else if(quadrant4.collidesWith(new Collider2D(robot.getPosition()))) { | ||
q4Robots++; | ||
} | ||
} | ||
|
||
return q1Robots * q2Robots * q3Robots * q4Robots; | ||
} | ||
|
||
public int solveB() { | ||
int seconds = 0; | ||
long count = -1; | ||
|
||
do { | ||
seconds++; | ||
for (Robot robot : robots) { | ||
robot.move(rows, cols); | ||
count = robots.stream().map(Robot::getPosition).distinct().count(); | ||
} | ||
} while (count != robots.size()) ; | ||
|
||
paint(); | ||
|
||
return seconds; | ||
|
||
} | ||
|
||
private void paint() { | ||
for(int row = 0; row < rows; row++) { | ||
for(int col = 0; col < cols; col++) { | ||
final int x = col; | ||
final int y = row; | ||
Optional<Robot> robot = robots.stream() | ||
.filter(r -> r.getPosition().getX() == x) | ||
.filter(r -> r.getPosition().getY() == y) | ||
.distinct() | ||
.findFirst(); | ||
|
||
|
||
if(robot.isPresent()) { | ||
System.out.print("#"); | ||
} else { | ||
System.out.print("."); | ||
} | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.adventofcode.flashk.day14; | ||
|
||
import com.adventofcode.flashk.common.Vector2; | ||
import lombok.Getter; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Robot { | ||
|
||
|
||
private static final Pattern ROBOT_PATTERN = Pattern.compile("p=(-?\\d*),(-?\\d*) v=(-?\\d*),(-?\\d*)"); | ||
|
||
@Getter | ||
private Vector2 position; | ||
private final Vector2 direction; | ||
|
||
public Robot(String input) { | ||
super(); | ||
|
||
Matcher matcher = ROBOT_PATTERN.matcher(input); | ||
|
||
if(matcher.find()) { | ||
|
||
String px = matcher.group(1); | ||
String py = matcher.group(2); | ||
String vx = matcher.group(3); | ||
String vy = matcher.group(4); | ||
|
||
position = new Vector2(Integer.parseInt(px), Integer.parseInt(py)); | ||
direction = new Vector2(Integer.parseInt(vx), Integer.parseInt(vy)); | ||
} else { | ||
throw new IllegalArgumentException("Invalid string"); | ||
} | ||
|
||
} | ||
|
||
public void move(int seconds, int rows, int cols) { | ||
for(int i = 0; i < seconds; i++) { | ||
move(rows, cols); | ||
} | ||
} | ||
|
||
public void move(int rows, int cols) { | ||
int x1 = position.getX() + direction.getX(); | ||
int y1 = position.getY() + direction.getY(); | ||
int x2 = (cols + x1) % cols; | ||
int y2 = (rows + y1) % rows; | ||
position = new Vector2(x2,y2); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule inputs
updated
from f92cbf to 141e25