summary refs log tree commit diff homepage
path: root/2020
diff options
context:
space:
mode:
Diffstat (limited to '2020')
-rw-r--r--2020/day13.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/2020/day13.c b/2020/day13.c
new file mode 100644
index 0000000..f8c0665
--- /dev/null
+++ b/2020/day13.c
@@ -0,0 +1,26 @@
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+int main(void) {
+	int earliest = 0;
+	scanf("%d\n", &earliest);
+	int buses[128] = {0};
+	int len = 0;
+	for (;;) {
+		int n = scanf("%d,", &buses[len]);
+		if (!n) n = scanf("x,");
+		if (n < 0) break;
+		len++;
+	}
+	int min = INT_MAX;
+	int bus = 0;
+	for (int i = 0; i < len; ++i) {
+		if (!buses[i]) continue;
+		int wait = (earliest / buses[i] + 1) * buses[i] - earliest;
+		if (wait < min) {
+			min = wait;
+			bus = buses[i];
+		}
+	}
+	printf("%d\n", bus * min);
+}