aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day2/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2017/day2/aoc.cpp')
-rw-r--r--src/2017/day2/aoc.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/2017/day2/aoc.cpp b/src/2017/day2/aoc.cpp
index ed2d9d3..9069bd3 100644
--- a/src/2017/day2/aoc.cpp
+++ b/src/2017/day2/aoc.cpp
@@ -2,4 +2,41 @@
namespace aoc2017 {
+int get_number(const char** pp) {
+ const char* p = *pp;
+ int d{0};
+ while (*p >= '0' && *p <= '9') {
+ d = d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+ return d;
}
+
+int diff(line_view lv) {
+ int max{INT_MIN};
+ int min{INT_MAX};
+ const char* p = lv.line;
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ int d = get_number(&p);
+ if (d > max)
+ max = d;
+ if (d < min)
+ min = d;
+ }
+ p++;
+ }
+ return max - min;
+}
+
+int day2(line_view file) {
+ int sum{0};
+ per_line(file, [&sum](line_view lv) {
+ sum += diff(lv);
+ return true;
+ });
+ return sum;
+}
+
+} // namespace aoc2017