summaryrefslogtreecommitdiff
path: root/06.c
diff options
context:
space:
mode:
Diffstat (limited to '06.c')
-rw-r--r--06.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/06.c b/06.c
new file mode 100644
index 0000000..d4bb7f5
--- /dev/null
+++ b/06.c
@@ -0,0 +1,51 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <err.h>
+#include "input.h"
+
+#define INPUT "input/06.txt"
+#define EXPECTED1 1598L
+#define EXPECTED2 2414L
+
+void find_marker(char* input, size_t marker_size, size_t expected) {
+ size_t pos;
+ size_t pos_max = strlen(input);
+ if(marker_size >= pos_max) {
+ err(2, "input is too short");
+ return;
+ }
+ size_t marker_size_loop = marker_size - 1;
+ for(pos = marker_size_loop; pos < pos_max; pos++) {
+ bool is_marker = true;
+ for(size_t i = pos; i > pos-marker_size_loop; i--) {
+ for(size_t j = i-1; j >= pos-marker_size_loop; j--) {
+ is_marker &= input[i] != input[j];
+ if(!is_marker)
+ break;
+ }
+ if(!is_marker)
+ break;
+ }
+ if(is_marker)
+ break;
+ }
+
+ pos++;
+ CHECK(pos, expected)
+}
+
+int main() {
+ // read input
+ struct input_str input;
+ input_str_read(&input, INPUT);
+
+ // do stuff
+ find_marker(input.lines[0], 4, EXPECTED1);
+ find_marker(input.lines[0], 14, EXPECTED2);
+
+ // cleanup & exit
+ input_str_free(&input);
+ return 0;
+}