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

There's a bug in Day 15 part 1 that I need to track down.

Row 10:
```
-8 ..........#.......................... 44
```
parent 935fe153
No related branches found
No related tags found
No related merge requests found
......@@ -520,6 +520,7 @@ Then look for the shortest distance to the finish from the 'a' `Location`s.
### Part 1
The subproblems are
- Decode packets
- Pairs of packets, each on its own line, with pairs separated by blank lines
- A packet is a list
......@@ -537,6 +538,7 @@ The subproblems are
### Part 2
The subproblems are
- Add the divider packets (trivial)
- Sort the packets
- Determine the indicies of the divider packets (using 1-based indexing)
......@@ -551,6 +553,7 @@ Since I created a `compareTo()` method as part of Part 1, Part 2 is easy.
### Part 1
The subproblems are
- Determine which locations are blocked
- Determine where a unit of sand moves to
- Can it move?
......@@ -566,5 +569,22 @@ Instead, we'll have to pretend there's an infinitely-long floor.
## Day 15
- [The problem](https://adventofcode.com/2022/day/15)
- [The solution](src/main/java/edu/unl/cse/bohn/year2022/Day15.java)
### Part 1
The subproblems are
- Parse the input to determine where each sensor is and where its nearest beacon is
- Determine which positions cannot contain a beacon (*i.e.*, which positions are no closer to the sensor
than the beacon)
### Part 2
...
## Day 16
(coming soon)
package edu.unl.cse.bohn.year2022;
import edu.unl.cse.bohn.Puzzle;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
@SuppressWarnings("unused")
public class Day15 extends Puzzle {
public Day15(boolean isProductionReady) {
super(isProductionReady);
sampleData = """
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3""";
}
@Override
public long computePart1(List<String> data) {
for (String datum : data) {
new Sensor(datum);
}
Sensor.markPositionsAsHavingNoUnknownBeacon();
return Sensor.countPositionsWithNoUnknownBeacon(10);
}
@Override
public long computePart2(List<String> data) {
return 0;
}
private static class Sensor {
private final int x, y;
private final int nearestBeaconX, nearestBeaconY;
private static final Set<Sensor> sensors = new HashSet<>();
private static boolean[][] mightContainAnUnknownBeacon = null;
private static int xOffset, yOffset;
public Sensor(String description) {
String[] hardwareDescriptions = description.split(":");
String[] coordinateDescriptions = hardwareDescriptions[0].split(",");
x = Integer.parseInt(coordinateDescriptions[0].substring(coordinateDescriptions[0].indexOf('=') + 1));
y = Integer.parseInt(coordinateDescriptions[1].substring(coordinateDescriptions[1].indexOf('=') + 1));
coordinateDescriptions = hardwareDescriptions[1].split(",");
nearestBeaconX =
Integer.parseInt(coordinateDescriptions[0].substring(coordinateDescriptions[0].indexOf('=') + 1));
nearestBeaconY =
Integer.parseInt(coordinateDescriptions[1].substring(coordinateDescriptions[1].indexOf('=') + 1));
sensors.add(this);
}
public int getDistanceToBeacon() {
return getDistanceToLocation(nearestBeaconX, nearestBeaconY);
}
private int getDistanceToLocation(int x, int y) {
return Math.abs(x - this.x) + Math.abs(y - this.y);
}
public boolean isNoFartherThanBeacon(int x, int y) {
return getDistanceToLocation(x, y) <= getDistanceToBeacon();
}
@Override
public String toString() {
return "Sensor at x=" + x + ", y=" + y + ": closest beacon is at x=" + nearestBeaconX
+ ", y=" + nearestBeaconY + "\t(distance=" + getDistanceToBeacon() + ")";
}
public static void markPositionsAsHavingNoUnknownBeacon() {
// create the matrix
int maximumX = sensors.stream()
.mapToInt(sensor -> sensor.x + sensor.getDistanceToBeacon())
.max().orElse(Integer.MIN_VALUE);
int minimumX = sensors.stream()
.mapToInt(sensor -> sensor.x - sensor.getDistanceToBeacon())
.min().orElse(Integer.MAX_VALUE);
int maximumY = sensors.stream()
.mapToInt(sensor -> sensor.y + sensor.getDistanceToBeacon())
.max().orElse(Integer.MIN_VALUE);
int minimumY = sensors.stream()
.mapToInt(sensor -> sensor.y - sensor.getDistanceToBeacon())
.min().orElse(Integer.MAX_VALUE);
xOffset = minimumX;
yOffset = minimumY;
mightContainAnUnknownBeacon = new boolean[maximumY - yOffset + 1][maximumX - xOffset + 1];
IntStream.range(0, mightContainAnUnknownBeacon.length)
.forEach(i -> Arrays.fill(mightContainAnUnknownBeacon[i], true));
// make our deductions
for (int i = 0; i < mightContainAnUnknownBeacon.length; i++) {
for (int j = 0; j < mightContainAnUnknownBeacon[i].length; j++) {
int y = i + yOffset, x = j + xOffset;
for (Sensor sensor : sensors) {
if (sensor.isNoFartherThanBeacon(x, y)) {
mightContainAnUnknownBeacon[i][j] = false;
}
}
}
}
}
public static int countPositionsWithNoUnknownBeacon(int y) {
if (mightContainAnUnknownBeacon == null) {
markPositionsAsHavingNoUnknownBeacon();
}
System.out.print(xOffset + " ");
int countOfUnobservedLocations = 0;
for (boolean b : mightContainAnUnknownBeacon[y + yOffset]) {
System.out.print(b ? '.' : "#");
countOfUnobservedLocations += b ? 0 : 1;
}
System.out.println(" " + (mightContainAnUnknownBeacon[y + yOffset].length - xOffset - 1));
return countOfUnobservedLocations;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment