aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day5/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2017/day5/aoc.cpp')
-rw-r--r--src/2017/day5/aoc.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/2017/day5/aoc.cpp b/src/2017/day5/aoc.cpp
index ed2d9d3..a89a18e 100644
--- a/src/2017/day5/aoc.cpp
+++ b/src/2017/day5/aoc.cpp
@@ -1,5 +1,44 @@
#include "aoc.h"
+#include <vector>
namespace aoc2017 {
+int get_number(line_view lv) {
+ const char* p = lv.line;
+ int sign = 1;
+ if (*p == '-') {
+ sign = -1;
+ p++;
+ }
+ int d{0};
+ while (*p >= '0' && *p <= '9') {
+ d = d * 10 + *p - '0';
+ p++;
+ }
+ return d * sign;
}
+
+void follow(int i, std::vector<int>& ins, int* steps) {
+ // printf("%d\n", ins[i]);
+ int x = ins[i];
+ int n = i + x;
+ if (n >= 0 && n < int(ins.size())) {
+ // ins[i] += x >= 3 ? -1 : 1; part2
+ ins[i] += 1;
+ *steps += 1;
+ follow(n, ins, steps);
+ }
+}
+
+int day5(line_view file) {
+ std::vector<int> ins;
+ per_line(file, [&ins](line_view lv) {
+ ins.emplace_back(get_number(lv));
+ return true;
+ });
+ int steps{0};
+ follow(0, ins, &steps);
+ return steps + 1;
+}
+
+} // namespace aoc2017