summaryrefslogtreecommitdiff
path: root/13
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2021-11-28 17:58:30 +0100
committerBond_009 <bond.009@outlook.com>2021-11-28 17:58:30 +0100
commit49d0c908f24b2c193c9deed1716fe36061ba26a1 (patch)
treef7ee07f77315d6eec24fe72eb33c69840d52cd77 /13
parent4ecc8a5baf2662dbb20071a96d8ac30180a6a0bc (diff)
Add day 13 part 1
Diffstat (limited to '13')
-rw-r--r--13/input2
-rw-r--r--13/part1.c48
-rw-r--r--13/testinput2
3 files changed, 52 insertions, 0 deletions
diff --git a/13/input b/13/input
new file mode 100644
index 0000000..f9503fe
--- /dev/null
+++ b/13/input
@@ -0,0 +1,2 @@
+1005526
+37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,587,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,13,19,x,x,x,23,x,x,x,x,x,29,x,733,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17
diff --git a/13/part1.c b/13/part1.c
new file mode 100644
index 0000000..b62d007
--- /dev/null
+++ b/13/part1.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define BUFFER_SIZE 256
+#define MAX_INPUT_LEN 128
+
+int solve(const char *filename)
+{
+ FILE *file = fopen(filename, "r");
+
+ char buffer[BUFFER_SIZE] = { 0 };
+ fgets(buffer, BUFFER_SIZE, file);
+ int depart = atoi(buffer);
+ fgets(buffer, BUFFER_SIZE, file);
+ int bus_ids[MAX_INPUT_LEN] = { 0 };
+ bus_ids[0] = atoi(buffer);
+ int i = 1;
+ char *p = buffer;
+ while ((p = strchr(p, ','))) {
+ // x is never the last value, just keep looping we find a valid input
+ while (*++p == 'x') {
+ p += 1;
+ }
+
+ bus_ids[i++] = atoi(p);
+ }
+
+ int bus_id = 0;
+ int wait = INT32_MAX;
+ for (int j = 0; j < i; j++) {
+ int id = bus_ids[j];
+ int tmprem = id - (depart % id);
+ if (tmprem < wait) {
+ bus_id = id;
+ wait = tmprem;
+ }
+ }
+
+ fclose(file);
+ return bus_id * wait;
+}
+
+int main(int argc, char *argv[])
+{
+ printf("%i\n", solve(argv[argc - 1]));
+}
diff --git a/13/testinput b/13/testinput
new file mode 100644
index 0000000..d76f619
--- /dev/null
+++ b/13/testinput
@@ -0,0 +1,2 @@
+939
+7,13,x,x,59,x,31,19