aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-04 13:31:56 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-04 13:31:56 +0800
commitbaf9117b3f11a03f21f37a5abb3ef105dff4652f (patch)
tree737df399dec0b68cf32eec0abb05b1dc76ef252a /src
parentf69e238bec1b742fcb17709ab16f6ccf3d4ed514 (diff)
downloadadvent-of-code-baf9117b3f11a03f21f37a5abb3ef105dff4652f.tar.gz
advent-of-code-baf9117b3f11a03f21f37a5abb3ef105dff4652f.zip
2022 day4
Diffstat (limited to 'src')
-rw-r--r--src/2022/day4/README.md15
-rw-r--r--src/2022/day4/aoc.cpp10
-rw-r--r--src/2022/day4/aoc.h44
3 files changed, 68 insertions, 1 deletions
diff --git a/src/2022/day4/README.md b/src/2022/day4/README.md
index 2085dc7..7816c12 100644
--- a/src/2022/day4/README.md
+++ b/src/2022/day4/README.md
@@ -42,3 +42,18 @@ This example list uses single-digit section IDs to make it easier to draw; your
Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.
In how many assignment pairs does one range fully contain the other?
+
+--- Part Two ---
+
+It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.
+
+In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:
+
+ 5-7,7-9 overlaps in a single section, 7.
+ 2-8,3-7 overlaps all of the sections 3 through 7.
+ 6-6,4-6 overlaps in a single section, 6.
+ 2-6,4-8 overlaps in sections 4, 5, and 6.
+
+So, in this example, the number of overlapping assignment pairs is 4.
+
+In how many assignment pairs do the ranges overlap?
diff --git a/src/2022/day4/aoc.cpp b/src/2022/day4/aoc.cpp
index 535a5df..0ba53e2 100644
--- a/src/2022/day4/aoc.cpp
+++ b/src/2022/day4/aoc.cpp
@@ -4,7 +4,15 @@
namespace aoc2022 {
std::pair<int, int> day4(line_view file) {
- return {0, 0};
+ int c1{0}, c2{0};
+ per_line(file, [&c1, &c2](line_view lv){
+ apair p;
+ p.load(lv);
+ c1 += (int) p.covered();
+ c2 += (int) p.overlap();
+ return true;
+ });
+ return {c1, c2};
}
} // namespace aoc2022
diff --git a/src/2022/day4/aoc.h b/src/2022/day4/aoc.h
index 3b8a1ec..b291262 100644
--- a/src/2022/day4/aoc.h
+++ b/src/2022/day4/aoc.h
@@ -3,6 +3,50 @@
namespace aoc2022 {
+struct apair {
+ int first[2] = {0};
+ int second[2] = {0};
+
+ void get_number(int* d, int i, const char* p) {
+ *(d + i) = *(d + i) * 10 + *p - '0';
+ }
+
+ void load(line_view lv) {
+ int* d = first;
+ int i{0};
+ const char* p0 = lv.line;
+ while (p0 < lv.line + lv.length) {
+ if (*p0 >= '0' && *p0 <= '9') {
+ get_number(d, i, p0);
+ }
+ else {
+ if (*p0 == ',') {
+ d = second;
+ i = 0;
+ }
+ if (*p0 == '-') {
+ i += 1;
+ }
+ }
+ p0++;
+ }
+ }
+
+ bool covered() const noexcept {
+ bool b1 = first[0] <= second[0] && first[1] >= second[1];
+ bool b2 = second[0] <= first[0] && second[1] >= first[1];
+ return b1 || b2;
+ }
+
+ bool overlap() const noexcept {
+ bool b1 = second[0] >= first[0] && second[0] <= first[1];
+ bool b2 = second[1] >= first[0] && second[1] <= first[1];
+ bool b3 = first[0] >= second[0] && first[0] <= second[1];
+ bool b4 = first[1] >= second[0] && first[1] <= second[1];
+ return covered() || b1 || b2 || b3 || b4;
+ }
+};
+
std::pair<int, int> day4(line_view file);
}