summary refs log tree commit diff homepage
path: root/2020/day15.c
diff options
context:
space:
mode:
Diffstat (limited to '2020/day15.c')
-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);
 }