summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2018/day12.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/2018/day12.c b/2018/day12.c
new file mode 100644
index 0000000..5f2f04e
--- /dev/null
+++ b/2018/day12.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned uint;
+
+enum { Zero = 128, Len = Zero * 2 };
+struct State {
+	char pots[Len];
+};
+
+struct Rule {
+	char pots[5];
+	char next;
+};
+
+int main() {
+	struct State state;
+	memset(state.pots, '.', Len);
+	scanf("initial state: %s\n\n", &state.pots[Zero]);
+	for (uint i = 0; i < Len; ++i) {
+		if (!state.pots[i]) state.pots[i] = '.';
+	}
+
+	uint rulen = 0;
+	struct Rule rules[32];
+	while (!feof(stdin)) {
+		scanf("%5c => %c\n", rules[rulen].pots, &rules[rulen].next);
+		rulen++;
+	}
+
+	for (uint g = 0; g < 20; ++g) {
+		struct State next;
+		memset(next.pots, '.', Len);
+		for (uint i = 2; i < Len - 2; ++i) {
+			for (uint r = 0; r < rulen; ++r) {
+				if (memcmp(&state.pots[i - 2], rules[r].pots, 5)) continue;
+				next.pots[i] = rules[r].next;
+				break;
+			}
+		}
+		state = next;
+	}
+
+	int sum = 0;
+	for (int i = 0; i < Len; ++i) {
+		if (state.pots[i] == '#') sum += i - Zero;
+	}
+	printf("%d\n", sum);
+}
ct Edit, use a global bufferJune McEnroe 2022-02-20Clear edit buffer before running commandJune McEnroe 2022-02-20Show indicator in status when window has pending inputJune McEnroe 2022-02-20Use separate edit buffers for each IDJune McEnroe 2022-02-20Make sure new cap is actually larger than new lengthJune McEnroe 2022-02-20Remove unused mbs.len field from struct EditJune McEnroe 2022-02-19Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 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 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe 2022-02-18Implement new line editing "library"June McEnroe 2022-02-18Simplify cursor positioning in inputJune McEnroe 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