summaryrefslogtreecommitdiff
path: root/06.c
blob: d4bb7f5c95bea4f9c705c9c657c23c625bacc48a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
}