day15
This commit is contained in:
parent
35af3ff7a5
commit
998531fa4e
2
15/.gitignore
vendored
Normal file
2
15/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.vimspector.json
|
||||
day15
|
23
15/beacons.txt
Normal file
23
15/beacons.txt
Normal file
@ -0,0 +1,23 @@
|
||||
Sensor at x=3907621, y=2895218: closest beacon is at x=3790542, y=2949630
|
||||
Sensor at x=1701067, y=3075142: closest beacon is at x=2275951, y=3717327
|
||||
Sensor at x=3532369, y=884718: closest beacon is at x=2733699, y=2000000
|
||||
Sensor at x=2362427, y=41763: closest beacon is at x=2999439, y=-958188
|
||||
Sensor at x=398408, y=3688691: closest beacon is at x=2275951, y=3717327
|
||||
Sensor at x=1727615, y=1744968: closest beacon is at x=2733699, y=2000000
|
||||
Sensor at x=2778183, y=3611924: closest beacon is at x=2275951, y=3717327
|
||||
Sensor at x=2452818, y=2533012: closest beacon is at x=2733699, y=2000000
|
||||
Sensor at x=88162, y=2057063: closest beacon is at x=-109096, y=390805
|
||||
Sensor at x=2985370, y=2315046: closest beacon is at x=2733699, y=2000000
|
||||
Sensor at x=2758780, y=3000106: closest beacon is at x=3279264, y=2775610
|
||||
Sensor at x=3501114, y=3193710: closest beacon is at x=3790542, y=2949630
|
||||
Sensor at x=313171, y=1016326: closest beacon is at x=-109096, y=390805
|
||||
Sensor at x=3997998, y=3576392: closest beacon is at x=3691556, y=3980872
|
||||
Sensor at x=84142, y=102550: closest beacon is at x=-109096, y=390805
|
||||
Sensor at x=3768533, y=3985372: closest beacon is at x=3691556, y=3980872
|
||||
Sensor at x=2999744, y=3998031: closest beacon is at x=3691556, y=3980872
|
||||
Sensor at x=3380504, y=2720962: closest beacon is at x=3279264, y=2775610
|
||||
Sensor at x=3357940, y=3730208: closest beacon is at x=3691556, y=3980872
|
||||
Sensor at x=1242851, y=838744: closest beacon is at x=-109096, y=390805
|
||||
Sensor at x=3991401, y=2367688: closest beacon is at x=3790542, y=2949630
|
||||
Sensor at x=3292286, y=2624894: closest beacon is at x=3279264, y=2775610
|
||||
Sensor at x=2194423, y=3990859: closest beacon is at x=2275951, y=3717327
|
117
15/day15.c
Normal file
117
15/day15.c
Normal file
@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MAX_SENSORS 30
|
||||
#define MAX_X 4000000
|
||||
#define MAX_Y 4000000
|
||||
|
||||
struct Sensor {
|
||||
int x, y;
|
||||
unsigned range;
|
||||
};
|
||||
struct Beacon {
|
||||
int x, y;
|
||||
};
|
||||
struct Area {
|
||||
int left, right, top, bottom;
|
||||
};
|
||||
|
||||
unsigned distance(int x1, int y1, int x2, int y2) {
|
||||
int x_dist = abs(x1 - x2);
|
||||
int y_dist = abs(y1 - y2);
|
||||
return x_dist + y_dist;
|
||||
}
|
||||
int max(int a, int b) {
|
||||
if (a > b) { return a; }
|
||||
return b;
|
||||
}
|
||||
int min(int a, int b) {
|
||||
if (a < b) { return a; }
|
||||
return b;
|
||||
}
|
||||
|
||||
int isPositionImpossible(int x, int y, struct Beacon* beacons, struct Sensor* sensors, unsigned sensorCount, unsigned* blockingSensor) {
|
||||
for (unsigned i = 0; i < sensorCount; i++) {
|
||||
if (x == beacons[i].x && y == beacons[i].y) { continue; } // if beacon
|
||||
if (distance(x, y, sensors[i].x, sensors[i].y) <= sensors[i].range) {
|
||||
*blockingSensor = i;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char** argv) {
|
||||
const char* filename = NULL;
|
||||
if (argc != 2) {
|
||||
filename = "beacons.txt";
|
||||
}
|
||||
else {
|
||||
filename = argv[1];
|
||||
}
|
||||
FILE* p_file;
|
||||
p_file = fopen(filename, "r");
|
||||
if (p_file == NULL) {
|
||||
printf("Could not open file\n");
|
||||
exit(1);
|
||||
}
|
||||
struct Sensor sensors[MAX_SENSORS];
|
||||
struct Beacon beacons[MAX_SENSORS];
|
||||
unsigned sensorCount = 0;
|
||||
struct Area area;
|
||||
|
||||
char* p_linebuffer = NULL;
|
||||
size_t linebufferLength = 0;
|
||||
ssize_t lineLength = getline(&p_linebuffer, &linebufferLength, p_file);
|
||||
|
||||
while (lineLength > 0) {
|
||||
sscanf(p_linebuffer, "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", &sensors[sensorCount].x, &sensors[sensorCount].y, &beacons[sensorCount].x, &beacons[sensorCount].y);
|
||||
sensors[sensorCount].range = distance(sensors[sensorCount].x, sensors[sensorCount].y, beacons[sensorCount].x, beacons[sensorCount].y);
|
||||
lineLength = getline(&p_linebuffer, &linebufferLength, p_file);
|
||||
sensorCount++;
|
||||
}
|
||||
free(p_linebuffer);
|
||||
p_linebuffer = NULL;
|
||||
fclose(p_file);
|
||||
|
||||
area.left = sensors[0].x - sensors[0].range;
|
||||
area.right = sensors[0].x + sensors[0].range;
|
||||
area.top = sensors[0].y - sensors[0].range;
|
||||
area.bottom = sensors[0].y + sensors[0].range;
|
||||
printf("Sensor: x0%d, y=%d, range=%d\n", sensors[0].x, sensors[0].y, sensors[0].range);
|
||||
for (unsigned i = 1; i < sensorCount; i++) {
|
||||
area.left = min(sensors[i].x - sensors[i].range, area.left);
|
||||
area.right = max(sensors[i].x + sensors[i].range, area.right);
|
||||
area.top = min(sensors[i].y - sensors[i].range, area.top);
|
||||
area.bottom = max(sensors[i].y + sensors[i].range, area.bottom);
|
||||
printf("Sensor: x=%d, y=%d, range=%d\n", sensors[i].x, sensors[i].y, sensors[i].range);
|
||||
}
|
||||
printf("Area: l-r: %d-%d, t-b: %d-%d\n", area.left, area.right, area.top, area.bottom);
|
||||
int no_beacons_for_sure_mate = 0;
|
||||
/* int y = 10; */
|
||||
int y = 2000000;
|
||||
// this could be optmized 50000000%
|
||||
unsigned blockingSensor;
|
||||
for (int x = area.left; x <= area.right; x++) {
|
||||
no_beacons_for_sure_mate += isPositionImpossible(x, y, beacons, sensors, sensorCount, &blockingSensor);
|
||||
}
|
||||
printf("In row %d, there are %d positions where no beacons are possible\n", y, no_beacons_for_sure_mate);
|
||||
|
||||
for (int x = 0; x < MAX_X; x++) {
|
||||
for (int y = 0; y < MAX_Y; y++) {
|
||||
if (!isPositionImpossible(x, y, beacons, sensors, sensorCount, &blockingSensor)) {
|
||||
unsigned long frequency = (unsigned long) x * 4000000 + y;
|
||||
printf("Tuning frequency from beacon at (%d, %d): %luHz\n", x, y, frequency);
|
||||
goto done;
|
||||
}
|
||||
// skip the until the end of range for the blocking sensor
|
||||
unsigned y_range = sensors[blockingSensor].range - abs(sensors[blockingSensor].x - x);
|
||||
// last y in range
|
||||
y = sensors[blockingSensor].y + y_range;
|
||||
}
|
||||
}
|
||||
printf("Could not find distress beacon :(\n");
|
||||
done:
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user