summaryrefslogtreecommitdiff
path: root/2022/day06.c
blob: 5e79b72fc20fa60935f2bb8cc4cddbc1b0d284c7 (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
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "util.h"

// char filename[] = "in/day06.ref";
char filename[] = "in/day06.pzl";

#define BUF_LEN 10000
u8 buf[BUF_LEN];
#define NAH_LEN 26
u8 not_a_hashmap[NAH_LEN];

s32 solve(u32 n)
{
	int fd = open(filename, O_RDONLY);
	u32 data_len = read(fd, buf, LEN(buf));
	if (data_len == LEN(buf)) {
		printf("buffer probably not big enough\n");
		exit(1);
	}

	for (u32 i = n; i < data_len; ++i) {
		memset(not_a_hashmap, 0x00, LEN(not_a_hashmap));

		for (u32 j = 1; j <= n; ++j) {
			u32 index = (buf[i-j]-'a');
			if (index >= NAH_LEN) {
				printf("nah too small\n");
				exit(1);
			}
			not_a_hashmap[index] = 1;
		}

		u32 sum = 0;
		for (u32 j = 0; j < NAH_LEN; ++j) {
			sum += not_a_hashmap[j];
		}
		if (sum == n) {
			return i;
		}

	}
	return -1;
}

int main()
{
	printf("res1: %d\n", solve(4));
	printf("res2: %d\n", solve(14));
}