diff options
-rw-r--r-- | src/2015/day2/aoc.cpp | 20 | ||||
-rw-r--r-- | src/2015/day2/aoc.h | 2 | ||||
-rw-r--r-- | test/test_2015.cpp | 4 |
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); } |