diff options
| author | nekineki <nekineki@nekineki.net> | 2022-12-06 07:05:59 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2022-12-06 07:05:59 +0100 |
| commit | 35db791d785c7bf37b33f4f60a585a0f1137d236 (patch) | |
| tree | f76ebffbd849c481374738ae3dadd2ef87739a0a /2022 | |
| parent | 04698b4ade0a2588f0e380e0be0e43f9df375cd5 (diff) | |
day6 c
Diffstat (limited to '2022')
| -rw-r--r-- | 2022/day6/day6.c | 54 | ||||
| -rw-r--r-- | 2022/util.h | 26 |
2 files changed, 80 insertions, 0 deletions
diff --git a/2022/day6/day6.c b/2022/day6/day6.c new file mode 100644 index 0000000..13d5c04 --- /dev/null +++ b/2022/day6/day6.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h> +#include "../util.h" + +// char filename[] = "ref.txt"; +char filename[] = "pzl.txt"; + +#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)); +} + diff --git a/2022/util.h b/2022/util.h new file mode 100644 index 0000000..4bc6c2e --- /dev/null +++ b/2022/util.h @@ -0,0 +1,26 @@ +#ifndef UTIL_H +#define UTIL_H + +#include <stdint.h> + +#define NULL ((void *)0) +#define LEN(arr) (sizeof arr / sizeof arr[0]) +#define BIT(b) (1UL << b) + +typedef int8_t s8; +typedef uint8_t u8; + +typedef int16_t s16; +typedef uint16_t u16; + +typedef int32_t s32; +typedef uint32_t u32; + +typedef int64_t s64; +typedef uint64_t u64; + +typedef float f32; +typedef double f64; + +#endif /* UTIL_H */ + |
