Skip to content

Commit 7afbbf2

Browse files
address unused function warnings
1 parent 4504138 commit 7afbbf2

36 files changed

+154
-152
lines changed

2018-c/.clangd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
CompileFlags:
2-
Add: [-std=c17, -Wall, -Wextra, -Wpedantic, -Wundef, -Winline, -Wimplicit-fallthrough, -Wformat=2, -Wconversion ]
2+
Add: [-std=c17, -Wall, -Wextra, -Wpedantic, -Wundef, -Winline, -Wimplicit-fallthrough, -Wformat=2, -Wvla, -Wconversion, -Wstringop-overflow=3 ]

2018-c/06.c

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "adventofcode.h"
2+
#include <assert.h>
23
#include <stdio.h>
34
#include <stdlib.h>
4-
#include <string.h>
55
#include <time.h>
66

77
#define PUZZLE_NAME "Day 6: Chronal Coordinates"
@@ -32,10 +32,10 @@ void determine_bounds(struct Point *points, size_t n, int bounds[4]) {
3232
}
3333
}
3434

35-
size_t parse(const char *s, struct Point *points, int bounds[4]) {
35+
static size_t parse(const char *s, struct Point points[static 64], int bounds[4]) {
3636
size_t n = 0;
3737

38-
while (*s != 0x0) {
38+
while (*s != 0x0 && n < 64) {
3939
s = parse_int(&points[n].x, s);
4040
s = skip(", ", s);
4141
s = parse_int(&points[n].y, s);
@@ -48,7 +48,7 @@ size_t parse(const char *s, struct Point *points, int bounds[4]) {
4848
return n;
4949
}
5050

51-
int get_closest_point(struct Point *points, size_t n, int x, int y) {
51+
static int get_closest_point(const struct Point points[static 64], const size_t n, const int x, const int y) {
5252
int closest_idx = -1;
5353
int closest_distance = 1 << 30;
5454

@@ -67,12 +67,10 @@ int get_closest_point(struct Point *points, size_t n, int x, int y) {
6767
return closest_idx;
6868
}
6969

70-
int pt1(struct Point points[], size_t n, int bounds[4]) {
71-
int areas[n];
72-
memset(areas, 0, n * sizeof(int));
73-
74-
char infinite[n];
75-
memset(areas, 0, n * sizeof(*infinite));
70+
unsigned int pt1(const struct Point points[static 64], const size_t n, const int bounds[4]) {
71+
assert(n <= 64);
72+
unsigned int areas[64] = {0};
73+
unsigned int infinite[64] = {0};
7674

7775
for (int x = bounds[0], y = bounds[1]; x <= bounds[2] && y <= bounds[3];) {
7876
int closest_idx = get_closest_point(points, n, x, y);
@@ -103,11 +101,13 @@ int pt1(struct Point points[], size_t n, int bounds[4]) {
103101
}
104102
}
105103

104+
106105
return areas[largest];
107106
}
108107

109-
int pt2(struct Point points[], size_t n, int bounds[4]) {
110-
int count = 0;
108+
unsigned int pt2(const struct Point points[static 64], const size_t n, const int bounds[static 4]) {
109+
assert(n <= 64);
110+
unsigned int count = 0;
111111
for (int x = bounds[0], y = bounds[1]; x <= bounds[2] && y <= bounds[3];) {
112112
int distance = 0;
113113

@@ -138,24 +138,15 @@ int main(void) {
138138
read_input_file(input, 64 * 1024, "06.txt");
139139

140140
int bounds[4];
141-
struct Point *points = malloc_or_die(50 * sizeof(*points));
141+
struct Point points[64];
142142
size_t n = parse(input, points, bounds);
143143

144-
// printf("%ld coords: (%d, %d) to (%d, %d)\n", n, bounds[0], bounds[1],
145-
// bounds[2], bounds[3]);
146-
147-
int a1 = pt1(points, n, bounds);
148-
int a2 = pt2(points, n, bounds);
149-
150-
free(points);
144+
unsigned int a1 = pt1(points, n, bounds);
145+
unsigned int a2 = pt2(points, n, bounds);
151146

152147
printf("--- %s ---\n", PUZZLE_NAME);
153148
printf("Part 1: %d\n", a1);
154149
printf("Part 2: %d\n", a2);
155150
printf("Time: %.2fms\n", clock_time_since(t));
156151
return EXIT_SUCCESS;
157152
}
158-
159-
// part 1: 3890 CORRECT
160-
// part 2: 99540 too high
161-
// part 2: 40284 CORRECT

2018-c/07.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ static void pt1(char *buf, struct step **steps, const size_t nsteps) {
7272
}
7373

7474
static int pt2(struct step **steps, const size_t nsteps) {
75-
const size_t nworkers = 5;
75+
#define NWORKERS 5
7676
int delay_s = 60;
77-
int worker_finish_times[nworkers];
78-
struct step *worker_tasks[nworkers];
79-
memset(worker_finish_times, 0, sizeof(*worker_finish_times) * nworkers);
80-
memset(worker_tasks, 0, sizeof(struct step *) * nworkers);
77+
int worker_finish_times[NWORKERS];
78+
struct step *worker_tasks[NWORKERS];
79+
memset(worker_finish_times, 0, sizeof(*worker_finish_times) * NWORKERS);
80+
memset(worker_tasks, 0, sizeof(struct step *) * NWORKERS);
8181
size_t ndone = 0;
8282

8383
// loop over seconds
8484
for (int s = 0;; s++) {
8585

8686
// first loop, mark steps as done
87-
for (size_t w = 0; w < nworkers; w++) {
87+
for (size_t w = 0; w < NWORKERS; w++) {
8888
if (worker_finish_times[w] != s) {
8989
continue;
9090
}
@@ -104,7 +104,7 @@ static int pt2(struct step **steps, const size_t nsteps) {
104104
}
105105

106106
// second loop: assign new tasks
107-
for (size_t w = 0; w < nworkers; w++) {
107+
for (size_t w = 0; w < NWORKERS; w++) {
108108
if (worker_tasks[w] != NULL) {
109109
continue;
110110
}

2018-c/08.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "adventofcode.h"
2+
#include <assert.h>
23
#include <linux/limits.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <time.h>
67

78
#define PUZZLE_NAME "Day 8: Memory Maneuver"
89

9-
const char *pt1(const char *s, int *sum) {
10+
static const char *pt1(const char *s, int *sum) {
1011
int nchilds;
1112
int nmetadata;
1213

@@ -30,7 +31,7 @@ const char *pt1(const char *s, int *sum) {
3031
return s;
3132
}
3233

33-
const char *pt2(const char *s, int *sum) {
34+
static const char *pt2(const char *s, int *sum) {
3435
int nchilds;
3536
int nmetadata;
3637

@@ -39,11 +40,11 @@ const char *pt2(const char *s, int *sum) {
3940
s = parse_int(&nmetadata, s);
4041
s = skip(" ", s);
4142

42-
int child_values[nchilds+1];
43+
int child_values[32];
44+
assert(nchilds <= 32);
4345
for (int i = 0; i < nchilds; i++) {
4446
child_values[i] = 0;
4547
s = pt2(s, &child_values[i]);
46-
// sum += child_values[i];
4748
}
4849

4950
for (int i = 0; i < nmetadata; i++) {
@@ -64,8 +65,8 @@ const char *pt2(const char *s, int *sum) {
6465

6566
int main(void) {
6667
clock_t t = clock_time();
67-
char input[1024 * 64];
68-
read_input_file(input, 1024 * 64, "08.txt");
68+
char input[64 << 10];
69+
read_input_file(input, 64 << 10, "08.txt");
6970

7071
int a1 = 0;
7172
pt1(input, &a1);

2018-c/09.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

77
#define PUZZLE_NAME "Day 9: Marble Mania"
88

9+
// Puzzle input
10+
#define NPLAYERS 458
11+
#define NMARBLES 72019
12+
913
struct node {
1014
unsigned long value;
1115
struct node *next;
1216
struct node *prev;
1317
};
1418

15-
static inline unsigned long max(const unsigned int size,
16-
const unsigned long arr[size]) {
19+
static unsigned long max(const unsigned long arr[static NPLAYERS]) {
1720
unsigned int mi = 1;
18-
for (unsigned int i = 2; i < size; i++) {
21+
for (unsigned int i = 2; i < NPLAYERS; i++) {
1922
if (arr[i] > arr[mi]) {
2023
mi = i;
2124
}
@@ -24,27 +27,25 @@ static inline unsigned long max(const unsigned int size,
2427
return arr[mi];
2528
}
2629

27-
unsigned long play(const unsigned int nplayers, const unsigned long nmarbles) {
30+
static unsigned long play(const unsigned long nmarbles) {
2831
struct node *marbles = malloc_or_die(sizeof(struct node) * (nmarbles + 1));
2932
for (unsigned long m = 0; m <= nmarbles; m++) {
3033
marbles[m].value = m;
3134
marbles[m].prev = NULL;
3235
marbles[m].next = NULL;
3336
}
3437

35-
unsigned long scores[nplayers];
36-
memset(scores, 0, sizeof(unsigned long) * (unsigned long)(nplayers));
37-
38+
unsigned long scores[NPLAYERS] = {0};
3839
struct node *cur = &marbles[0];
3940
cur->next = cur;
4041
cur->prev = cur;
4142
unsigned int player = 0;
42-
for (unsigned long m = 1; m <= nmarbles; m++, player++, player %= nplayers) {
43+
for (unsigned long m = 1; m <= nmarbles; m++, player++, player %= NPLAYERS) {
4344

4445
if (m % 23 == 0) {
4546
scores[player] += m;
4647
struct node *item = cur;
47-
for (int i = 0; i < 7; i++) {
48+
for (unsigned int i = 0; i < 7; i++) {
4849
item = item->prev;
4950
}
5051
scores[player] += item->value;
@@ -64,17 +65,14 @@ unsigned long play(const unsigned int nplayers, const unsigned long nmarbles) {
6465
}
6566

6667
free(marbles);
67-
return max(nplayers, scores);
68+
return max( scores);
6869
}
6970

7071
int main(void) {
7172
clock_t t = clock_time();
7273

73-
const unsigned int nplayers = 458;
74-
const unsigned long nmarbles = 72019;
75-
76-
unsigned long pt1 = play(nplayers, nmarbles);
77-
unsigned long pt2 = play(nplayers, nmarbles * 100);
74+
unsigned long pt1 = play(NMARBLES);
75+
unsigned long pt2 = play(NMARBLES * 100);
7876

7977
printf("--- %s ---\n", PUZZLE_NAME);
8078
printf("Part 1: %ld\n", pt1);

2018-c/10.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ struct Point {
1313
int vx, vy;
1414
};
1515

16-
static void print(const struct Point points[], const unsigned int npoints,
16+
static void print(const struct Point points[static 512], const unsigned int npoints,
1717
const int x1, const int y1, const int x2, const int y2) {
1818
unsigned w = (unsigned) (x2 - x1) + 1;
1919
unsigned h = (unsigned) (y2 - y1) + 1;
20-
char grid[h][w];
21-
memset(grid, ' ', (unsigned int)w * (unsigned int)h * sizeof(char));
20+
21+
assert(h <= 12);
22+
assert(w <= 64);
23+
char grid[12][64];
24+
memset(grid, ' ', 12 * 64 * sizeof(char));
2225

2326
for (unsigned p = 0; p < npoints; p++) {
2427
unsigned y = (unsigned) (points[p].py - y1);

2018-c/11.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static inline int power_level(const unsigned x, const unsigned y,
2828
return -5;
2929
}
3030

31-
struct Answer solve_pt1(const int serial_number) {
31+
static struct Answer solve_pt1(const int serial_number) {
3232
int max = INT_MIN;
3333
unsigned xmax = 0;
3434
unsigned ymax = 0;
@@ -52,7 +52,7 @@ struct Answer solve_pt1(const int serial_number) {
5252
return (struct Answer){xmax + 1, ymax + 1, 3};
5353
}
5454

55-
struct Answer solve_pt2(const int serial_number) {
55+
static struct Answer solve_pt2(const int serial_number) {
5656
int max = INT_MIN;
5757
struct Answer a;
5858

2018-c/12.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
#include <time.h>
66

77
#define PUZZLE_NAME "Day 12: Subterranean Sustainability"
8-
9-
static const unsigned PADDING = 200; // should be larger than # of cycles
10-
static const unsigned INITIAL_PLANTS = 99;
11-
static const unsigned WIDTH = INITIAL_PLANTS + PADDING * 2;
8+
#define PADDING 200 // should be larger than # of cycles
9+
#define INITIAL_PLANTS 99
10+
#define WIDTH (INITIAL_PLANTS + PADDING * 2)
1211

1312
struct Pattern {
1413
char in[5];
1514
char out;
1615
};
1716

18-
unsigned long count_plants(const char plants[WIDTH], unsigned long offset) {
17+
static unsigned long count_plants(const char plants[static WIDTH], const unsigned long offset) {
1918
unsigned long count = 0;
2019

2120
// for part 2 I noticed the grid would stabilise
@@ -31,7 +30,7 @@ unsigned long count_plants(const char plants[WIDTH], unsigned long offset) {
3130
return count;
3231
}
3332

34-
void growth_cycles(char plants[WIDTH], const struct Pattern patterns[],
33+
static void growth_cycles(char plants[static WIDTH], const struct Pattern patterns[],
3534
const unsigned npatterns, const unsigned ncycles) {
3635
char next[WIDTH];
3736
for (unsigned g = 0; g < ncycles; g++) {
@@ -68,8 +67,7 @@ int main(void) {
6867

6968
unsigned npatterns = 0;
7069
struct Pattern patterns[64];
71-
while (fgets(line, 1024, stdin)) {
72-
70+
while (fgets(line, 1024, stdin) && npatterns < 64) {
7371
// we only need to know about the patterns that result into a plant
7472
if (line[9] != '#') {
7573
continue;

2018-c/18.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct Count {
2525
int open;
2626
};
2727

28-
static struct Count visit_adjacent(const char *grid, const size_t width,
28+
static struct Count visit_adjacent(const char grid[static 50*50], const size_t width,
2929
const unsigned int r1,
3030
const unsigned int c1) {
3131
struct Count cnt = {0, 0, 0};
@@ -59,8 +59,8 @@ static struct Count visit_adjacent(const char *grid, const size_t width,
5959
return cnt;
6060
}
6161

62-
static void step(const size_t width, char grid[width * width],
63-
char next[width * width]) {
62+
static void step(const size_t width, char grid[static 50*50],
63+
char next[static 50*50]) {
6464
for (unsigned r = 0; r < width; r++) {
6565
for (unsigned c = 0; c < width; c++) {
6666
const struct Count cnt = visit_adjacent(grid, width, r, c);
@@ -95,10 +95,7 @@ static void step(const size_t width, char grid[width * width],
9595
}
9696

9797
// swap pointers
98-
// char *tmp = grid;
9998
memcpy(grid, next, width * width * sizeof(char));
100-
// grid = next;
101-
// next = tmp;
10299
}
103100

104101
struct Cycle {
@@ -113,7 +110,7 @@ struct Cycle {
113110
Floyd's tortoise and hare cycle-finding algorithm
114111
See https://en.wikipedia.org/wiki/Cycle_detection
115112
*/
116-
static struct Cycle floyd(const size_t width, const char grid[width * width]) {
113+
static struct Cycle floyd(const size_t width, const char grid[static 50*50]) {
117114
const size_t n = width * width * sizeof(char);
118115
char *tortoise = malloc(n);
119116
char *hare = malloc(n);
@@ -180,7 +177,7 @@ static struct Cycle floyd(const size_t width, const char grid[width * width]) {
180177
return (struct Cycle){mu, lam};
181178
}
182179

183-
static unsigned grid_value(const size_t width, const char grid[width * width]) {
180+
static unsigned grid_value(const size_t width, const char grid[static 50*50]) {
184181
unsigned trees = 0;
185182
unsigned lumberyards = 0;
186183
const size_t sz = width * width;
@@ -198,7 +195,7 @@ static unsigned grid_value(const size_t width, const char grid[width * width]) {
198195
return trees * lumberyards;
199196
}
200197

201-
static unsigned int nsteps(const size_t width, const char grid[width * width],
198+
static unsigned int nsteps(const size_t width, const char grid[static 50*50],
202199
unsigned nsteps) {
203200
const size_t n = width * width * sizeof(char);
204201
char *cur = malloc(n);
@@ -226,6 +223,7 @@ int main(void) {
226223
char *grid = malloc(grid_size * sizeof(char));
227224
assert(grid != NULL);
228225
const size_t width = parse(grid);
226+
assert(width <= 50);
229227

230228
const unsigned int pt1 = nsteps(width, grid, 10);
231229

0 commit comments

Comments
 (0)