day 11 part 1
[aoc_eblake.git] / 2017 / advent2.c
blobe326d3cff124bba8ff8d91d5355cfb8e2e1a83e8
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #if 0 // part 1
7 int main(void) {
8 ssize_t nread;
9 size_t len = 0;
10 char *line = NULL;
11 int sum = 0;
12 while ((nread = getline(&line, &len, stdin)) >= 0) {
13 unsigned int min = -1, max = 0;
14 char *token = strtok(line, " \t");
15 while (token) {
16 int value = atoi(token);
17 if (value < min)
18 min = value;
19 if (value > max)
20 max = value;
21 token = strtok(NULL, " \t");
23 sum += max - min;
25 printf("%d\n", sum);
28 #else // part 2
30 int main(void) {
31 ssize_t nread;
32 size_t len = 0;
33 char *line = NULL;
34 int sum = 0;
35 while ((nread = getline(&line, &len, stdin)) >= 0) {
36 int *array = calloc(sizeof(int), nread);
37 char *token = strtok(line, " \t");
38 int i = 0, j;
39 while (token) {
40 array[i++] = atoi(token);
41 token = strtok(NULL, " \t");
43 for (i = 0; i < nread; i++) {
44 if (!array[i])
45 continue;
46 for (j = 0; j < nread; j++) {
47 if (i == j || !array[j])
48 continue;
49 if (!(array[i] % array[j])) {
50 sum += array[i] / array[j];
51 printf("found %d/%d=%d\n", array[i], array[j], array[i]/array[j]);
52 goto done;
56 done:
59 printf("%d\n", sum);
62 #endif