diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-02-13 14:32:16 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-02-13 14:32:16 +0800 |
commit | 51acfc18dfd36fe4eb018a2626893c0e8401ab49 (patch) | |
tree | 9e54f5bb4f6982a4d7ae90bb6daf5c9d6da801d6 | |
parent | 18e9b3dd35e16cb2393ffac02b4c88be89274273 (diff) | |
download | advent-of-code-51acfc18dfd36fe4eb018a2626893c0e8401ab49.tar.gz advent-of-code-51acfc18dfd36fe4eb018a2626893c0e8401ab49.zip |
2017 day19
-rw-r--r-- | src/2017/day19/aoc.cpp | 8 | ||||
-rw-r--r-- | src/2017/day20/README.md | 32 | ||||
-rw-r--r-- | test/test_2017.cpp | 4 |
3 files changed, 39 insertions, 5 deletions
diff --git a/src/2017/day19/aoc.cpp b/src/2017/day19/aoc.cpp index 9336fdb..a9511f4 100644 --- a/src/2017/day19/aoc.cpp +++ b/src/2017/day19/aoc.cpp @@ -46,7 +46,7 @@ static heading next(heading h, alpha_grid& g) { return next(h); } -static void move(heading h, alpha_grid& g, std::string& alpha) { +static void move(heading h, alpha_grid& g, std::string& alpha, int* count) { while (true) { char c = g.get(h.p); if (c == ' ') @@ -55,6 +55,7 @@ static void move(heading h, alpha_grid& g, std::string& alpha) { alpha.push_back(c); } h = next(h, g); + *count += 1; } } @@ -69,7 +70,8 @@ std::pair<std::string, int64_t> day19(line_view file) { heading start = {g.start, direction::down}; std::string alpha{""}; - move(start, g, alpha); - return {alpha, 0}; + int count{0}; + move(start, g, alpha, &count); + return {alpha, count}; } } // namespace aoc2017 diff --git a/src/2017/day20/README.md b/src/2017/day20/README.md index e69de29..4e930f2 100644 --- a/src/2017/day20/README.md +++ b/src/2017/day20/README.md @@ -0,0 +1,32 @@ +--- Day 20: Particle Swarm --- +Suddenly, the GPU contacts you, asking for help. Someone has asked it to simulate too many particles, and it won't be able to finish them all in time to render the next frame at this rate. + +It transmits to you a buffer (your puzzle input) listing each particle in order (starting with particle 0, then particle 1, particle 2, and so on). For each particle, it provides the X, Y, and Z coordinates for the particle's position (p), velocity (v), and acceleration (a), each in the format <X,Y,Z>. + +Each tick, all particles are updated simultaneously. A particle's properties are updated in the following order: + +Increase the X velocity by the X acceleration. +Increase the Y velocity by the Y acceleration. +Increase the Z velocity by the Z acceleration. +Increase the X position by the X velocity. +Increase the Y position by the Y velocity. +Increase the Z position by the Z velocity. +Because of seemingly tenuous rationale involving z-buffering, the GPU would like to know which particle will stay closest to position <0,0,0> in the long term. Measure this using the Manhattan distance, which in this situation is simply the sum of the absolute values of a particle's X, Y, and Z position. + +For example, suppose you are only given two particles, both of which stay entirely on the X-axis (for simplicity). Drawing the current states of particles 0 and 1 (in that order) with an adjacent a number line and diagram of current X positions (marked in parentheses), the following would take place: + +p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4 +p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0> (0)(1) + +p=< 4,0,0>, v=< 1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4 +p=< 2,0,0>, v=<-2,0,0>, a=<-2,0,0> (1) (0) + +p=< 4,0,0>, v=< 0,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4 +p=<-2,0,0>, v=<-4,0,0>, a=<-2,0,0> (1) (0) + +p=< 3,0,0>, v=<-1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4 +p=<-8,0,0>, v=<-6,0,0>, a=<-2,0,0> (0) +At this point, particle 1 will never be closer to <0,0,0> than particle 0, and so, in the long run, particle 0 will stay closest. + +Which particle will stay closest to position <0,0,0> in the long term? + diff --git a/test/test_2017.cpp b/test/test_2017.cpp index bce71ef..975096a 100644 --- a/test/test_2017.cpp +++ b/test/test_2017.cpp @@ -190,11 +190,11 @@ TEST_CASE("A Series of Tubes", "[2017]") { line_view lv = load_file("../src/2017/day19/input"); auto p = aoc2017::day19(lv); REQUIRE("PVBSCMEQHY" == p.first); - REQUIRE(0 == p.second); + REQUIRE(17736 == p.second); } -TEST_CASE("", "[2017]") { +TEST_CASE("Particle Swarm", "[2017]") { line_view lv = load_file("../src/2017/day20/input"); auto p = aoc2017::day20(lv); REQUIRE(0 == p.first); |