blob: b62d0072fe0063b0b318a4d70da5396038247e2c (
plain)
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
|
#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]));
}
|