summary refs log tree commit diff homepage
path: root/2020
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-13 00:13:32 -0500
committerJune McEnroe <june@causal.agency>2020-12-13 00:40:09 -0500
commitbed2dd20f640ccd4eab0a8ffbd5052f3593357b5 (patch)
treedae8405902a0af2b6ee95b774d54ecd690b38cbd /2020
parentSolve day 12 part 2 (diff)
downloadaoc-bed2dd20f640ccd4eab0a8ffbd5052f3593357b5.tar.gz
aoc-bed2dd20f640ccd4eab0a8ffbd5052f3593357b5.zip
Solve day 13 part 1
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);
+}