aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day15/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2022/day15/aoc.cpp')
-rw-r--r--src/2022/day15/aoc.cpp92
1 files changed, 67 insertions, 25 deletions
diff --git a/src/2022/day15/aoc.cpp b/src/2022/day15/aoc.cpp
index 09a923f..1314b8d 100644
--- a/src/2022/day15/aoc.cpp
+++ b/src/2022/day15/aoc.cpp
@@ -5,42 +5,84 @@ namespace aoc2022 {
int sensor::minx;
int sensor::maxx;
+typedef sensor::pos pos;
+typedef sensor::line line;
int no_beacon(int y, std::vector<sensor>& ss) {
- int count{0};
- for (int x = sensor::minx; x <= sensor::maxx; x++) {
- sensor::pos p;
- p.x = x;
- p.y = y;
- size_t n{0};
- for (auto&s : ss) {
- if(s.inscope(p)) {
- n++;
- }
+ int total = sensor::maxx - sensor::minx + 1;
+ char * p = (char *) malloc(total);
+ memset(p, 0, total);
+ std::vector<line> ls;
+ for (auto& s : ss) {
+ int dy = y - s.ps[0].y;
+ if (sensor::mdistance(s.ps[0], s.ps[1]) >= std::abs(dy)) {
+ ls.push_back(s.line_by_dy(dy));
}
- // printf("(%d, %d) has %zu\n", x, y, n);
- count += (bool) n > 0;
}
- return count;
-}
-bool valid(sensor::pos p, int dx) {
- return p.x >= 0 && p.x <= dx && p.y >= 0 && p.y <= dx;
+ for (auto& l : ls) {
+ int x0 = l.p0.x - sensor::minx;
+ int x1 = l.p1.x - sensor::minx;
+ memset(p + x0, 1, x1 - x0);
+ }
+
+ int ones{0};
+ for (int i = 0; i < total; i++) {
+ ones += (int) *(p + i) == 1;
+ }
+
+ free(p);
+ return ones;
}
-int only_beacon(int dx, std::vector<sensor>& ss) {
- return 0;
+int one_beacon(int y, int range, std::vector<sensor>& ss) {
+ int total = sensor::maxx - sensor::minx + 1;
+ char * p = (char *) malloc(total);
+ memset(p, 0, total);
+
+ std::vector<line> ls;
+ for (auto& s : ss) {
+ int dy = y - s.ps[0].y;
+ if (sensor::mdistance(s.ps[0], s.ps[1]) >= std::abs(dy)) {
+ ls.push_back(s.line_by_dy(dy));
+ }
+ }
+
+ for (auto& l : ls) {
+ int x0 = l.p0.x - sensor::minx;
+ int x1 = l.p1.x - sensor::minx;
+ memset(p + x0, 1, x1 - x0);
+ }
+
+ for (int i = 0 - sensor::minx; i <= range - sensor::minx; i++) {
+ if (*(p + i) == 0) {
+ free(p);
+ return sensor::minx + i + 1;
+ }
+ }
+
+ free(p);
+ return range;
}
std::pair<int, int> day15(line_view file) {
std::vector<sensor> ss;
- per_line(file, [&ss](line_view lv){
- ss.emplace_back(lv);
- return true;
+ per_line(file, [&ss](line_view lv) {
+ ss.emplace_back(lv);
+ return true;
});
- int n1 = no_beacon(2000000, ss);
- int n2 = only_beacon(4000000, ss);
- return {n1, n2};
-}
+ auto n = no_beacon(2000000, ss);
+
+ // int total = 4000000;
+ // for (int y = 0; y <= total; y++) {
+ // int x = one_beacon(y, total, ss);
+ // if (x != total) {
+ // printf("%d %d\n", x, y);
+ // break;
+ // }
+ // }
+
+ return {n, 0};
}
+} // namespace aoc2022