aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-02-08 15:57:49 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-02-08 15:57:49 +0800
commitf16c630fad1481f7097b4c514739e15b2cddb334 (patch)
tree619f99381d584395bc824743dbbf9a47dd226253
parent6700ffdab424d6f238431efbd6094224f14c8379 (diff)
downloadadvent-of-code-f16c630fad1481f7097b4c514739e15b2cddb334.tar.gz
advent-of-code-f16c630fad1481f7097b4c514739e15b2cddb334.zip
2017 day12
-rw-r--r--src/2017/day12/README.md9
-rw-r--r--src/2017/day12/aoc.cpp68
-rw-r--r--src/2017/day13/README.md159
-rw-r--r--src/2017/day13/input43
-rw-r--r--src/2017/day13/input04
-rw-r--r--test/test_2017.cpp8
6 files changed, 286 insertions, 5 deletions
diff --git a/src/2017/day12/README.md b/src/2017/day12/README.md
index 831539b..18d02c1 100644
--- a/src/2017/day12/README.md
+++ b/src/2017/day12/README.md
@@ -29,3 +29,12 @@ Program 6 via programs 4, then 2.
Therefore, a total of 6 programs are in this group; all but program 1, which has a pipe that connects it to itself.
How many programs are in the group that contains program ID 0?
+
+--- Part Two ---
+There are more programs than just the ones in the group containing program ID 0. The rest of them have no way of reaching that group, and still might have no way of reaching each other.
+
+A group is a collection of programs that can all communicate via pipes either directly or indirectly. The programs you identified just a moment ago are all part of the same group. Now, they would like you to determine the total number of groups.
+
+In the example above, there were 2 groups: one consisting of programs 0,2,3,4,5,6, and the other consisting solely of program 1.
+
+How many groups are there in total?
diff --git a/src/2017/day12/aoc.cpp b/src/2017/day12/aoc.cpp
index 30f0501..0323e71 100644
--- a/src/2017/day12/aoc.cpp
+++ b/src/2017/day12/aoc.cpp
@@ -2,5 +2,71 @@
namespace aoc2017 {
-std::pair<int64_t, int64_t> day12(line_view) { return {0, 0}; }
+static void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+}
+
+struct record12 {
+ int ns[10] = {0};
+ bool connected = false;
+
+ void print() const noexcept {
+ for (auto n : ns) {
+ printf("%d ", n);
+ }
+ printf("\n");
+ }
+
+ record12(line_view lv) {
+ const char* p = lv.line;
+ int i{0};
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, &ns[i++]);
+ }
+ p++;
+ }
+ }
+};
+
+void mark_connected(size_t i, std::vector<record12>& rs, int* total) {
+ auto& r = rs[i];
+ if (!r.connected) {
+ *total += 1;
+ r.connected = true;
+ for (int x = 1; x < 10; x++) {
+ if (r.ns[x] > 0) {
+ mark_connected(r.ns[x], rs, total);
+ }
+ }
+ }
+}
+
+std::pair<int64_t, int64_t> day12(line_view file) {
+ std::vector<record12> rs;
+ per_line(file, [&rs](line_view lv) {
+ rs.emplace_back(lv);
+ return true;
+ });
+
+ int total{0};
+ mark_connected(0, rs, &total);
+ int t0 = total;
+ int t1 = 1;
+
+ for (size_t i = 1; i < rs.size() && total < (int)rs.size(); i++) {
+ int tx = total;
+ mark_connected(i, rs, &total);
+ if (total > tx) {
+ t1 += 1;
+ }
+ }
+
+ return {t0, t1};
+}
} // namespace aoc2017
diff --git a/src/2017/day13/README.md b/src/2017/day13/README.md
index e69de29..62c6429 100644
--- a/src/2017/day13/README.md
+++ b/src/2017/day13/README.md
@@ -0,0 +1,159 @@
+--- Day 13: Packet Scanners ---
+You need to cross a vast firewall. The firewall consists of several layers, each with a security scanner that moves back and forth across the layer. To succeed, you must not be detected by a scanner.
+
+By studying the firewall briefly, you are able to record (in your puzzle input) the depth of each layer and the range of the scanning area for the scanner within it, written as depth: range. Each layer has a thickness of exactly 1. A layer at depth 0 begins immediately inside the firewall; a layer at depth 1 would start immediately after that.
+
+For example, suppose you've recorded the following:
+
+0: 3
+1: 2
+4: 4
+6: 4
+This means that there is a layer immediately inside the firewall (with range 3), a second layer immediately after that (with range 2), a third layer which begins at depth 4 (with range 4), and a fourth layer which begins at depth 6 (also with range 4). Visually, it might look like this:
+
+ 0 1 2 3 4 5 6
+[ ] [ ] ... ... [ ] ... [ ]
+[ ] [ ] [ ] [ ]
+[ ] [ ] [ ]
+ [ ] [ ]
+Within each layer, a security scanner moves back and forth within its range. Each security scanner starts at the top and moves down until it reaches the bottom, then moves up until it reaches the top, and repeats. A security scanner takes one picosecond to move one step. Drawing scanners as S, the first few picoseconds look like this:
+
+
+Picosecond 0:
+ 0 1 2 3 4 5 6
+[S] [S] ... ... [S] ... [S]
+[ ] [ ] [ ] [ ]
+[ ] [ ] [ ]
+ [ ] [ ]
+
+Picosecond 1:
+ 0 1 2 3 4 5 6
+[ ] [ ] ... ... [ ] ... [ ]
+[S] [S] [S] [S]
+[ ] [ ] [ ]
+ [ ] [ ]
+
+Picosecond 2:
+ 0 1 2 3 4 5 6
+[ ] [S] ... ... [ ] ... [ ]
+[ ] [ ] [ ] [ ]
+[S] [S] [S]
+ [ ] [ ]
+
+Picosecond 3:
+ 0 1 2 3 4 5 6
+[ ] [ ] ... ... [ ] ... [ ]
+[S] [S] [ ] [ ]
+[ ] [ ] [ ]
+ [S] [S]
+Your plan is to hitch a ride on a packet about to move through the firewall. The packet will travel along the top of each layer, and it moves at one layer per picosecond. Each picosecond, the packet moves one layer forward (its first move takes it into layer 0), and then the scanners move one step. If there is a scanner at the top of the layer as your packet enters it, you are caught. (If a scanner moves into the top of its layer while you are there, you are not caught: it doesn't have time to notice you before you leave.) If you were to do this in the configuration above, marking your current position with parentheses, your passage through the firewall would look like this:
+
+Initial state:
+ 0 1 2 3 4 5 6
+[S] [S] ... ... [S] ... [S]
+[ ] [ ] [ ] [ ]
+[ ] [ ] [ ]
+ [ ] [ ]
+
+Picosecond 0:
+ 0 1 2 3 4 5 6
+(S) [S] ... ... [S] ... [S]
+[ ] [ ] [ ] [ ]
+[ ] [ ] [ ]
+ [ ] [ ]
+
+ 0 1 2 3 4 5 6
+( ) [ ] ... ... [ ] ... [ ]
+[S] [S] [S] [S]
+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[ ] [ ] ... ... [ ] ... ( )
+[S] [S] [S] [S]
+[ ] [ ] [ ]
+ [ ] [ ]
+In this situation, you are caught in layers 0 and 6, because your packet entered the layer when its scanner was at the top when you entered it. You are not caught in layer 1, since the scanner moved into the top of the layer once you were already there.
+
+The severity of getting caught on a layer is equal to its depth multiplied by its range. (Ignore layers in which you do not get caught.) The severity of the whole trip is the sum of these values. In the example above, the trip severity is 0*3 + 6*4 = 24.
+
+Given the details of the firewall you've recorded, if you leave immediately, what is the severity of your whole trip?
+
diff --git a/src/2017/day13/input b/src/2017/day13/input
index e69de29..a82183e 100644
--- a/src/2017/day13/input
+++ b/src/2017/day13/input
@@ -0,0 +1,43 @@
+0: 3
+1: 2
+2: 4
+4: 4
+6: 5
+8: 6
+10: 8
+12: 8
+14: 6
+16: 6
+18: 9
+20: 8
+22: 6
+24: 10
+26: 12
+28: 8
+30: 8
+32: 14
+34: 12
+36: 8
+38: 12
+40: 12
+42: 12
+44: 12
+46: 12
+48: 14
+50: 12
+52: 12
+54: 10
+56: 14
+58: 12
+60: 14
+62: 14
+64: 14
+66: 14
+68: 14
+70: 14
+72: 14
+74: 20
+78: 14
+80: 14
+90: 17
+96: 18
diff --git a/src/2017/day13/input0 b/src/2017/day13/input0
index e69de29..0239024 100644
--- a/src/2017/day13/input0
+++ b/src/2017/day13/input0
@@ -0,0 +1,4 @@
+0: 3
+1: 2
+4: 4
+6: 4
diff --git a/test/test_2017.cpp b/test/test_2017.cpp
index 4a5b194..57777b8 100644
--- a/test/test_2017.cpp
+++ b/test/test_2017.cpp
@@ -130,15 +130,15 @@ TEST_CASE("Hex Ed", "[2017]") {
}
-TEST_CASE("", "[2017]") {
+TEST_CASE("Digital Plumber", "[2017]") {
line_view lv = load_file("../src/2017/day12/input");
auto p = aoc2017::day12(lv);
- REQUIRE(0 == p.first);
- REQUIRE(0 == p.second);
+ REQUIRE(239 == p.first);
+ REQUIRE(215 == p.second);
}
-TEST_CASE("", "[2017]") {
+TEST_CASE("Packet Scanners", "[2017]") {
line_view lv = load_file("../src/2017/day13/input");
auto p = aoc2017::day13(lv);
REQUIRE(0 == p.first);