diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2022-12-26 21:39:33 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2022-12-26 21:39:33 +0100 |
commit | 08f5e8a4664a2b54ba5bd827dbb744bcb48e6d51 (patch) | |
tree | f0f42a771f3bdf9ad4948c8854d95ba7254e68cb /06.c | |
parent | d2969bd72436e81a1c2d4c0ecb5d66cb6654fc89 (diff) |
Diffstat (limited to '06.c')
-rw-r--r-- | 06.c | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -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; +} |