summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-15 00:49:30 -0500
committerJune McEnroe <june@causal.agency>2020-12-15 00:49:30 -0500
commit6ca56a0af155f867c00c7ded859bdc787bb4a3b2 (patch)
tree86999cdcb9bbe89644aa6eab1527cffdc0c93bfa
parentSolve day 15 part 1 (diff)
downloadaoc-6ca56a0af155f867c00c7ded859bdc787bb4a3b2.tar.gz
aoc-6ca56a0af155f867c00c7ded859bdc787bb4a3b2.zip
Solve day 15 part 2
-rw-r--r--2020/day15.c42
1 files changed, 28 insertions, 14 deletions
diff --git a/2020/day15.c b/2020/day15.c
index f5ee806..1a6a867 100644
--- a/2020/day15.c
+++ b/2020/day15.c
@@ -1,21 +1,35 @@
 #include <stdio.h>
 #include <stdlib.h>
 int main(void) {
-	int nums[2048];
-	int len = 0;
-	while (0 < scanf("%d,", &nums[len])) {
-		len++;
+	static int turns[30000000];
+	int turn = 1;
+	int last, next;
+	scanf("%d,", &last);
+	while (0 < scanf("%d,", &next)) {
+		turns[last] = turn;
+		last = next;
+		turn++;
 	}
-	while (len < 2020) {
-		int last = nums[len-1];
-		int next = 0;
-		for (int i = len - 2; i >= 0; --i) {
-			if (nums[i] == last) {
-				next = len - 1 - i;
-				break;
-			}
+	while (turn != 2020) {
+		if (turns[last]) {
+			next = turn - turns[last];
+		} else {
+			next = 0;
 		}
-		nums[len++] = next;
+		turns[last] = turn;
+		last = next;
+		turn++;
 	}
-	printf("%d\n", nums[len-1]);
+	printf("%d\n", last);
+	while (turn != 30000000) {
+		if (turns[last]) {
+			next = turn - turns[last];
+		} else {
+			next = 0;
+		}
+		turns[last] = turn;
+		last = next;
+		turn++;
+	}
+	printf("%d\n", last);
 }
21-01-19 23:38:11 -0500'>2021-01-19Prefer tag matches not preceded by [[:alnum:]]June McEnroe Otherwise the "id" in "void" matches for "void id". 2021-01-19Escape \ and / in mtags search patternsJune McEnroe 2021-01-20Use mtags in source-filterJune McEnroe 2021-01-19Add mtags to generate tags for make and mdocJune McEnroe 2021-01-19Map tags to IDs using only [[:alnum:]-._]June McEnroe 2021-01-19Don't use a pager if reading standard inputJune McEnroe 2021-01-19Support BSD make syntax and match *.amJune McEnroe These don't really go together, but... 2021-01-19Match tab following escaped newline in make assignmentsJune McEnroe Otherwise it ends up going into Shell state. 2021-01-18Allow matching lexers using first input lineJune McEnroe