aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-06 22:00:27 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-06 22:00:27 +0800
commitd3549d2056fe9b78fc786533c4b8f8be839e7770 (patch)
treef737baa6dfe55ca0b678b4ff45762574a40e77c8
parent76183b2d789320456599ef811434f4b65cae9524 (diff)
downloadadvent-of-code-d3549d2056fe9b78fc786533c4b8f8be839e7770.tar.gz
advent-of-code-d3549d2056fe9b78fc786533c4b8f8be839e7770.zip
2019 day10
-rw-r--r--src/2019/day10/aoc.cpp61
-rw-r--r--src/2019/day10/aoc.h2
-rw-r--r--test/test_2019.cpp6
3 files changed, 18 insertions, 51 deletions
diff --git a/src/2019/day10/aoc.cpp b/src/2019/day10/aoc.cpp
index 60de3ea..2104343 100644
--- a/src/2019/day10/aoc.cpp
+++ b/src/2019/day10/aoc.cpp
@@ -1,6 +1,7 @@
#include "aoc.h"
#include <algorithm>
#include <vector>
+#include <math.h>
namespace aoc2019 {
@@ -10,47 +11,20 @@ struct posd {
int count = 0;
};
-enum dim {
- xe0yl0,
- xg0yl0,
- xg0ye0,
- xg0yg0,
- xe0yg0,
- xl0yg0,
- xl0ye0,
- xl0yl0
-};
-
-dim dimention(belt::distance d) {
- if (d.dx == 0 && d.dy < 0) return xe0yl0;
- if (d.dx > 0 && d.dy < 0) return xg0yl0;
- if (d.dx > 0 && d.dy == 0) return xg0ye0;
- if (d.dx > 0 && d.dy > 0) return xg0yg0;
- if (d.dx == 0 && d.dy > 0) return xe0yg0;
- if (d.dx < 0 && d.dy > 0) return xl0yg0;
- if (d.dx < 0 && d.dy == 0) return xl0ye0;
- return xl0yl0;
-}
-
-bool dimcmp(dim m, belt::distance d1, belt::distance d2) {
- int x1 = std::abs(d1.dx);
- int y1 = std::abs(d1.dy);
- int x2 = std::abs(d2.dx);
- int y2 = std::abs(d2.dy);
-
- switch (m) {
- case xe0yl0: return y1 < y2;
- case xg0yl0: return y1 > y2 ? true : y1 < y2 ? false : x1 < x2;
- case xg0ye0: return x1 < x2;
- case xg0yg0: return y1 < y2 ? true : y1 > y2 ? false : x1 < x2;
- case xe0yg0: return y1 < y2;
- case xl0yg0: return y1 > y2 ? true : y1 < y2 ? false : x1 < x2;
- case xl0ye0: return x1 < x2;
- case xl0yl0: return y1 < y2 ? true : y1 > y2 ? false : x1 < x2;
+float angle(int x, int y) {
+ auto a = std::atan2(y, x);
+ auto r = a * 180 / M_PI;
+ if (r > 360) {
+ r -= 360;
}
- return false;
+ if (r < 0) {
+ r += 360;
+ }
+ r += 90;
+ return r >= 360 ? r - 360 : r;
}
+
belt::pos vaporize(belt b, belt::pos& m, std::vector<posd>& ps, int* count) {
belt bx = b;
for (auto& dp : ps) {
@@ -58,7 +32,7 @@ belt::pos vaporize(belt b, belt::pos& m, std::vector<posd>& ps, int* count) {
dp.count = 1;
b.get(dp.p) = '.';
*count += 1;
- printf("%d asteroid to be vaporized is at (%d, %d)\n", *count, dp.p.x, dp.p.y);
+ // printf("%d asteroid to be vaporized is at (%d, %d)\n", *count, dp.p.x, dp.p.y);
if (*count == 200) {
return dp.p;
}
@@ -98,14 +72,7 @@ std::pair<int, int> day10(line_view file) {
}
std::sort(ps.begin(), ps.end(), [](const posd& d1, const posd& d2) {
- dim m1 = dimention(d1.d);
- dim m2 = dimention(d2.d);
- if (m1 == m2) {
- return dimcmp(m1, d1.d, d2.d);
- }
- else {
- return (int) m1 < (int) m2;
- }
+ return angle(d1.d.dx, d1.d.dy) < angle(d2.d.dx, d2.d.dy);
});
int count{0};
diff --git a/src/2019/day10/aoc.h b/src/2019/day10/aoc.h
index 02de28b..916566e 100644
--- a/src/2019/day10/aoc.h
+++ b/src/2019/day10/aoc.h
@@ -5,7 +5,7 @@
namespace aoc2019 {
struct belt {
- static constexpr int grid = 20;
+ static constexpr int grid = 34;
struct distance {
int dx;
diff --git a/test/test_2019.cpp b/test/test_2019.cpp
index c8b6394..2876ec9 100644
--- a/test/test_2019.cpp
+++ b/test/test_2019.cpp
@@ -69,8 +69,8 @@ TEST_CASE("Space Image Format", "[2019]") {
}
TEST_CASE("Monitoring Station", "[2019]") {
- line_view lv = load_file("../src/2019/day10/input3");
+ line_view lv = load_file("../src/2019/day10/input");
auto p = aoc2019::day10(lv);
- // REQUIRE(334 == p.first);
- REQUIRE(0 == p.second);
+ REQUIRE(334 == p.first);
+ REQUIRE(1119 == p.second);
}