summary refs log tree commit diff homepage
path: root/2018/day14.c
blob: 9d2bdd91dfd8448dc54a940b75b6d32fe263cb0e (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Vec {
	size_t cap, len;
	char *ptr;
};
static struct Vec new(size_t cap) {
	struct Vec vec = { cap };
	vec.ptr = malloc(sizeof(*vec.ptr) * cap);
	return vec;
}
static void push(struct Vec *vec, char val) {
	if (vec->len == vec->cap) {
		vec->cap *= 2;
		vec->ptr = realloc(vec->ptr, sizeof(*vec->ptr) * vec->cap);
	}
	vec->ptr[vec->len++] = val;
}

int main() {
	size_t count;
	scanf("%zu", &count);
	char *scores;
	asprintf(&scores, "%zu", count);

	struct Vec vec = new(256);
	push(&vec, '3');
	push(&vec, '7');

	size_t elf[2] = { 0, 1 };
	for (size_t i = 0; 1; ++i) {
		if (i == count + 10) {
			for (size_t j = count; j < count + 10; ++j) {
				printf("%c", vec.ptr[j]);
			}
			printf("\n");
		}
		char sum = (vec.ptr[elf[0]] - '0') + (vec.ptr[elf[1]] - '0');
		if (sum / 10) push(&vec, '0' + sum / 10);
		push(&vec, '0' + sum % 10);
		elf[0] = (elf[0] + 1 + vec.ptr[elf[0]] - '0') % vec.len;
		elf[1] = (elf[1] + 1 + vec.ptr[elf[1]] - '0') % vec.len;
		size_t len = strlen(scores);
		if (vec.len < len) continue;
		char *match = strnstr(&vec.ptr[vec.len - len - 1], scores, len + 1);
		if (!match) continue;
		printf("%zu\n", match - vec.ptr);
		break;
	}
}
span title='2022-02-19 20:44:36 -0500'>2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe In other words, warn when a function is missing static. I don't see why this isn't in -Wextra. 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe I know, it feels wrong. 2022-02-18Implement new line editing "library"June McEnroe Losing tab complete and text macros, for now. This new implementation works on an instance of a struct and does not interact with the rest of catgirl, making it possible to copy into another project. Unlike existing line editing libraries, this one is entirely abstract and can be rendered externally. My goal with this library is to be able to implement vi mode. Since it operates on struct instances rather than globals, it might also be possible to give catgirl separate line editing buffers for each window, which would be a nice UX improvement. 2022-02-18Simplify cursor positioning in inputJune McEnroe Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe