aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day2/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-15 10:49:19 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-15 10:49:19 +0800
commit869443933e5686433ef92fbcad7cd377017651b2 (patch)
tree432a2d4311ac3a3dff0406bbd2410d690adc8395 /src/2015/day2/aoc.cpp
parent8c0bc49502ee9101d3f761cd791ebe770012d26d (diff)
downloadadvent-of-code-869443933e5686433ef92fbcad7cd377017651b2.tar.gz
advent-of-code-869443933e5686433ef92fbcad7cd377017651b2.zip
day2 part1
Diffstat (limited to 'src/2015/day2/aoc.cpp')
-rw-r--r--src/2015/day2/aoc.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/2015/day2/aoc.cpp b/src/2015/day2/aoc.cpp
new file mode 100644
index 0000000..33ae6b6
--- /dev/null
+++ b/src/2015/day2/aoc.cpp
@@ -0,0 +1,57 @@
+#include "aoc.h"
+
+namespace aoc2015 {
+
+int cal_day2(const char* p1, const char* p2) {
+ int x = 0;
+ const char* p = p1;
+ while (p != p2) {
+ x = x * 10 + (*p) - '0';
+ p++;
+ }
+ return x;
+}
+
+/*
+27x2x5
+29x13x26
+*/
+box parse_day2(line_view lv) {
+ box b;
+ int* is[] = {&b.l, &b.w, &b.h};
+ const char* p1 = lv.line;
+ const char* p2 = p1;
+ const char* pe = p1 + lv.length;
+ int i = 0;
+ while (p2 != pe) {
+ if ((*p2) >= '0' && (*p2) <= '9') {
+ p2++;
+ } else {
+ *is[i++] = cal_day2(p1, p2);
+ p1 = p2 + 1;
+ p2 = p1;
+ }
+ }
+ return b;
+}
+
+inline int min_day2(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 surface(box b) { return 2 * b.l * b.w + 2 * b.l * b.h + 2 * b.w * b.h; }
+
+int day2(line_view input) {
+ int total = 0;
+ per_line(input, [&total](line_view line) {
+ box b = parse_day2(line);
+ total += surface(b) + min_day2(b);
+ return true;
+ });
+ return total;
+}
+
+} // namespace aoc2015