aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-15 11:15:02 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-15 11:15:02 +0800
commit1a768c1b8d69986c1cafcf5ead8a1f0424cefcbe (patch)
treee64e58553a1bb5ef6f502f6972efbb5efbd51389
parent869443933e5686433ef92fbcad7cd377017651b2 (diff)
downloadadvent-of-code-1a768c1b8d69986c1cafcf5ead8a1f0424cefcbe.tar.gz
advent-of-code-1a768c1b8d69986c1cafcf5ead8a1f0424cefcbe.zip
day2 part2
-rw-r--r--src/2015/day2/aoc.cpp20
-rw-r--r--src/2015/day2/aoc.h2
-rw-r--r--test/test_2015.cpp4
3 files changed, 19 insertions, 7 deletions
diff --git a/src/2015/day2/aoc.cpp b/src/2015/day2/aoc.cpp
index 33ae6b6..a23d9ad 100644
--- a/src/2015/day2/aoc.cpp
+++ b/src/2015/day2/aoc.cpp
@@ -35,23 +35,33 @@ box parse_day2(line_view lv) {
return b;
}
-inline int min_day2(box b) {
+inline int min_square(box b) {
int x1 = b.l * b.w;
int x2 = b.l * b.h;
int x3 = b.h * b.w;
return std::min(std::min(x1, x2), x3);
}
+inline int min_perimeter(box b) {
+ int x1 = b.l + b.w;
+ int x2 = b.l + b.h;
+ int x3 = b.h + b.w;
+ return 2 * std::min(std::min(x1, x2), x3);
+}
+
inline int surface(box b) { return 2 * b.l * b.w + 2 * b.l * b.h + 2 * b.w * b.h; }
+inline int squire(box b) { return b.l * b.w * b.h; }
-int day2(line_view input) {
+std::pair<int, int> day2(line_view input) {
int total = 0;
- per_line(input, [&total](line_view line) {
+ int ribbon = 0;
+ per_line(input, [&total, &ribbon](line_view line) {
box b = parse_day2(line);
- total += surface(b) + min_day2(b);
+ total += surface(b) + min_square(b);
+ ribbon += squire(b) + min_perimeter(b);
return true;
});
- return total;
+ return {total, ribbon};
}
} // namespace aoc2015
diff --git a/src/2015/day2/aoc.h b/src/2015/day2/aoc.h
index 72402cf..66fe384 100644
--- a/src/2015/day2/aoc.h
+++ b/src/2015/day2/aoc.h
@@ -13,6 +13,6 @@ struct box {
box parse_day2(line_view lv);
int min_day2(box);
int surface(box);
-int day2(line_view);
+std::pair<int, int> day2(line_view);
} // namespace aoc2015
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index 84ddcbe..32ab803 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -22,5 +22,7 @@ TEST_CASE("parse box", "[day2]") {
TEST_CASE("I Was Told There Would Be No Math", "[day2]") {
line_view lv = load_file("../src/2015/day2/input");
- printf("%d\n", aoc2015::day2(lv));
+ auto p = aoc2015::day2(lv);
+ REQUIRE(p.first == 1586300);
+ REQUIRE(p.second == 3737498);
}