diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-25 17:56:34 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-25 17:56:34 +0100 |
| commit | 94d0033ed89bb5bfb6500051296892fb7cea6c29 (patch) | |
| tree | 245eac2218310c958c2df2fad5109ea4f7b2a9d9 /2023 | |
| parent | 6a480c4eb9c96c82a8fd3663f0fbee4d32e56f19 (diff) | |
| download | aoc-94d0033ed89bb5bfb6500051296892fb7cea6c29.tar.gz aoc-94d0033ed89bb5bfb6500051296892fb7cea6c29.zip | |
Small cleanup
Diffstat (limited to '2023')
41 files changed, 100 insertions, 239 deletions
diff --git a/2023/01/1a.c b/2023/01/1a.c index 0983236..229df20 100644 --- a/2023/01/1a.c +++ b/2023/01/1a.c | |||
| @@ -9,8 +9,7 @@ int main() { | |||
| 9 | sum = 0; | 9 | sum = 0; |
| 10 | while ((buf = fgets(line, N, stdin)) != NULL) { | 10 | while ((buf = fgets(line, N, stdin)) != NULL) { |
| 11 | for (first = -1; *buf; buf++) { | 11 | for (first = -1; *buf; buf++) { |
| 12 | if (*buf < '0' || *buf > '9') | 12 | if (*buf < '0' || *buf > '9') continue; |
| 13 | continue; | ||
| 14 | first = first == -1 ? *buf - '0' : first; | 13 | first = first == -1 ? *buf - '0' : first; |
| 15 | last = *buf - '0'; | 14 | last = *buf - '0'; |
| 16 | } | 15 | } |
diff --git a/2023/01/1b.c b/2023/01/1b.c index adc3da8..795571b 100644 --- a/2023/01/1b.c +++ b/2023/01/1b.c | |||
| @@ -16,8 +16,7 @@ int main() { | |||
| 16 | for (j = 1; j < 10; j++) | 16 | for (j = 1; j < 10; j++) |
| 17 | if (!strncmp(buf, nums[j], strlen(nums[j]))) | 17 | if (!strncmp(buf, nums[j], strlen(nums[j]))) |
| 18 | *buf = j + '0'; | 18 | *buf = j + '0'; |
| 19 | if (*buf < '0' || *buf > '9') | 19 | if (*buf < '0' || *buf > '9') continue; |
| 20 | continue; | ||
| 21 | first = first == -1 ? *buf - '0' : first; | 20 | first = first == -1 ? *buf - '0' : first; |
| 22 | last = *buf - '0'; | 21 | last = *buf - '0'; |
| 23 | } | 22 | } |
diff --git a/2023/02/2a.c b/2023/02/2a.c index f3ddfc8..3268cff 100644 --- a/2023/02/2a.c +++ b/2023/02/2a.c | |||
| @@ -1,11 +1,8 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | ||
| 5 | 3 | ||
| 6 | #define N 10000 | 4 | #define N 10000 |
| 7 | 5 | #define MAX(x,y) ((x)>(y)?(x):(y)) | |
| 8 | #define MAX(x, y) ((x)>(y) ? (x) : (y)) | ||
| 9 | 6 | ||
| 10 | int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 }; | 7 | int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 }; |
| 11 | 8 | ||
| @@ -17,11 +14,9 @@ int main() { | |||
| 17 | maxc[0] = maxc[1] = maxc[2] = 0; | 14 | maxc[0] = maxc[1] = maxc[2] = 0; |
| 18 | while (*buf++ != ':'); | 15 | while (*buf++ != ':'); |
| 19 | while (*buf++) { | 16 | while (*buf++) { |
| 20 | if (*buf < '0' || *buf > '9') | 17 | if (*buf < '0' || *buf > '9') continue; |
| 21 | continue; | ||
| 22 | x = atoi(buf); | 18 | x = atoi(buf); |
| 23 | while (*buf < 'a' || *buf > 'z') | 19 | while (*buf < 'a' || *buf > 'z') buf++; |
| 24 | buf++; | ||
| 25 | maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x); | 20 | maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x); |
| 26 | } | 21 | } |
| 27 | if (maxc[0] <= 12 && maxc[1] <= 13 && maxc[2] <= 14) | 22 | if (maxc[0] <= 12 && maxc[1] <= 13 && maxc[2] <= 14) |
diff --git a/2023/02/2b.c b/2023/02/2b.c index 1b30114..9d7f357 100644 --- a/2023/02/2b.c +++ b/2023/02/2b.c | |||
| @@ -1,11 +1,8 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | ||
| 5 | 3 | ||
| 6 | #define N 10000 | 4 | #define N 10000 |
| 7 | 5 | #define MAX(x,y) ((x)>(y)?(x):(y)) | |
| 8 | #define MAX(x, y) ((x)>(y) ? (x) : (y)) | ||
| 9 | 6 | ||
| 10 | int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 }; | 7 | int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 }; |
| 11 | 8 | ||
| @@ -18,11 +15,9 @@ int main() { | |||
| 18 | maxc[0] = maxc[1] = maxc[2] = 0; | 15 | maxc[0] = maxc[1] = maxc[2] = 0; |
| 19 | while (*buf++ != ':'); | 16 | while (*buf++ != ':'); |
| 20 | while (*buf++) { | 17 | while (*buf++) { |
| 21 | if (*buf < '0' || *buf > '9') | 18 | if (*buf < '0' || *buf > '9') continue; |
| 22 | continue; | ||
| 23 | x = atoi(buf); | 19 | x = atoi(buf); |
| 24 | while (*buf < 'a' || *buf > 'z') | 20 | while (*buf < 'a' || *buf > 'z') buf++; |
| 25 | buf++; | ||
| 26 | maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x); | 21 | maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x); |
| 27 | } | 22 | } |
| 28 | sum += maxc[0] * maxc[1] * maxc[2]; | 23 | sum += maxc[0] * maxc[1] * maxc[2]; |
diff --git a/2023/03/3a.c b/2023/03/3a.c index 13c8c8d..1d07a8e 100644 --- a/2023/03/3a.c +++ b/2023/03/3a.c | |||
| @@ -1,30 +1,23 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 1000 | 5 | #define N 1000 |
| 7 | 6 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 8 | bool isnum(char c) { return c >= '0' && c <= '9'; } | 7 | #define ISSYM(c) (c != '.' && c != 0 && c != '\n' && !ISNUM(c)) |
| 9 | bool issymbol(char c) { return c != '.' && c != 0 && c != '\n' && !isnum(c); } | ||
| 10 | 8 | ||
| 11 | int grab(char *buf) { | 9 | int grab(char *buf) { |
| 12 | int x; | 10 | if (!ISNUM(*buf)) return 0; |
| 13 | 11 | while (ISNUM(*buf)) buf--; | |
| 14 | if (!isnum(*buf)) | ||
| 15 | return 0; | ||
| 16 | |||
| 17 | while (isnum(*buf)) buf--; | ||
| 18 | buf++; | 12 | buf++; |
| 19 | x = atoi(buf); | 13 | int x = atoi(buf); |
| 20 | while (isnum(*buf)) *buf++ = '.'; | 14 | while (ISNUM(*buf)) *buf++ = '.'; |
| 21 | 15 | ||
| 22 | return x; | 16 | return x; |
| 23 | } | 17 | } |
| 24 | 18 | ||
| 25 | int main() { | 19 | int main() { |
| 26 | char line[N], prev[N]; | 20 | char line[N], prev[N]; |
| 27 | bool l, c, r, ul, uc, ur; | ||
| 28 | int i, sum; | 21 | int i, sum; |
| 29 | 22 | ||
| 30 | sum = 0; | 23 | sum = 0; |
| @@ -32,15 +25,10 @@ int main() { | |||
| 32 | memset(prev, '.', N); | 25 | memset(prev, '.', N); |
| 33 | while (fgets(&line[1], N-1, stdin) != NULL) { | 26 | while (fgets(&line[1], N-1, stdin) != NULL) { |
| 34 | for (i = 1; line[i]; i++) { | 27 | for (i = 1; line[i]; i++) { |
| 35 | l = issymbol(line[i-1]); | 28 | if (ISSYM(line[i-1]) || ISSYM(line[i+1]) || |
| 36 | c = issymbol(line[i]); | 29 | ISSYM(prev[i-1]) || ISSYM(prev[i]) || ISSYM(prev[i+1])) |
| 37 | r = issymbol(line[i+1]); | ||
| 38 | ul = issymbol(prev[i-1]); | ||
| 39 | uc = issymbol(prev[i]); | ||
| 40 | ur = issymbol(prev[i+1]); | ||
| 41 | if (l || r || ul || uc || ur) | ||
| 42 | sum += grab(&line[i]); | 30 | sum += grab(&line[i]); |
| 43 | if (l || c || r) | 31 | if (ISSYM(line[i-1]) || ISSYM(line[i]) || ISSYM(line[i+1])) |
| 44 | sum += grab(&prev[i]); | 32 | sum += grab(&prev[i]); |
| 45 | } | 33 | } |
| 46 | memcpy(prev, line, N); | 34 | memcpy(prev, line, N); |
diff --git a/2023/03/3b.c b/2023/03/3b.c index 27b5b7c..9bf4c27 100644 --- a/2023/03/3b.c +++ b/2023/03/3b.c | |||
| @@ -1,22 +1,16 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 1000 | 5 | #define N 1000 |
| 7 | 6 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 8 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 9 | 7 | ||
| 10 | int grab(char *buf) { | 8 | int grab(char *buf) { |
| 11 | int x; | 9 | if (!ISNUM(*buf)) return 0; |
| 12 | 10 | while (ISNUM(*buf)) buf--; | |
| 13 | if (!isnum(*buf)) | ||
| 14 | return 0; | ||
| 15 | |||
| 16 | while (isnum(*buf)) buf--; | ||
| 17 | buf++; | 11 | buf++; |
| 18 | x = atoi(buf); | 12 | int x = atoi(buf); |
| 19 | while (isnum(*buf)) *buf++ = '.'; | 13 | while (ISNUM(*buf)) *buf++ = '.'; |
| 20 | 14 | ||
| 21 | return x; | 15 | return x; |
| 22 | } | 16 | } |
diff --git a/2023/04/4a.c b/2023/04/4a.c index a1f6a9e..30b7b41 100644 --- a/2023/04/4a.c +++ b/2023/04/4a.c | |||
| @@ -1,11 +1,8 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | ||
| 5 | 3 | ||
| 6 | #define N 1000 | 4 | #define N 1000 |
| 7 | 5 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 8 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 9 | 6 | ||
| 10 | int main() { | 7 | int main() { |
| 11 | char *buf, line[N]; | 8 | char *buf, line[N]; |
| @@ -17,15 +14,15 @@ int main() { | |||
| 17 | while (*buf != ':') buf++; | 14 | while (*buf != ':') buf++; |
| 18 | while (*buf != '|') { | 15 | while (*buf != '|') { |
| 19 | buf++; | 16 | buf++; |
| 20 | if (!isnum(*buf)) continue; | 17 | if (!ISNUM(*buf)) continue; |
| 21 | w[nw++] = atoi(buf); | 18 | w[nw++] = atoi(buf); |
| 22 | while (isnum(*buf)) buf++; | 19 | while (ISNUM(*buf)) buf++; |
| 23 | } | 20 | } |
| 24 | while (*buf != '\n') { | 21 | while (*buf != '\n') { |
| 25 | buf++; | 22 | buf++; |
| 26 | if (!isnum(*buf)) continue; | 23 | if (!ISNUM(*buf)) continue; |
| 27 | r[nr++] = atoi(buf); | 24 | r[nr++] = atoi(buf); |
| 28 | while (isnum(*buf)) buf++; | 25 | while (ISNUM(*buf)) buf++; |
| 29 | } | 26 | } |
| 30 | for (x = 0, i = 0; i < nr; i++) { | 27 | for (x = 0, i = 0; i < nr; i++) { |
| 31 | for (j = 0; j < nw; j++) { | 28 | for (j = 0; j < nw; j++) { |
diff --git a/2023/04/4b.c b/2023/04/4b.c index c122f8b..4529ff7 100644 --- a/2023/04/4b.c +++ b/2023/04/4b.c | |||
| @@ -1,11 +1,9 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 1000 | 5 | #define N 1000 |
| 7 | 6 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 8 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 9 | 7 | ||
| 10 | int main() { | 8 | int main() { |
| 11 | char *buf, line[N]; | 9 | char *buf, line[N]; |
| @@ -19,15 +17,15 @@ int main() { | |||
| 19 | while (*buf != ':') buf++; | 17 | while (*buf != ':') buf++; |
| 20 | while (*buf != '|') { | 18 | while (*buf != '|') { |
| 21 | buf++; | 19 | buf++; |
| 22 | if (!isnum(*buf)) continue; | 20 | if (!ISNUM(*buf)) continue; |
| 23 | w[nw++] = atoi(buf); | 21 | w[nw++] = atoi(buf); |
| 24 | while (isnum(*buf)) buf++; | 22 | while (ISNUM(*buf)) buf++; |
| 25 | } | 23 | } |
| 26 | while (*buf != '\n') { | 24 | while (*buf != '\n') { |
| 27 | buf++; | 25 | buf++; |
| 28 | if (!isnum(*buf)) continue; | 26 | if (!ISNUM(*buf)) continue; |
| 29 | r[nr++] = atoi(buf); | 27 | r[nr++] = atoi(buf); |
| 30 | while (isnum(*buf)) buf++; | 28 | while (ISNUM(*buf)) buf++; |
| 31 | } | 29 | } |
| 32 | for (x = 0, i = 0; i < nr; i++) { | 30 | for (x = 0, i = 0; i < nr; i++) { |
| 33 | for (j = 0; j < nw; j++) { | 31 | for (j = 0; j < nw; j++) { |
diff --git a/2023/05/5a.c b/2023/05/5a.c index 15d2805..14b8c7c 100644 --- a/2023/05/5a.c +++ b/2023/05/5a.c | |||
| @@ -1,26 +1,25 @@ | |||
| 1 | #include <inttypes.h> | 1 | #include <inttypes.h> |
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdio.h> | 2 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 5 | #include <string.h> | 4 | #include <string.h> |
| 6 | 5 | ||
| 7 | #define N 100 | 6 | #define N 100 |
| 8 | 7 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 9 | bool isnum(char c) { return c >= '0' && c <= '9'; } | 8 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
| 10 | 9 | ||
| 11 | int main() { | 10 | int main() { |
| 12 | char *buf, line[N]; | 11 | char *buf, line[N]; |
| 13 | int64_t i, m, ns, seed[N], next[N], r[3]; | 12 | int64_t i, m, ns, seed[N], next[N], r[3]; |
| 14 | 13 | ||
| 15 | for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) { | 14 | for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) { |
| 16 | if (!isnum(*buf)) continue; | 15 | if (!ISNUM(*buf)) continue; |
| 17 | next[ns++] = atoll(buf); | 16 | next[ns++] = atoll(buf); |
| 18 | while (isnum(*buf)) buf++; | 17 | while (ISNUM(*buf)) buf++; |
| 19 | } | 18 | } |
| 20 | 19 | ||
| 21 | 20 | ||
| 22 | while ((buf = fgets(line, N, stdin)) != NULL) { | 21 | while ((buf = fgets(line, N, stdin)) != NULL) { |
| 23 | if (!isnum(*buf)) { | 22 | if (!ISNUM(*buf)) { |
| 24 | memcpy(seed, next, ns * sizeof(int64_t)); | 23 | memcpy(seed, next, ns * sizeof(int64_t)); |
| 25 | fgets(line, N, stdin); /* Discard description */ | 24 | fgets(line, N, stdin); /* Discard description */ |
| 26 | continue; | 25 | continue; |
| @@ -28,7 +27,7 @@ int main() { | |||
| 28 | 27 | ||
| 29 | for (i = 0; *buf; buf++) { | 28 | for (i = 0; *buf; buf++) { |
| 30 | r[i++] = atoll(buf); | 29 | r[i++] = atoll(buf); |
| 31 | while (isnum(*buf)) buf++; | 30 | while (ISNUM(*buf)) buf++; |
| 32 | } | 31 | } |
| 33 | 32 | ||
| 34 | for (i = 0; i < ns; i++) | 33 | for (i = 0; i < ns; i++) |
| @@ -36,9 +35,8 @@ int main() { | |||
| 36 | next[i] = seed[i] + (r[0] - r[1]); | 35 | next[i] = seed[i] + (r[0] - r[1]); |
| 37 | } | 36 | } |
| 38 | 37 | ||
| 39 | m = next[0]; | 38 | for (i = 1, m = next[0]; i < ns; i++) |
| 40 | for (i = 1; i < ns; i++) | 39 | m = MIN(m, next[i]); |
| 41 | m = m > next[i] ? next[i] : m; | ||
| 42 | 40 | ||
| 43 | printf("%" PRId64 "\n", m); | 41 | printf("%" PRId64 "\n", m); |
| 44 | return 0; | 42 | return 0; |
diff --git a/2023/05/5b.c b/2023/05/5b.c index aeac8c0..c59f61b 100644 --- a/2023/05/5b.c +++ b/2023/05/5b.c | |||
| @@ -1,17 +1,15 @@ | |||
| 1 | /* For part 2 we save ranges as (first, last) instead of (first, length) */ | 1 | /* For part 2 we save ranges as (first, last) instead of (first, length) */ |
| 2 | 2 | ||
| 3 | #include <inttypes.h> | 3 | #include <inttypes.h> |
| 4 | #include <stdbool.h> | ||
| 5 | #include <stdio.h> | 4 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 5 | #include <stdlib.h> |
| 7 | #include <string.h> | 6 | #include <string.h> |
| 8 | 7 | ||
| 9 | #define N 10000 | 8 | #define N 10000 |
| 10 | 9 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 11 | #define MIN(a, b) ((a)<(b)?(a):(b)) | 10 | #define MIN(a, b) ((a)<(b)?(a):(b)) |
| 12 | #define MAX(a, b) ((a)>(b)?(a):(b)) | 11 | #define MAX(a, b) ((a)>(b)?(a):(b)) |
| 13 | 12 | ||
| 14 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 15 | 13 | ||
| 16 | void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { | 14 | void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { |
| 17 | for (int i = 0; i < *nnr; i++) { | 15 | for (int i = 0; i < *nnr; i++) { |
| @@ -25,9 +23,9 @@ void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { | |||
| 25 | int64_t readl(int64_t nums[], char *buf) { | 23 | int64_t readl(int64_t nums[], char *buf) { |
| 26 | int64_t i; | 24 | int64_t i; |
| 27 | for (i = 0; *buf; buf++) { | 25 | for (i = 0; *buf; buf++) { |
| 28 | if (!isnum(*buf)) continue; | 26 | if (!ISNUM(*buf)) continue; |
| 29 | nums[i++] = atoll(buf); | 27 | nums[i++] = atoll(buf); |
| 30 | while (isnum(*buf)) buf++; | 28 | while (ISNUM(*buf)) buf++; |
| 31 | } | 29 | } |
| 32 | return i; | 30 | return i; |
| 33 | } | 31 | } |
| @@ -43,7 +41,7 @@ int main() { | |||
| 43 | } | 41 | } |
| 44 | 42 | ||
| 45 | for (nr = 0; (buf = fgets(line, N, stdin)) != NULL; ) { | 43 | for (nr = 0; (buf = fgets(line, N, stdin)) != NULL; ) { |
| 46 | if (!isnum(*buf)) { | 44 | if (!ISNUM(*buf)) { |
| 47 | append(range, next, &nr, &nnr); | 45 | append(range, next, &nr, &nnr); |
| 48 | continue; | 46 | continue; |
| 49 | } | 47 | } |
diff --git a/2023/06/6a.c b/2023/06/6a.c index 25a85e5..b3c0ba1 100644 --- a/2023/06/6a.c +++ b/2023/06/6a.c | |||
| @@ -2,30 +2,24 @@ | |||
| 2 | 2 | ||
| 3 | #include <inttypes.h> | 3 | #include <inttypes.h> |
| 4 | #include <math.h> | 4 | #include <math.h> |
| 5 | #include <stdbool.h> | ||
| 6 | #include <stdio.h> | 5 | #include <stdio.h> |
| 7 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 8 | #include <string.h> | ||
| 9 | 7 | ||
| 10 | #define N 100000 | 8 | #define N 100000 |
| 11 | 9 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 12 | #define MIN(a, b) ((a)<(b)?(a):(b)) | ||
| 13 | #define MAX(a, b) ((a)>(b)?(a):(b)) | ||
| 14 | |||
| 15 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 16 | 10 | ||
| 17 | int64_t readl(int64_t nums[], char *buf) { | 11 | int64_t readl(int64_t nums[], char *buf) { |
| 18 | int64_t i; | 12 | int64_t i; |
| 19 | for (i = 0; *buf; buf++) { | 13 | for (i = 0; *buf; buf++) { |
| 20 | if (!isnum(*buf)) continue; | 14 | if (!ISNUM(*buf)) continue; |
| 21 | nums[i++] = atoll(buf); | 15 | nums[i++] = atoll(buf); |
| 22 | while (isnum(*buf)) buf++; | 16 | while (ISNUM(*buf)) buf++; |
| 23 | } | 17 | } |
| 24 | return i; | 18 | return i; |
| 25 | } | 19 | } |
| 26 | 20 | ||
| 27 | int main() { | 21 | int main() { |
| 28 | char line[N], clean[N]; | 22 | char line[N]; |
| 29 | int64_t i, n, p, D, x1, x2, t[N], d[N]; | 23 | int64_t i, n, p, D, x1, x2, t[N], d[N]; |
| 30 | 24 | ||
| 31 | n = readl(t, fgets(line, N, stdin)); | 25 | n = readl(t, fgets(line, N, stdin)); |
diff --git a/2023/07/7a.c b/2023/07/7a.c index ffc9b5c..7cc4958 100644 --- a/2023/07/7a.c +++ b/2023/07/7a.c | |||
| @@ -11,18 +11,15 @@ int value[255] = { | |||
| 11 | ['T'] = 10, ['J'] = 11, ['Q'] = 12, ['K'] = 13, ['A'] = 14 | 11 | ['T'] = 10, ['J'] = 11, ['Q'] = 12, ['K'] = 13, ['A'] = 14 |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| 14 | typedef struct { | 14 | typedef struct { char c[5]; int64_t b; } hand_t; |
| 15 | char c[5]; | ||
| 16 | int64_t b; | ||
| 17 | } hand_t; | ||
| 18 | 15 | ||
| 19 | int strength(hand_t h) { | 16 | int strength(hand_t h) { |
| 20 | int i, c2, c1, s, x[15]; | 17 | int i, c2, c1, s, x[15]; |
| 21 | 18 | ||
| 22 | memset(x, 0, 15 * sizeof(int)); | 19 | memset(x, 0, 15 * sizeof(int)); |
| 23 | for (i = 0, s = 0; i < 5; i++) { | 20 | for (i = 0, s = 0; i < 5; i++) { |
| 24 | s = s * 15 + value[h.c[i]]; | 21 | s = s * 15 + value[(int)h.c[i]]; |
| 25 | x[value[h.c[i]]]++; | 22 | x[value[(int)h.c[i]]]++; |
| 26 | } | 23 | } |
| 27 | for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { | 24 | for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { |
| 28 | if (c1 < x[i]) { | 25 | if (c1 < x[i]) { |
diff --git a/2023/07/7b.c b/2023/07/7b.c index 58716b3..be3e9e8 100644 --- a/2023/07/7b.c +++ b/2023/07/7b.c | |||
| @@ -11,18 +11,15 @@ int value[255] = { | |||
| 11 | ['T'] = 10, ['J'] = 1, ['Q'] = 12, ['K'] = 13, ['A'] = 14 | 11 | ['T'] = 10, ['J'] = 1, ['Q'] = 12, ['K'] = 13, ['A'] = 14 |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| 14 | typedef struct { | 14 | typedef struct { char c[5]; int64_t b; } hand_t; |
| 15 | char c[5]; | ||
| 16 | int64_t b; | ||
| 17 | } hand_t; | ||
| 18 | 15 | ||
| 19 | int strength(hand_t h) { | 16 | int strength(hand_t h) { |
| 20 | int i, c2, c1, s, x[15]; | 17 | int i, c2, c1, s, x[15]; |
| 21 | 18 | ||
| 22 | memset(x, 0, 15 * sizeof(int)); | 19 | memset(x, 0, 15 * sizeof(int)); |
| 23 | for (i = 0, s = 0; i < 5; i++) { | 20 | for (i = 0, s = 0; i < 5; i++) { |
| 24 | s = s * 15 + value[h.c[i]]; | 21 | s = s * 15 + value[(int)h.c[i]]; |
| 25 | x[value[h.c[i]]]++; | 22 | x[value[(int)h.c[i]]]++; |
| 26 | } | 23 | } |
| 27 | for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { | 24 | for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { |
| 28 | if (c1 < x[i]) { | 25 | if (c1 < x[i]) { |
diff --git a/2023/08/8a.c b/2023/08/8a.c index 62f6fcd..f6d58b5 100644 --- a/2023/08/8a.c +++ b/2023/08/8a.c | |||
| @@ -1,17 +1,13 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <math.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stdio.h> | 1 | #include <stdio.h> |
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | 2 | #include <string.h> |
| 7 | 3 | ||
| 8 | #define N 30000 | 4 | #define N 30000 |
| 9 | 5 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 10 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 11 | int index(char *c) { return c[0]-'A' + (c[1]-'A' + (c[2]-'A')*26)*26; } | ||
| 12 | 6 | ||
| 13 | int map[N][2]; | 7 | int map[N][2]; |
| 14 | 8 | ||
| 9 | int index(char *c) { return c[0]-'A' + (c[1]-'A' + (c[2]-'A')*26)*26; } | ||
| 10 | |||
| 15 | int main() { | 11 | int main() { |
| 16 | char *buf, dir[N], line[N]; | 12 | char *buf, dir[N], line[N]; |
| 17 | int n, i, s; | 13 | int n, i, s; |
diff --git a/2023/08/8b.c b/2023/08/8b.c index 713bada..f704a36 100644 --- a/2023/08/8b.c +++ b/2023/08/8b.c | |||
| @@ -1,74 +1,50 @@ | |||
| 1 | /* | 1 | /* |
| 2 | I hate this stupid problem. If you read the description carefully (which is | 2 | I hate this stupid problem. There are a bunch of unwritten properties |
| 3 | what I did), you would think that the problem is much, much harder than it | 3 | of the paths that make the problem very simple: |
| 4 | actually is. In reality, the author made many assumptions about the possible | ||
| 5 | paths that made is super easy. Let's see a few: | ||
| 6 | 4 | ||
| 7 | - Different ghost's path do not end up in the same Z-node (this is not super | 5 | - Different ghosts' paths do not end up in the same Z-node. |
| 8 | important for the solution). | 6 | - Each ghost has exactly one Z-node in its path. |
| 9 | - Each ghost has exactly one Z-node in its path (one could think about this | 7 | - Each ghost meets a Z-node exactly once before entering a loop. |
| 10 | when reading the strange remark that "there are as many A-nodes as Z-nodes", | ||
| 11 | which does not look important at first). | ||
| 12 | - Each ghosts meets a Z-node exactly once before entering a loop (this is | ||
| 13 | a fundamental and incredibly strong assupmtion). | ||
| 14 | - If a ghost encounters a Z-node after X steps, it will encounter it exactly | 8 | - If a ghost encounters a Z-node after X steps, it will encounter it exactly |
| 15 | every X steps (like WTF I don't even have to solve a system of congruences? | 9 | every X steps; preperiods just end up aligning nicely. |
| 16 | what is this, a problem for babies?) | ||
| 17 | 10 | ||
| 18 | Fuck. | 11 | In practice, everything just works out so that the lcm of the periods |
| 19 | 12 | is the solution, which is very much not the case for a general input. | |
| 20 | This code does not even solve the problem by the way, it computes some data | 13 | This code computes the periods of the paths of the different ghosts. Then |
| 21 | about the path that each ghost takes. Then you can figure out the solution | 14 | you can figure out the solution with a pocket calculator, or by hand. |
| 22 | with a pocket calculator, or by hand. | ||
| 23 | */ | 15 | */ |
| 24 | #include <inttypes.h> | 16 | |
| 25 | #include <math.h> | ||
| 26 | #include <stdbool.h> | ||
| 27 | #include <stdio.h> | 17 | #include <stdio.h> |
| 28 | #include <stdlib.h> | ||
| 29 | #include <string.h> | 18 | #include <string.h> |
| 30 | 19 | ||
| 31 | #define N 1000 | 20 | #define N 1000 |
| 32 | 21 | ||
| 33 | typedef struct { char last; int next[2]; } node_t; | 22 | typedef struct { char last; int next[2]; } node_t; |
| 34 | typedef struct { int preplen, plen, nz, z[N], zid[N]; } source_t; | 23 | |
| 24 | int v[N*N]; | ||
| 35 | 25 | ||
| 36 | int ind(char s[3], char m[][3], int n) { | 26 | int ind(char s[3], char m[][3], int n) { |
| 37 | for (int j = 0; j < n; j++) | 27 | for (int j = 0; j < n; j++) |
| 38 | if (s[0] == m[j][0] && s[1] == m[j][1] && s[2] == m[j][2]) | 28 | if (s[0] == m[j][0] && s[1] == m[j][1] && s[2] == m[j][2]) |
| 39 | return j; | 29 | return j; |
| 40 | |||
| 41 | return -1; | 30 | return -1; |
| 42 | } | 31 | } |
| 43 | 32 | ||
| 44 | int v[N*N]; | 33 | int findperiod(int i, node_t *nodes, char *dir, int n, int k) { |
| 45 | source_t worksource(int i, node_t *nodes, char *dir, int n, int k) { | ||
| 46 | source_t s; | ||
| 47 | int state; | ||
| 48 | |||
| 49 | printf("Working source %d\n", i); | ||
| 50 | |||
| 51 | memset(v, 0, sizeof(int) * N*N); | 34 | memset(v, 0, sizeof(int) * N*N); |
| 52 | for (int j = i, d = 0, l = 1; true; d = (d+1)%k) { | 35 | for (int j = i, d = 0, l = 1, state = -1; ; d = (d+1)%k) { |
| 53 | j = nodes[j].next[dir[d] == 'R']; | 36 | j = nodes[j].next[dir[d] == 'R']; |
| 54 | state = j*k+d; | 37 | state = j*k+d; |
| 55 | if (nodes[j].last == 'Z') printf("Found Z %d at %d\n", j, l); | 38 | if (v[state]) return l - v[state]; |
| 56 | if (v[state]) { | 39 | else v[state] = l++; |
| 57 | s.preplen = v[state]-1; | ||
| 58 | s.plen = l - v[state]; | ||
| 59 | printf("Stopping at %d. Preperiod: %d, period: %d\n", l, s.preplen, s.plen); | ||
| 60 | return s; | ||
| 61 | } else v[state] = l++; | ||
| 62 | } | 40 | } |
| 63 | 41 | return -1; | |
| 64 | return s; | ||
| 65 | } | 42 | } |
| 66 | 43 | ||
| 67 | int main() { | 44 | int main() { |
| 68 | node_t nodes[N]; | 45 | node_t nodes[N]; |
| 69 | source_t src[N]; | ||
| 70 | char *buf, dir[N], line[N], name[N][3], lstr[N][3], rstr[N][3]; | 46 | char *buf, dir[N], line[N], name[N][3], lstr[N][3], rstr[N][3]; |
| 71 | int k, i, n, m, s; | 47 | int k, n; |
| 72 | 48 | ||
| 73 | k = strlen(fgets(dir, N, stdin)) - 1; | 49 | k = strlen(fgets(dir, N, stdin)) - 1; |
| 74 | fgets(line, N, stdin); | 50 | fgets(line, N, stdin); |
| @@ -81,16 +57,14 @@ int main() { | |||
| 81 | memcpy(rstr[n], buf+1, 3); | 57 | memcpy(rstr[n], buf+1, 3); |
| 82 | } | 58 | } |
| 83 | 59 | ||
| 84 | for (i = 0; i < n; i++) { | 60 | for (int i = 0; i < n; i++) { |
| 85 | nodes[i].next[0] = ind(lstr[i], name, n); | 61 | nodes[i].next[0] = ind(lstr[i], name, n); |
| 86 | nodes[i].next[1] = ind(rstr[i], name, n); | 62 | nodes[i].next[1] = ind(rstr[i], name, n); |
| 87 | } | 63 | } |
| 88 | 64 | ||
| 89 | printf("%d %d\n", n, k); | 65 | for (int i = 0; i < n; i++) |
| 90 | |||
| 91 | for (i = 0, m = 0; i < n; i++) | ||
| 92 | if (nodes[i].last == 'A') | 66 | if (nodes[i].last == 'A') |
| 93 | src[m++] = worksource(i, nodes, dir, n, k); | 67 | printf("%d\n", findperiod(i, nodes, dir, n, k)); |
| 94 | 68 | ||
| 95 | return 0; | 69 | return 0; |
| 96 | } | 70 | } |
diff --git a/2023/09/9a.c b/2023/09/9a.c index 10b7beb..741c344 100644 --- a/2023/09/9a.c +++ b/2023/09/9a.c | |||
| @@ -2,7 +2,6 @@ | |||
| 2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 5 | #include <string.h> | ||
| 6 | 5 | ||
| 7 | #define N 1000 | 6 | #define N 1000 |
| 8 | 7 | ||
diff --git a/2023/09/9b.c b/2023/09/9b.c index 1b539c1..f04e9a6 100644 --- a/2023/09/9b.c +++ b/2023/09/9b.c | |||
| @@ -2,7 +2,6 @@ | |||
| 2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 5 | #include <string.h> | ||
| 6 | 5 | ||
| 7 | #define N 1000 | 6 | #define N 1000 |
| 8 | 7 | ||
diff --git a/2023/10/10a.c b/2023/10/10a.c index d25b0c8..884a8b0 100644 --- a/2023/10/10a.c +++ b/2023/10/10a.c | |||
| @@ -1,7 +1,4 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 2 | ||
| 6 | #define N 1000 | 3 | #define N 1000 |
| 7 | 4 | ||
diff --git a/2023/10/10b.c b/2023/10/10b.c index 0b298dc..d63a4b0 100644 --- a/2023/10/10b.c +++ b/2023/10/10b.c | |||
| @@ -1,7 +1,4 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 2 | ||
| 6 | #define N 1000 | 3 | #define N 1000 |
| 7 | #define set(i, j, c) if (newmap[i][j] == '.') newmap[i][j] = c; | 4 | #define set(i, j, c) if (newmap[i][j] == '.') newmap[i][j] = c; |
diff --git a/2023/11/11a.c b/2023/11/11a.c index 3b7f8fb..323438e 100644 --- a/2023/11/11a.c +++ b/2023/11/11a.c | |||
| @@ -1,7 +1,5 @@ | |||
| 1 | #include <inttypes.h> | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 3 | ||
| 6 | #define N 1000 | 4 | #define N 1000 |
| 7 | 5 | ||
diff --git a/2023/11/11b.c b/2023/11/11b.c index edb0fd6..2ec104c 100644 --- a/2023/11/11b.c +++ b/2023/11/11b.c | |||
| @@ -1,7 +1,5 @@ | |||
| 1 | #include <inttypes.h> | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 3 | ||
| 6 | #define N 1000 | 4 | #define N 1000 |
| 7 | 5 | ||
diff --git a/2023/12/12a.c b/2023/12/12a.c index d88ed9a..129746f 100644 --- a/2023/12/12a.c +++ b/2023/12/12a.c | |||
| @@ -1,14 +1,10 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdio.h> | 1 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 5 | #include <string.h> | 3 | #include <string.h> |
| 6 | 4 | ||
| 7 | #define N 1000 | 5 | #define N 1000 |
| 8 | 6 | ||
| 9 | #define MAX(a,b) ((a)>(b)?(a):(b)) | 7 | #define ABS(a) ((a)>0?(a):-(a)) |
| 10 | #define MIN(a,b) ((a)<(b)?(a):(b)) | ||
| 11 | #define ABS(a) MAX((a),-(a)) | ||
| 12 | 8 | ||
| 13 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) | 9 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) |
| 14 | 10 | ||
| @@ -16,7 +12,7 @@ char *buf, line[N]; | |||
| 16 | int s, n, a[N]; | 12 | int s, n, a[N]; |
| 17 | 13 | ||
| 18 | int readl(int nums[], char *buf) { | 14 | int readl(int nums[], char *buf) { |
| 19 | int64_t i; | 15 | int i; |
| 20 | for (i = 0; *buf; buf++) { | 16 | for (i = 0; *buf; buf++) { |
| 21 | if (!isnum(*buf)) continue; | 17 | if (!isnum(*buf)) continue; |
| 22 | nums[i++] = atoll(buf); | 18 | nums[i++] = atoll(buf); |
diff --git a/2023/13/13a.c b/2023/13/13a.c index c7c0b3a..273b31d 100644 --- a/2023/13/13a.c +++ b/2023/13/13a.c | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #include <inttypes.h> | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 64 | 5 | #define N 64 |
diff --git a/2023/13/13b.c b/2023/13/13b.c index 090a895..37e1822 100644 --- a/2023/13/13b.c +++ b/2023/13/13b.c | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #include <inttypes.h> | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 64 | 5 | #define N 64 |
diff --git a/2023/15/15b.c b/2023/15/15b.c index 15b97f9..bfdafe4 100644 --- a/2023/15/15b.c +++ b/2023/15/15b.c | |||
| @@ -12,8 +12,7 @@ int main() { | |||
| 12 | for (b = line, c = 0; *b != '\n'; b++) { | 12 | for (b = line, c = 0; *b != '\n'; b++) { |
| 13 | switch (*b) { | 13 | switch (*b) { |
| 14 | case ',': | 14 | case ',': |
| 15 | c = 0; | 15 | c = l = 0; |
| 16 | l = 0; | ||
| 17 | break; | 16 | break; |
| 18 | case '=': | 17 | case '=': |
| 19 | int64_t i; | 18 | int64_t i; |
diff --git a/2023/16/16a.c b/2023/16/16a.c index 0f95553..79aba38 100644 --- a/2023/16/16a.c +++ b/2023/16/16a.c | |||
| @@ -1,7 +1,4 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 2 | ||
| 6 | #define M 200 | 3 | #define M 200 |
| 7 | 4 | ||
| @@ -25,7 +22,7 @@ void walk(int i, int j, int d) { | |||
| 25 | if (!entered[i][j]) s++; | 22 | if (!entered[i][j]) s++; |
| 26 | entered[i][j] |= d; | 23 | entered[i][j] |= d; |
| 27 | for (int k = 1; k <= 8; k <<= 1) | 24 | for (int k = 1; k <= 8; k <<= 1) |
| 28 | if (k & turn[d][map[i][j]]) | 25 | if (k & turn[d][(int)map[i][j]]) |
| 29 | walk(i+go[k][0], j+go[k][1], k); | 26 | walk(i+go[k][0], j+go[k][1], k); |
| 30 | } | 27 | } |
| 31 | 28 | ||
diff --git a/2023/16/16b.c b/2023/16/16b.c index ce42c31..6247942 100644 --- a/2023/16/16b.c +++ b/2023/16/16b.c | |||
| @@ -1,6 +1,4 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | 2 | #include <string.h> |
| 5 | 3 | ||
| 6 | #define M 200 | 4 | #define M 200 |
| @@ -26,7 +24,7 @@ void walk(int i, int j, int d) { | |||
| 26 | if (!entered[i][j]) s++; | 24 | if (!entered[i][j]) s++; |
| 27 | entered[i][j] |= d; | 25 | entered[i][j] |= d; |
| 28 | for (int k = 1; k <= 8; k <<= 1) | 26 | for (int k = 1; k <= 8; k <<= 1) |
| 29 | if (k & turn[d][map[i][j]]) | 27 | if (k & turn[d][(int)map[i][j]]) |
| 30 | walk(i+go[k][0], j+go[k][1], k); | 28 | walk(i+go[k][0], j+go[k][1], k); |
| 31 | } | 29 | } |
| 32 | 30 | ||
diff --git a/2023/17/17a.c b/2023/17/17a.c index 0b02f45..9b025a4 100644 --- a/2023/17/17a.c +++ b/2023/17/17a.c | |||
| @@ -1,7 +1,4 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 2 | ||
| 6 | #define M 150 | 3 | #define M 150 |
| 7 | #define MAXE 1000000 | 4 | #define MAXE 1000000 |
diff --git a/2023/17/17b.c b/2023/17/17b.c index 10fad81..51e1636 100644 --- a/2023/17/17b.c +++ b/2023/17/17b.c | |||
| @@ -1,7 +1,4 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | 2 | ||
| 6 | #define M 150 | 3 | #define M 150 |
| 7 | #define MAXE 1000000 | 4 | #define MAXE 1000000 |
diff --git a/2023/18/18b.c b/2023/18/18b.c index d79d4cb..7b16309 100644 --- a/2023/18/18b.c +++ b/2023/18/18b.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | I have lost my code for part one. It reused some stuff from day 10. | 2 | I have lost my code for part one. It reused some stuff from day |
| 3 | This code can be adjusted to work for part one, just change the input reading. | 3 | 10. This code can be adjusted to work for part one, just change the |
| 4 | input reading part. | ||
| 4 | */ | 5 | */ |
| 5 | 6 | ||
| 6 | #include <inttypes.h> | 7 | #include <inttypes.h> |
| @@ -57,10 +58,6 @@ int64_t overlaplen(int64_t a[], int64_t na, int64_t b[], int64_t nb) { | |||
| 57 | return ret; | 58 | return ret; |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | int64_t linelen(int64_t a[], int64_t n) { | ||
| 61 | return overlaplen(a, n, a, n); | ||
| 62 | } | ||
| 63 | |||
| 64 | int main() { | 61 | int main() { |
| 65 | p[n++] = (point_t) {0}; | 62 | p[n++] = (point_t) {0}; |
| 66 | while ((buf = fgets(in, 50, stdin)) != NULL) { | 63 | while ((buf = fgets(in, 50, stdin)) != NULL) { |
| @@ -80,7 +77,7 @@ int main() { | |||
| 80 | if (p[i].i != p[i+1].i) { | 77 | if (p[i].i != p[i+1].i) { |
| 81 | qsort(a, ia, sizeof(int64_t), &cmp_int64); | 78 | qsort(a, ia, sizeof(int64_t), &cmp_int64); |
| 82 | ia = removedoubles(a, ia); | 79 | ia = removedoubles(a, ia); |
| 83 | t += linelen(a, ia) * (p[i+1].i - p[i].i + 1) - | 80 | t += overlaplen(a, ia, a, ia)*(p[i+1].i - p[i].i + 1) - |
| 84 | overlaplen(a, ia, olda, nolda); | 81 | overlaplen(a, ia, olda, nolda); |
| 85 | nolda = ia; | 82 | nolda = ia; |
| 86 | memcpy(olda, a, nolda * sizeof(int64_t)); | 83 | memcpy(olda, a, nolda * sizeof(int64_t)); |
diff --git a/2023/20/20a.c b/2023/20/20a.c index 99b6874..1fda99e 100644 --- a/2023/20/20a.c +++ b/2023/20/20a.c | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | #include <inttypes.h> | 1 | #include <inttypes.h> |
| 2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | 4 | #include <string.h> |
| 6 | 5 | ||
| 7 | #define N 100 | 6 | #define N 100 |
| @@ -95,7 +94,6 @@ int main() { | |||
| 95 | for (int i = 0; i < 1000; i++) | 94 | for (int i = 0; i < 1000; i++) |
| 96 | pushbutton(); | 95 | pushbutton(); |
| 97 | 96 | ||
| 98 | printf("%" PRId64 " (%" PRId64 " low, %" PRId64 " hi)\n", | 97 | printf("%" PRId64 "\n", hitot * lowtot); |
| 99 | hitot * lowtot, lowtot, hitot); | ||
| 100 | return 0; | 98 | return 0; |
| 101 | } | 99 | } |
diff --git a/2023/20/20b.c b/2023/20/20b.c index dc0d6b3..a6f549d 100644 --- a/2023/20/20b.c +++ b/2023/20/20b.c | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | This one is a bit weird. This program works only for my specific input | 2 | This one is a bit weird. This program works only for my specific input. |
| 3 | (included in this folder). Similarly to number 8, this program outputs | 3 | Similarly to day 8, this program outputs 4 numbers and you have to |
| 4 | 4 numbers and you have to take the lcm of them. | 4 | take the lcm of them. |
| 5 | 5 | ||
| 6 | I solved it this way: | 6 | I solved it this way: |
| 7 | 1. First, using a modified version of the code for part one (graph.c), | 7 | 1. First, using a modified version of the code for part one (graph.c), |
| @@ -19,7 +19,6 @@ I solved it this way: | |||
| 19 | #include <inttypes.h> | 19 | #include <inttypes.h> |
| 20 | #include <stdbool.h> | 20 | #include <stdbool.h> |
| 21 | #include <stdio.h> | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> | ||
| 23 | #include <string.h> | 22 | #include <string.h> |
| 24 | 23 | ||
| 25 | #define N 100 | 24 | #define N 100 |
| @@ -97,11 +96,9 @@ bool isclean(void) { | |||
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | int64_t period(int node) { | 98 | int64_t period(int node) { |
| 100 | int64_t npush = 0; | 99 | int64_t npush; |
| 101 | do { | 100 | for (npush = 0; npush == 0 || !isclean(); npush++) |
| 102 | sig(node, false); | 101 | sig(node, false); |
| 103 | npush++; | ||
| 104 | } while (!isclean()); | ||
| 105 | return npush; | 102 | return npush; |
| 106 | } | 103 | } |
| 107 | 104 | ||
diff --git a/2023/21/21a.c b/2023/21/21a.c index a4009d9..c7b4e52 100644 --- a/2023/21/21a.c +++ b/2023/21/21a.c | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 200 | 5 | #define N 200 |
diff --git a/2023/21/21b.c b/2023/21/21b.c index 1748185..8799b93 100644 --- a/2023/21/21b.c +++ b/2023/21/21b.c | |||
| @@ -14,11 +14,10 @@ and fuckyou2.png. | |||
| 14 | #include <inttypes.h> | 14 | #include <inttypes.h> |
| 15 | #include <stdbool.h> | 15 | #include <stdbool.h> |
| 16 | #include <stdio.h> | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> | ||
| 18 | #include <string.h> | 17 | #include <string.h> |
| 19 | 18 | ||
| 20 | #define N 200 | 19 | #define N 200 |
| 21 | #define S 202300L /* My input was 202300*131+65 */ | 20 | #define S 202300L |
| 22 | 21 | ||
| 23 | char map[N][N]; | 22 | char map[N][N]; |
| 24 | int64_t n; | 23 | int64_t n; |
diff --git a/2023/22/22a.c b/2023/22/22a.c index a70a181..c2ca307 100644 --- a/2023/22/22a.c +++ b/2023/22/22a.c | |||
| @@ -1,12 +1,9 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 2 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 5 | #include <string.h> | ||
| 6 | 4 | ||
| 7 | #define N 1500 | 5 | #define N 1500 |
| 8 | #define M 340 | 6 | #define M 340 |
| 9 | #define MAX(x,y) ((x)>(y)?(x):(y)) | ||
| 10 | 7 | ||
| 11 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) | 8 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) |
| 12 | 9 | ||
diff --git a/2023/22/22b.c b/2023/22/22b.c index bbafdfa..7941554 100644 --- a/2023/22/22b.c +++ b/2023/22/22b.c | |||
| @@ -1,12 +1,9 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 2 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 5 | #include <string.h> | ||
| 6 | 4 | ||
| 7 | #define N 1500 | 5 | #define N 1500 |
| 8 | #define M 340 | 6 | #define M 340 |
| 9 | #define MAX(x,y) ((x)>(y)?(x):(y)) | ||
| 10 | 7 | ||
| 11 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) | 8 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) |
| 12 | 9 | ||
| @@ -78,7 +75,7 @@ int main() { | |||
| 78 | drop(j); | 75 | drop(j); |
| 79 | 76 | ||
| 80 | for (int i = 0; i < n; i++) | 77 | for (int i = 0; i < n; i++) |
| 81 | x += homanyfall(i); | 78 | x += howmanyfall(i); |
| 82 | 79 | ||
| 83 | printf("%d\n", x); | 80 | printf("%d\n", x); |
| 84 | return 0; | 81 | return 0; |
diff --git a/2023/23/23a.c b/2023/23/23a.c index fb6ac7d..429b1a4 100644 --- a/2023/23/23a.c +++ b/2023/23/23a.c | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 200 | 5 | #define N 200 |
diff --git a/2023/23/23b.c b/2023/23/23b.c index 9635805..ee35705 100644 --- a/2023/23/23b.c +++ b/2023/23/23b.c | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 150 | 5 | #define N 150 |
diff --git a/2023/24/24b.c b/2023/24/24b.c index 2dab176..48b0a79 100644 --- a/2023/24/24b.c +++ b/2023/24/24b.c | |||
| @@ -92,10 +92,6 @@ bool solvesystem(double A[D][D], double C[D], int d, double *X) { | |||
| 92 | return true; | 92 | return true; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | bool equal(point_t p, point_t q) { | ||
| 96 | return EQ(p.x, q.x) && EQ(p.y, q.y) && EQ(p.z, q.z); | ||
| 97 | } | ||
| 98 | |||
| 99 | point_t pos(line_t l, double t) { | 95 | point_t pos(line_t l, double t) { |
| 100 | return (point_t) { | 96 | return (point_t) { |
| 101 | .x = l.p.x + t*l.v.x, | 97 | .x = l.p.x + t*l.v.x, |
diff --git a/2023/25/25a.c b/2023/25/25a.c index 7626c74..1f3161c 100644 --- a/2023/25/25a.c +++ b/2023/25/25a.c | |||
| @@ -1,18 +1,11 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 2 | #include <stdio.h> |
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | 3 | #include <string.h> |
| 6 | 4 | ||
| 7 | #define N 1500 | 5 | #define N 1500 |
| 8 | #define M 15000 | ||
| 9 | #define MIN(x,y) ((x)<(y)?(x):(y)) | ||
| 10 | #define ischar(c) (c >= 'a' && c <= 'z') | 6 | #define ischar(c) (c >= 'a' && c <= 'z') |
| 11 | 7 | ||
| 12 | typedef struct { | 8 | typedef struct { int nout, out[N]; char s[4], outc[N][4]; } node_t; |
| 13 | int nout, out[N]; | ||
| 14 | char s[4], outc[N][4]; | ||
| 15 | } node_t; | ||
| 16 | 9 | ||
| 17 | bool visited[N]; | 10 | bool visited[N]; |
| 18 | char *buf, line[N][N]; | 11 | char *buf, line[N][N]; |
| @@ -90,6 +83,6 @@ int main() { | |||
| 90 | for (int t = 1; t < n; t++) | 83 | for (int t = 1; t < n; t++) |
| 91 | if (flowatmost(0, t, 3)) nother++; | 84 | if (flowatmost(0, t, 3)) nother++; |
| 92 | 85 | ||
| 93 | printf("%d (%d %d)\n", nother * (n-nother), nother, n-nother); | 86 | printf("%d\n", nother * (n-nother)); |
| 94 | return 0; | 87 | return 0; |
| 95 | } | 88 | } |
diff --git a/2023/README.md b/2023/README.md index eb64e56..1a427bf 100644 --- a/2023/README.md +++ b/2023/README.md | |||
| @@ -8,7 +8,8 @@ send me an email and I'll be happy to explain! | |||
| 8 | 8 | ||
| 9 | ## Instructions | 9 | ## Instructions |
| 10 | 10 | ||
| 11 | Compile with `-std=c99` and other required options, for example | 11 | Compile with `-std=c99`. Day 6 requires also the `-lm` option to link with the |
| 12 | math library, that is | ||
| 12 | 13 | ||
| 13 | ``` | 14 | ``` |
| 14 | $ cc -std=c99 -lm 06/6a.c | 15 | $ cc -std=c99 -lm 06/6a.c |
