summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-14 00:17:58 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commiteac0fdd9a186f060721157381bdbd59d9d9ddf1c (patch)
treeda8cff4dde4c60099cc64e2da334bd7acad2eb32 /2018
parentSolve day 13 part 2 (diff)
downloadaoc-eac0fdd9a186f060721157381bdbd59d9d9ddf1c.tar.gz
aoc-eac0fdd9a186f060721157381bdbd59d9d9ddf1c.zip
Solve day 14 part 1
Diffstat (limited to '2018')
-rw-r--r--2018/day14.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/2018/day14.c b/2018/day14.c
new file mode 100644
index 0000000..9df7190
--- /dev/null
+++ b/2018/day14.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef unsigned uint;
+
+struct Vec {
+	size_t cap, len;
+	uint *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, uint 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() {
+	uint count;
+	scanf("%u", &count);
+
+	struct Vec vec = new(256);
+	push(&vec, 3);
+	push(&vec, 7);
+
+	size_t elf[2] = { 0, 1 };
+	for (uint i = 0; i < count + 10; ++i) {
+		uint sum = vec.ptr[elf[0]] + vec.ptr[elf[1]];
+		if (sum / 10) push(&vec, sum / 10);
+		push(&vec, sum % 10);
+		elf[0] = (elf[0] + 1 + vec.ptr[elf[0]]) % vec.len;
+		elf[1] = (elf[1] + 1 + vec.ptr[elf[1]]) % vec.len;
+	}
+
+	for (uint i = count; i < count + 10; ++i) {
+		printf("%u", vec.ptr[i]);
+	}
+	printf("\n");
+}