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
Diffstat (limited to '')
-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);
 }