aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit effd59e4f2d9e0930b6f8abf5a0df4a240e015d8
parent 2d26ba694e8237e691be0e359ccbf2929c7aa466
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Mon, 25 Dec 2023 17:56:34 +0100

Small cleanup

Diffstat:
M2023/01/1a.c | 3+--
M2023/01/1b.c | 3+--
M2023/02/2a.c | 11+++--------
M2023/02/2b.c | 11+++--------
M2023/03/3a.c | 30+++++++++---------------------
M2023/03/3b.c | 16+++++-----------
M2023/04/4a.c | 13+++++--------
M2023/04/4b.c | 12+++++-------
M2023/05/5a.c | 18++++++++----------
M2023/05/5b.c | 10++++------
M2023/06/6a.c | 14++++----------
M2023/07/7a.c | 9+++------
M2023/07/7b.c | 9+++------
M2023/08/8a.c | 10+++-------
M2023/08/8b.c | 70++++++++++++++++++++++------------------------------------------------
M2023/09/9a.c | 1-
M2023/09/9b.c | 1-
M2023/10/10a.c | 3---
M2023/10/10b.c | 3---
M2023/11/11a.c | 2--
M2023/11/11b.c | 2--
M2023/12/12a.c | 8++------
M2023/13/13a.c | 1-
M2023/13/13b.c | 1-
M2023/15/15b.c | 3+--
M2023/16/16a.c | 5+----
M2023/16/16b.c | 4+---
M2023/17/17a.c | 3---
M2023/17/17b.c | 3---
M2023/18/18b.c | 11++++-------
M2023/20/20a.c | 4+---
M2023/20/20b.c | 13+++++--------
M2023/21/21a.c | 1-
M2023/21/21b.c | 3+--
M2023/22/22a.c | 3---
M2023/22/22b.c | 5+----
M2023/23/23a.c | 1-
M2023/23/23b.c | 1-
M2023/24/24b.c | 4----
M2023/25/25a.c | 11++---------
M2023/README.md | 3++-
41 files changed, 100 insertions(+), 239 deletions(-)

diff --git a/2023/01/1a.c b/2023/01/1a.c @@ -9,8 +9,7 @@ int main() { sum = 0; while ((buf = fgets(line, N, stdin)) != NULL) { for (first = -1; *buf; buf++) { - if (*buf < '0' || *buf > '9') - continue; + if (*buf < '0' || *buf > '9') continue; first = first == -1 ? *buf - '0' : first; last = *buf - '0'; } diff --git a/2023/01/1b.c b/2023/01/1b.c @@ -16,8 +16,7 @@ int main() { for (j = 1; j < 10; j++) if (!strncmp(buf, nums[j], strlen(nums[j]))) *buf = j + '0'; - if (*buf < '0' || *buf > '9') - continue; + if (*buf < '0' || *buf > '9') continue; first = first == -1 ? *buf - '0' : first; last = *buf - '0'; } diff --git a/2023/02/2a.c b/2023/02/2a.c @@ -1,11 +1,8 @@ -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 10000 - -#define MAX(x, y) ((x)>(y) ? (x) : (y)) +#define MAX(x,y) ((x)>(y)?(x):(y)) int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 }; @@ -17,11 +14,9 @@ int main() { maxc[0] = maxc[1] = maxc[2] = 0; while (*buf++ != ':'); while (*buf++) { - if (*buf < '0' || *buf > '9') - continue; + if (*buf < '0' || *buf > '9') continue; x = atoi(buf); - while (*buf < 'a' || *buf > 'z') - buf++; + while (*buf < 'a' || *buf > 'z') buf++; maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x); } if (maxc[0] <= 12 && maxc[1] <= 13 && maxc[2] <= 14) diff --git a/2023/02/2b.c b/2023/02/2b.c @@ -1,11 +1,8 @@ -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 10000 - -#define MAX(x, y) ((x)>(y) ? (x) : (y)) +#define MAX(x,y) ((x)>(y)?(x):(y)) int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 }; @@ -18,11 +15,9 @@ int main() { maxc[0] = maxc[1] = maxc[2] = 0; while (*buf++ != ':'); while (*buf++) { - if (*buf < '0' || *buf > '9') - continue; + if (*buf < '0' || *buf > '9') continue; x = atoi(buf); - while (*buf < 'a' || *buf > 'z') - buf++; + while (*buf < 'a' || *buf > 'z') buf++; maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x); } sum += maxc[0] * maxc[1] * maxc[2]; diff --git a/2023/03/3a.c b/2023/03/3a.c @@ -1,30 +1,23 @@ -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1000 - -bool isnum(char c) { return c >= '0' && c <= '9'; } -bool issymbol(char c) { return c != '.' && c != 0 && c != '\n' && !isnum(c); } +#define ISNUM(c) (c >= '0' && c <= '9') +#define ISSYM(c) (c != '.' && c != 0 && c != '\n' && !ISNUM(c)) int grab(char *buf) { - int x; - - if (!isnum(*buf)) - return 0; - - while (isnum(*buf)) buf--; + if (!ISNUM(*buf)) return 0; + while (ISNUM(*buf)) buf--; buf++; - x = atoi(buf); - while (isnum(*buf)) *buf++ = '.'; + int x = atoi(buf); + while (ISNUM(*buf)) *buf++ = '.'; return x; } int main() { char line[N], prev[N]; - bool l, c, r, ul, uc, ur; int i, sum; sum = 0; @@ -32,15 +25,10 @@ int main() { memset(prev, '.', N); while (fgets(&line[1], N-1, stdin) != NULL) { for (i = 1; line[i]; i++) { - l = issymbol(line[i-1]); - c = issymbol(line[i]); - r = issymbol(line[i+1]); - ul = issymbol(prev[i-1]); - uc = issymbol(prev[i]); - ur = issymbol(prev[i+1]); - if (l || r || ul || uc || ur) + if (ISSYM(line[i-1]) || ISSYM(line[i+1]) || + ISSYM(prev[i-1]) || ISSYM(prev[i]) || ISSYM(prev[i+1])) sum += grab(&line[i]); - if (l || c || r) + if (ISSYM(line[i-1]) || ISSYM(line[i]) || ISSYM(line[i+1])) sum += grab(&prev[i]); } memcpy(prev, line, N); diff --git a/2023/03/3b.c b/2023/03/3b.c @@ -1,22 +1,16 @@ -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1000 - -bool isnum(char c) { return c >= '0' && c <= '9'; } +#define ISNUM(c) (c >= '0' && c <= '9') int grab(char *buf) { - int x; - - if (!isnum(*buf)) - return 0; - - while (isnum(*buf)) buf--; + if (!ISNUM(*buf)) return 0; + while (ISNUM(*buf)) buf--; buf++; - x = atoi(buf); - while (isnum(*buf)) *buf++ = '.'; + int x = atoi(buf); + while (ISNUM(*buf)) *buf++ = '.'; return x; } diff --git a/2023/04/4a.c b/2023/04/4a.c @@ -1,11 +1,8 @@ -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 1000 - -bool isnum(char c) { return c >= '0' && c <= '9'; } +#define ISNUM(c) (c >= '0' && c <= '9') int main() { char *buf, line[N]; @@ -17,15 +14,15 @@ int main() { while (*buf != ':') buf++; while (*buf != '|') { buf++; - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; w[nw++] = atoi(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } while (*buf != '\n') { buf++; - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; r[nr++] = atoi(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } for (x = 0, i = 0; i < nr; i++) { for (j = 0; j < nw; j++) { diff --git a/2023/04/4b.c b/2023/04/4b.c @@ -1,11 +1,9 @@ -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1000 - -bool isnum(char c) { return c >= '0' && c <= '9'; } +#define ISNUM(c) (c >= '0' && c <= '9') int main() { char *buf, line[N]; @@ -19,15 +17,15 @@ int main() { while (*buf != ':') buf++; while (*buf != '|') { buf++; - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; w[nw++] = atoi(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } while (*buf != '\n') { buf++; - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; r[nr++] = atoi(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } for (x = 0, i = 0; i < nr; i++) { for (j = 0; j < nw; j++) { diff --git a/2023/05/5a.c b/2023/05/5a.c @@ -1,26 +1,25 @@ #include <inttypes.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 100 - -bool isnum(char c) { return c >= '0' && c <= '9'; } +#define ISNUM(c) (c >= '0' && c <= '9') +#define MIN(x,y) ((x)<(y)?(x):(y)) int main() { char *buf, line[N]; int64_t i, m, ns, seed[N], next[N], r[3]; for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) { - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; next[ns++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } while ((buf = fgets(line, N, stdin)) != NULL) { - if (!isnum(*buf)) { + if (!ISNUM(*buf)) { memcpy(seed, next, ns * sizeof(int64_t)); fgets(line, N, stdin); /* Discard description */ continue; @@ -28,7 +27,7 @@ int main() { for (i = 0; *buf; buf++) { r[i++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } for (i = 0; i < ns; i++) @@ -36,9 +35,8 @@ int main() { next[i] = seed[i] + (r[0] - r[1]); } - m = next[0]; - for (i = 1; i < ns; i++) - m = m > next[i] ? next[i] : m; + for (i = 1, m = next[0]; i < ns; i++) + m = MIN(m, next[i]); printf("%" PRId64 "\n", m); return 0; diff --git a/2023/05/5b.c b/2023/05/5b.c @@ -1,17 +1,15 @@ /* For part 2 we save ranges as (first, last) instead of (first, length) */ #include <inttypes.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 10000 - +#define ISNUM(c) (c >= '0' && c <= '9') #define MIN(a, b) ((a)<(b)?(a):(b)) #define MAX(a, b) ((a)>(b)?(a):(b)) -bool isnum(char c) { return c >= '0' && c <= '9'; } void append(int64_t dst[][2], int64_t src[][2], int64_t *nr, int64_t *nnr) { 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) { int64_t readl(int64_t nums[], char *buf) { int64_t i; for (i = 0; *buf; buf++) { - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; nums[i++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } return i; } @@ -43,7 +41,7 @@ int main() { } for (nr = 0; (buf = fgets(line, N, stdin)) != NULL; ) { - if (!isnum(*buf)) { + if (!ISNUM(*buf)) { append(range, next, &nr, &nnr); continue; } diff --git a/2023/06/6a.c b/2023/06/6a.c @@ -2,30 +2,24 @@ #include <inttypes.h> #include <math.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 100000 - -#define MIN(a, b) ((a)<(b)?(a):(b)) -#define MAX(a, b) ((a)>(b)?(a):(b)) - -bool isnum(char c) { return c >= '0' && c <= '9'; } +#define ISNUM(c) (c >= '0' && c <= '9') int64_t readl(int64_t nums[], char *buf) { int64_t i; for (i = 0; *buf; buf++) { - if (!isnum(*buf)) continue; + if (!ISNUM(*buf)) continue; nums[i++] = atoll(buf); - while (isnum(*buf)) buf++; + while (ISNUM(*buf)) buf++; } return i; } int main() { - char line[N], clean[N]; + char line[N]; int64_t i, n, p, D, x1, x2, t[N], d[N]; n = readl(t, fgets(line, N, stdin)); diff --git a/2023/07/7a.c b/2023/07/7a.c @@ -11,18 +11,15 @@ int value[255] = { ['T'] = 10, ['J'] = 11, ['Q'] = 12, ['K'] = 13, ['A'] = 14 }; -typedef struct { - char c[5]; - int64_t b; -} hand_t; +typedef struct { char c[5]; int64_t b; } hand_t; int strength(hand_t h) { int i, c2, c1, s, x[15]; memset(x, 0, 15 * sizeof(int)); for (i = 0, s = 0; i < 5; i++) { - s = s * 15 + value[h.c[i]]; - x[value[h.c[i]]]++; + s = s * 15 + value[(int)h.c[i]]; + x[value[(int)h.c[i]]]++; } for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { if (c1 < x[i]) { diff --git a/2023/07/7b.c b/2023/07/7b.c @@ -11,18 +11,15 @@ int value[255] = { ['T'] = 10, ['J'] = 1, ['Q'] = 12, ['K'] = 13, ['A'] = 14 }; -typedef struct { - char c[5]; - int64_t b; -} hand_t; +typedef struct { char c[5]; int64_t b; } hand_t; int strength(hand_t h) { int i, c2, c1, s, x[15]; memset(x, 0, 15 * sizeof(int)); for (i = 0, s = 0; i < 5; i++) { - s = s * 15 + value[h.c[i]]; - x[value[h.c[i]]]++; + s = s * 15 + value[(int)h.c[i]]; + x[value[(int)h.c[i]]]++; } for (i = 2, c1 = 0, c2 = 0; i < 15; i++) { if (c1 < x[i]) { diff --git a/2023/08/8a.c b/2023/08/8a.c @@ -1,17 +1,13 @@ -#include <inttypes.h> -#include <math.h> -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 30000 - -bool isnum(char c) { return c >= '0' && c <= '9'; } -int index(char *c) { return c[0]-'A' + (c[1]-'A' + (c[2]-'A')*26)*26; } +#define ISNUM(c) (c >= '0' && c <= '9') int map[N][2]; +int index(char *c) { return c[0]-'A' + (c[1]-'A' + (c[2]-'A')*26)*26; } + int main() { char *buf, dir[N], line[N]; int n, i, s; diff --git a/2023/08/8b.c b/2023/08/8b.c @@ -1,74 +1,50 @@ /* -I hate this stupid problem. If you read the description carefully (which is -what I did), you would think that the problem is much, much harder than it -actually is. In reality, the author made many assumptions about the possible -paths that made is super easy. Let's see a few: +I hate this stupid problem. There are a bunch of unwritten properties +of the paths that make the problem very simple: -- Different ghost's path do not end up in the same Z-node (this is not super - important for the solution). -- Each ghost has exactly one Z-node in its path (one could think about this - when reading the strange remark that "there are as many A-nodes as Z-nodes", - which does not look important at first). -- Each ghosts meets a Z-node exactly once before entering a loop (this is - a fundamental and incredibly strong assupmtion). +- Different ghosts' paths do not end up in the same Z-node. +- Each ghost has exactly one Z-node in its path. +- Each ghost meets a Z-node exactly once before entering a loop. - If a ghost encounters a Z-node after X steps, it will encounter it exactly - every X steps (like WTF I don't even have to solve a system of congruences? - what is this, a problem for babies?) + every X steps; preperiods just end up aligning nicely. -Fuck. - -This code does not even solve the problem by the way, it computes some data -about the path that each ghost takes. Then you can figure out the solution -with a pocket calculator, or by hand. +In practice, everything just works out so that the lcm of the periods +is the solution, which is very much not the case for a general input. +This code computes the periods of the paths of the different ghosts. Then +you can figure out the solution with a pocket calculator, or by hand. */ -#include <inttypes.h> -#include <math.h> -#include <stdbool.h> + #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 1000 typedef struct { char last; int next[2]; } node_t; -typedef struct { int preplen, plen, nz, z[N], zid[N]; } source_t; + +int v[N*N]; int ind(char s[3], char m[][3], int n) { for (int j = 0; j < n; j++) if (s[0] == m[j][0] && s[1] == m[j][1] && s[2] == m[j][2]) return j; - return -1; } -int v[N*N]; -source_t worksource(int i, node_t *nodes, char *dir, int n, int k) { - source_t s; - int state; - -printf("Working source %d\n", i); - +int findperiod(int i, node_t *nodes, char *dir, int n, int k) { memset(v, 0, sizeof(int) * N*N); - for (int j = i, d = 0, l = 1; true; d = (d+1)%k) { + for (int j = i, d = 0, l = 1, state = -1; ; d = (d+1)%k) { j = nodes[j].next[dir[d] == 'R']; state = j*k+d; -if (nodes[j].last == 'Z') printf("Found Z %d at %d\n", j, l); - if (v[state]) { - s.preplen = v[state]-1; - s.plen = l - v[state]; -printf("Stopping at %d. Preperiod: %d, period: %d\n", l, s.preplen, s.plen); - return s; - } else v[state] = l++; + if (v[state]) return l - v[state]; + else v[state] = l++; } - - return s; + return -1; } int main() { node_t nodes[N]; - source_t src[N]; char *buf, dir[N], line[N], name[N][3], lstr[N][3], rstr[N][3]; - int k, i, n, m, s; + int k, n; k = strlen(fgets(dir, N, stdin)) - 1; fgets(line, N, stdin); @@ -81,16 +57,14 @@ int main() { memcpy(rstr[n], buf+1, 3); } - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { nodes[i].next[0] = ind(lstr[i], name, n); nodes[i].next[1] = ind(rstr[i], name, n); } -printf("%d %d\n", n, k); - - for (i = 0, m = 0; i < n; i++) + for (int i = 0; i < n; i++) if (nodes[i].last == 'A') - src[m++] = worksource(i, nodes, dir, n, k); + printf("%d\n", findperiod(i, nodes, dir, n, k)); return 0; } diff --git a/2023/09/9a.c b/2023/09/9a.c @@ -2,7 +2,6 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 1000 diff --git a/2023/09/9b.c b/2023/09/9b.c @@ -2,7 +2,6 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 1000 diff --git a/2023/10/10a.c b/2023/10/10a.c @@ -1,7 +1,4 @@ -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define N 1000 diff --git a/2023/10/10b.c b/2023/10/10b.c @@ -1,7 +1,4 @@ -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define N 1000 #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 @@ -1,7 +1,5 @@ #include <inttypes.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define N 1000 diff --git a/2023/11/11b.c b/2023/11/11b.c @@ -1,7 +1,5 @@ #include <inttypes.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define N 1000 diff --git a/2023/12/12a.c b/2023/12/12a.c @@ -1,14 +1,10 @@ -#include <inttypes.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1000 -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) -#define ABS(a) MAX((a),-(a)) +#define ABS(a) ((a)>0?(a):-(a)) #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) @@ -16,7 +12,7 @@ char *buf, line[N]; int s, n, a[N]; int readl(int nums[], char *buf) { - int64_t i; + int i; for (i = 0; *buf; buf++) { if (!isnum(*buf)) continue; nums[i++] = atoll(buf); diff --git a/2023/13/13a.c b/2023/13/13a.c @@ -1,6 +1,5 @@ #include <inttypes.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 64 diff --git a/2023/13/13b.c b/2023/13/13b.c @@ -1,6 +1,5 @@ #include <inttypes.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 64 diff --git a/2023/15/15b.c b/2023/15/15b.c @@ -12,8 +12,7 @@ int main() { for (b = line, c = 0; *b != '\n'; b++) { switch (*b) { case ',': - c = 0; - l = 0; + c = l = 0; break; case '=': int64_t i; diff --git a/2023/16/16a.c b/2023/16/16a.c @@ -1,7 +1,4 @@ -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define M 200 @@ -25,7 +22,7 @@ void walk(int i, int j, int d) { if (!entered[i][j]) s++; entered[i][j] |= d; for (int k = 1; k <= 8; k <<= 1) - if (k & turn[d][map[i][j]]) + if (k & turn[d][(int)map[i][j]]) walk(i+go[k][0], j+go[k][1], k); } diff --git a/2023/16/16b.c b/2023/16/16b.c @@ -1,6 +1,4 @@ -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define M 200 @@ -26,7 +24,7 @@ void walk(int i, int j, int d) { if (!entered[i][j]) s++; entered[i][j] |= d; for (int k = 1; k <= 8; k <<= 1) - if (k & turn[d][map[i][j]]) + if (k & turn[d][(int)map[i][j]]) walk(i+go[k][0], j+go[k][1], k); } diff --git a/2023/17/17a.c b/2023/17/17a.c @@ -1,7 +1,4 @@ -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define M 150 #define MAXE 1000000 diff --git a/2023/17/17b.c b/2023/17/17b.c @@ -1,7 +1,4 @@ -#include <stdbool.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #define M 150 #define MAXE 1000000 diff --git a/2023/18/18b.c b/2023/18/18b.c @@ -1,6 +1,7 @@ /* -I have lost my code for part one. It reused some stuff from day 10. -This code can be adjusted to work for part one, just change the input reading. +I have lost my code for part one. It reused some stuff from day +10. This code can be adjusted to work for part one, just change the +input reading part. */ #include <inttypes.h> @@ -57,10 +58,6 @@ int64_t overlaplen(int64_t a[], int64_t na, int64_t b[], int64_t nb) { return ret; } -int64_t linelen(int64_t a[], int64_t n) { - return overlaplen(a, n, a, n); -} - int main() { p[n++] = (point_t) {0}; while ((buf = fgets(in, 50, stdin)) != NULL) { @@ -80,7 +77,7 @@ int main() { if (p[i].i != p[i+1].i) { qsort(a, ia, sizeof(int64_t), &cmp_int64); ia = removedoubles(a, ia); - t += linelen(a, ia) * (p[i+1].i - p[i].i + 1) - + t += overlaplen(a, ia, a, ia)*(p[i+1].i - p[i].i + 1) - overlaplen(a, ia, olda, nolda); nolda = ia; memcpy(olda, a, nolda * sizeof(int64_t)); diff --git a/2023/20/20a.c b/2023/20/20a.c @@ -1,7 +1,6 @@ #include <inttypes.h> #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 100 @@ -95,7 +94,6 @@ int main() { for (int i = 0; i < 1000; i++) pushbutton(); - printf("%" PRId64 " (%" PRId64 " low, %" PRId64 " hi)\n", - hitot * lowtot, lowtot, hitot); + printf("%" PRId64 "\n", hitot * lowtot); return 0; } diff --git a/2023/20/20b.c b/2023/20/20b.c @@ -1,7 +1,7 @@ /* -This one is a bit weird. This program works only for my specific input -(included in this folder). Similarly to number 8, this program outputs -4 numbers and you have to take the lcm of them. +This one is a bit weird. This program works only for my specific input. +Similarly to day 8, this program outputs 4 numbers and you have to +take the lcm of them. I solved it this way: 1. First, using a modified version of the code for part one (graph.c), @@ -19,7 +19,6 @@ I solved it this way: #include <inttypes.h> #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 100 @@ -97,11 +96,9 @@ bool isclean(void) { } int64_t period(int node) { - int64_t npush = 0; - do { + int64_t npush; + for (npush = 0; npush == 0 || !isclean(); npush++) sig(node, false); - npush++; - } while (!isclean()); return npush; } diff --git a/2023/21/21a.c b/2023/21/21a.c @@ -1,6 +1,5 @@ #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 200 diff --git a/2023/21/21b.c b/2023/21/21b.c @@ -14,11 +14,10 @@ and fuckyou2.png. #include <inttypes.h> #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 200 -#define S 202300L /* My input was 202300*131+65 */ +#define S 202300L char map[N][N]; int64_t n; diff --git a/2023/22/22a.c b/2023/22/22a.c @@ -1,12 +1,9 @@ -#include <inttypes.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 1500 #define M 340 -#define MAX(x,y) ((x)>(y)?(x):(y)) #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) diff --git a/2023/22/22b.c b/2023/22/22b.c @@ -1,12 +1,9 @@ -#include <inttypes.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define N 1500 #define M 340 -#define MAX(x,y) ((x)>(y)?(x):(y)) #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) @@ -78,7 +75,7 @@ int main() { drop(j); for (int i = 0; i < n; i++) - x += homanyfall(i); + x += howmanyfall(i); printf("%d\n", x); return 0; diff --git a/2023/23/23a.c b/2023/23/23a.c @@ -1,6 +1,5 @@ #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 200 diff --git a/2023/23/23b.c b/2023/23/23b.c @@ -1,6 +1,5 @@ #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 150 diff --git 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) { return true; } -bool equal(point_t p, point_t q) { - return EQ(p.x, q.x) && EQ(p.y, q.y) && EQ(p.z, q.z); -} - point_t pos(line_t l, double t) { return (point_t) { .x = l.p.x + t*l.v.x, diff --git a/2023/25/25a.c b/2023/25/25a.c @@ -1,18 +1,11 @@ -#include <inttypes.h> #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define N 1500 -#define M 15000 -#define MIN(x,y) ((x)<(y)?(x):(y)) #define ischar(c) (c >= 'a' && c <= 'z') -typedef struct { - int nout, out[N]; - char s[4], outc[N][4]; -} node_t; +typedef struct { int nout, out[N]; char s[4], outc[N][4]; } node_t; bool visited[N]; char *buf, line[N][N]; @@ -90,6 +83,6 @@ int main() { for (int t = 1; t < n; t++) if (flowatmost(0, t, 3)) nother++; - printf("%d (%d %d)\n", nother * (n-nother), nother, n-nother); + printf("%d\n", nother * (n-nother)); return 0; } diff --git a/2023/README.md b/2023/README.md @@ -8,7 +8,8 @@ send me an email and I'll be happy to explain! ## Instructions -Compile with `-std=c99` and other required options, for example +Compile with `-std=c99`. Day 6 requires also the `-lm` option to link with the +math library, that is ``` $ cc -std=c99 -lm 06/6a.c