1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"
int text2array(int **dst, const char *r) {
int *a = malloc(sizeof *a);
size_t na = 0, sa = 1;
char *err;
int v;
for (;;) {
if (na == sa) {
int *tmp = realloc(a, (2*sa) * sizeof *tmp);
if (!tmp) exit(EXIT_FAILURE);
a = tmp;
sa *= 2;
}
v = strtol(r, &err, 10);
a[na++] = v;
if (!*err) break;
r = err;
}
*dst = a;
return na;
}
|
|
|
|
|
>
|
|
|
|
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
|
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"
size_t text2array(unsigned **dst, const char *r) {
unsigned *a = malloc(512 * sizeof *a);
size_t na = 0, sa = 512;
char *err;
unsigned v;
for (;;) {
if (na == sa) {
// 13/8 is within 0.5% of the golden ratio
unsigned *tmp = realloc(a, ((13*sa) / 8) * sizeof *tmp);
if (!tmp) exit(EXIT_FAILURE);
a = tmp;
sa = (13*sa) / 8;
}
v = strtoul(r, &err, 10);
a[na++] = v;
if (!*err) break;
r = err;
}
*dst = a;
return na;
}
|
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
int ch;
char *tmp = malloc(512);
size_t s = 512;
size_t r = 0;
while ((ch = fgetc(h)) != EOF) {
if (r == s) {
//grow tmp
char *ttmp = realloc(tmp, (13*s)/8); // 13/8 is within 0.5% of the golden ratio
if (ttmp) {
tmp = ttmp;
s = (13*s) / 8;
} else {
free(tmp);
return 0;
}
}
tmp[r++] = ch;
}
fclose(h);
*dst = tmp;
return r;
}
int max3(int a, int b, int c) {
if (a > b) {
if (a > c) return a;
return c;
} else {
if (b > c) return b;
}
return c;
}
double fmax3(double a, double b, double c) {
if (a > b) {
if (a > c) return a;
return c;
} else {
if (b > c) return b;
}
return c;
}
long max3l(long a, long b, long c) {
if (a > b) {
if (a > c) return a;
return c;
} else {
if (b > c) return b;
}
return c;
}
int min3(int a, int b, int c) {
if (a < b) {
if (a < c) return a;
return c;
} else {
if (b < c) return b;
}
return c;
}
double fmin3(double a, double b, double c) {
if (a < b) {
if (a < c) return a;
return c;
} else {
if (b < c) return b;
}
return c;
}
long min3l(long a, long b, long c) {
if (a < b) {
if (a < c) return a;
return c;
} else {
if (b < c) return b;
}
return c;
}
|
>
|
|
|
<
|
<
<
|
<
|
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
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
85
86
|
int ch;
char *tmp = malloc(512);
size_t s = 512;
size_t r = 0;
while ((ch = fgetc(h)) != EOF) {
if (r == s) {
//grow tmp
// 13/8 is within 0.5% of the golden ratio
char *ttmp = realloc(tmp, (13*s) / 8);
if (ttmp) {
tmp = ttmp;
s = (13*s) / 8;
} else {
free(tmp);
return 0;
}
}
tmp[r++] = ch;
}
fclose(h);
*dst = tmp;
return r;
}
unsigned distance(unsigned a, unsigned b) {
if (a > b) return a - b;
return b - a;
}
unsigned max3u(unsigned a, unsigned b, unsigned c) {
if (a > b) {
if (a > c) return a;
return c;
} else {
if (b > c) return b;
}
return c;
}
unsigned min3u(unsigned a, unsigned b, unsigned c) {
if (a < b) {
if (a < c) return a;
return c;
} else {
if (b < c) return b;
}
return c;
}
|