From 998531fa4ec4922fd28fbe60a490e2903e335954 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Mon, 19 Dec 2022 17:01:28 +0100 Subject: [PATCH] day15 --- 15/.gitignore | 2 + 15/beacons.txt | 23 ++++++++++ 15/day15.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 15/.gitignore create mode 100644 15/beacons.txt create mode 100644 15/day15.c diff --git a/15/.gitignore b/15/.gitignore new file mode 100644 index 0000000..a3e5a87 --- /dev/null +++ b/15/.gitignore @@ -0,0 +1,2 @@ +.vimspector.json +day15 diff --git a/15/beacons.txt b/15/beacons.txt new file mode 100644 index 0000000..80de533 --- /dev/null +++ b/15/beacons.txt @@ -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 diff --git a/15/day15.c b/15/day15.c new file mode 100644 index 0000000..b676758 --- /dev/null +++ b/15/day15.c @@ -0,0 +1,117 @@ +#include +#include +#include + +#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; +} diff --git a/README.md b/README.md index 1c7db1e..fa6c818 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Also, check out the repositories of my friends who do stuff in go, Visual Basic, - **Awk**: day 1, 7 - **Assembly, x86-64**: day 4 - **Bash**: day 1, 7 -- **C**: day 3, 4 +- **C**: day 3, 4, 15 - **C++**: day 2, 8, 12, 14 - **HolyC:** day 13 - **Java**: day 10