-
Notifications
You must be signed in to change notification settings - Fork 2
/
13.c
84 lines (73 loc) · 1.94 KB
/
13.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <assert.h>
#include <err.h>
#include <stdint.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static const char* input =
"1002576\n"
"13,x,x,x,x,x,x,37,x,x,x,x,x,449,x,29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,"
"19,x,"
"x,x,23,x,x,x,x,x,x,x,773,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,17";
int day13(void) {
const char* s = input;
int ready_timestamp = 0;
while (*s != '\n') {
ready_timestamp = (ready_timestamp * 10) + (*s - '0');
s++;
}
s++; // Skip '\n'
// parse bus schedules from 2nd line
int buses[64] = {0};
int nbuses = 0;
while (*s != '\0') {
if (*s == 'x') {
buses[nbuses++] = 1;
s++;
} else {
int n = 0;
while (*s >= '0' && *s <= '9') {
n = (n * 10) + (*s - '0');
s++;
}
buses[nbuses++] = n;
}
if (*s == ',')
s++; // skip comma
}
// // find idx of highest bus ID (so we can use that to increment search
// step) int highest_bus_id_idx = 0; for (int i=0; i < nbuses; i++) {
// if (buses[i] > buses[highest_bus_id_idx]) {
// highest_bus_id_idx = i;
// }
// }
// int t2;
// for (unsigned long long t=buses[highest_bus_id_idx]-highest_bus_id_idx; t
// < ULLONG_MAX - buses[highest_bus_id_idx] - 1; t +=
// buses[highest_bus_id_idx])
// {
// for (i =0, t2=t; i < nbuses && (buses[i] == 1 || t2 % buses[i] == 0);
// i++, t2++);
// if (i >= nbuses - 1) {
// printf("t = %lld\n", t);
// break;
// }
// }
// Above solution takes several minutes... Had to look up Chinese remainder
// Theorem. https://en.wikipedia.org/wiki/Chinese_remainder_theorem
int64_t t = 0;
int64_t step = buses[0];
for (int b = 0; b < nbuses; b++) {
while (1) {
if ((t + b) % buses[b] == 0) {
step *= (int64_t) buses[b];
break;
}
t += step;
}
}
// Part 2
printf("%ld\n", t);
assert(t == 415579909629976);
return EXIT_SUCCESS;
}