aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day6/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2022/day6/aoc.cpp')
-rw-r--r--src/2022/day6/aoc.cpp51
1 files changed, 50 insertions, 1 deletions
diff --git a/src/2022/day6/aoc.cpp b/src/2022/day6/aoc.cpp
index 6c53b41..6fd3159 100644
--- a/src/2022/day6/aoc.cpp
+++ b/src/2022/day6/aoc.cpp
@@ -1,8 +1,57 @@
#include "aoc.h"
namespace aoc2022 {
+
+int next4(const char* p) {
+ char cs[4] = {0};
+ for(int i = 0; i < 4; i++) {
+ cs[i] = *(p + i);
+ }
+ if (cs[0] == cs[1]) return 1;
+ if (cs[0] == cs[2]) return 1;
+ if (cs[0] == cs[3]) return 1;
+ if (cs[1] == cs[2]) return 2;
+ if (cs[1] == cs[3]) return 2;
+ if (cs[2] == cs[3]) return 3;
+ return 0;
+}
+
+int exam(int x, char cs[], int s) {
+ if (x == s) {
+ return 0;
+ }
+ for (int i = x + 1; i < s; i++) {
+ if (cs[x] == cs[i]) {
+ return x + 1;
+ }
+ }
+ return exam(x+1, cs, s);
+}
+
+int next14(const char* p) {
+ char cs[14] = {0};
+ for(int i = 0; i < 14; i++) {
+ cs[i] = *(p + i);
+ }
+ return exam(0, cs, 14);
+}
+
std::pair<int,int> day6(line_view file) {
- return {0,0};
+ const char *p0 = file.line;
+ int d0 = next4(p0);
+ while (d0 != 0) {
+ p0 += d0;
+ d0 = next4(p0);
+ }
+
+ const char *p1 = file.line;
+ int d1 = next14(p1);
+ while (d1 != 0) {
+ p1 += d1;
+ d1 = next14(p1);
+ }
+
+ return {p0 - file.line + 4 , p1 - file.line + 14};
}
}