Skip to content

Commit b73e713

Browse files
committed
Add -Wconversion compiler warning
1 parent d43bfda commit b73e713

16 files changed

+97
-92
lines changed

Day01.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define CAP 4096
44

55
static int parse(const char *input, int depths[CAP]) {
6-
size_t n = 0;
6+
int n = 0;
77
int charsRead = 0;
88
int filled = 0;
99

Day03.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static int parse(const char *input, int diags[CAP]) {
2424

2525
input += charsRead;
2626

27-
diags[n++] = strtoimax(diagString, NULL, 2);
27+
diags[n++] = (int)strtoimax(diagString, NULL, 2);
2828

2929
assert(n < CAP);
3030
}
@@ -58,7 +58,7 @@ static int partTwoRating(int n, const int diags[n], Rating rating) {
5858
int filtered[n];
5959
int filteredCount = n;
6060

61-
memcpy(filtered, diags, sizeof(*diags) * n);
61+
memcpy(filtered, diags, sizeof(*diags) * (uint32_t)n);
6262

6363
int bitMask = 1 << (BIT_WIDTH - 1);
6464

Day04.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void playBingo(Input *input, Bingo *bingo) {
6969
bool firstWin = false;
7070

7171
bool alreadyWonBoards[input->boardsCount];
72-
memset(alreadyWonBoards, false, input->boardsCount * sizeof(bool));
72+
memset(alreadyWonBoards, false, (uint32_t)input->boardsCount * sizeof(bool));
7373

7474
for (int i = 0; i < input->numbersCount; ++i) {
7575
int number = input->numbers[i];

Day05.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int countOverlaps(int h, int w, const int diagram[h][w]) {
9696

9797
static int partOne(int count, const Line lines[count]) {
9898
Line vhLines[count];
99-
memset(vhLines, 0, sizeof(Line) * count);
99+
memset(vhLines, 0, sizeof(Line) * (uint32_t)count);
100100

101101
int countVH = 0;
102102

@@ -109,7 +109,7 @@ static int partOne(int count, const Line lines[count]) {
109109
DiagramSize size = diagramSizeFromLines(countVH, vhLines);
110110

111111
int diagram[size.h][size.w];
112-
memset(diagram, 0, size.w * size.h * sizeof(int));
112+
memset(diagram, 0, (uint32_t)size.w * (uint32_t)size.h * sizeof(int));
113113

114114
drawLines(countVH, vhLines, size.h, size.w, diagram);
115115

@@ -120,7 +120,7 @@ static int partTwo(int count, const Line lines[count]) {
120120
DiagramSize size = diagramSizeFromLines(count, lines);
121121

122122
int diagram[size.h][size.w];
123-
memset(diagram, 0, size.w * size.h * sizeof(int));
123+
memset(diagram, 0, (uint32_t)size.w * (uint32_t)size.h * sizeof(int));
124124

125125
drawLines(count, lines, size.h, size.w, diagram);
126126

Day07.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ static int compareInt(const void *a, const void *b) {
3030

3131
static int partOne(int n, const int xs[n]) {
3232
int sortedXs[n];
33-
memcpy(sortedXs, xs, n * sizeof(xs[0]));
33+
memcpy(sortedXs, xs, (uint32_t)n * sizeof(xs[0]));
3434

35-
qsort(sortedXs, n, sizeof(xs[0]), compareInt); // O(n * log n)
35+
qsort(sortedXs, (uint32_t)n, sizeof(xs[0]), compareInt); // O(n * log n)
3636

3737
int fuels[n];
38-
memset(fuels, 0, n * sizeof(fuels[0]));
38+
memset(fuels, 0, (uint32_t)n * sizeof(fuels[0]));
3939

4040
int midpoint = sortedXs[n / 2]; // Midpoint is the median position
4141
int fuel = 0;

Day09.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static Dim parse(const char *input, uint8_t heightmap[CAP][CAP]) {
2323
int height = c - 48;
2424
assert(height >= 0 && height <= 9);
2525

26-
heightmap[dim.h][x++] = height;
26+
heightmap[dim.h][x++] = (uint8_t)height;
2727
assert(x < CAP);
2828
}
2929

Day10.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void validateSyntax(const char *line, SyntaxResult *result) {
7575
--result->nOpen;
7676
break;
7777
} else {
78-
result->position = line - lineBegin;
78+
result->position = (int)(line - lineBegin);
7979
return;
8080
}
8181
}
@@ -124,7 +124,7 @@ static int64_t partTwo(int n, const char lines[n][SYNTAX_CAP]) {
124124
}
125125
}
126126

127-
qsort(scores, nScores, sizeof(scores[0]), compareInt64); // O(n * log n)
127+
qsort(scores, (uint32_t)nScores, sizeof(scores[0]), compareInt64); // O(n * log n)
128128

129129
return scores[nScores / 2];
130130
}

Day12.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ static CaveId idFromString(const char *s) {
1818
switch (strnlen(s, 6)) {
1919
case 5: return START_CAVE_ID;
2020
case 3: return END_CAVE_ID;
21-
case 2: return (s[0] << 8) | s[1];
22-
case 1: return (s[0] << 8) | s[0];
21+
case 2: return (CaveId)((s[0] << 8) | s[1]);
22+
case 1: return (CaveId)((s[0] << 8) | s[0]);
2323
default: assert(false);
2424
}
2525
}

Day14.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static int parse(const char *input, uint8_t template[TEMPLATE_CAP], uint8_t rule
2727
static int64_t pairFromTemplate(int n, const uint8_t template[n], const uint8_t rules[26][26], int steps) {
2828
// Allocate stack memory for all possible pair combinations for all steps.
2929
int64_t pairsByStep[steps + 1][26][26];
30-
memset(pairsByStep, 0, sizeof(int64_t) * 26 * 26 * (steps + 1));
30+
memset(pairsByStep, 0, sizeof(int64_t) * 26 * 26 * (uint32_t)(steps + 1));
3131

3232
// Insert initial pairs from template into step 0.
3333
for (int i = 0; i < n - 1; ++i) {

Day15.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int parseSize(const char *input) {
8787
++inputFirstLine;
8888
}
8989

90-
return inputFirstLine - input;
90+
return (int)(inputFirstLine - input);
9191
}
9292

9393
static void parseMap(const char *input, int n, uint8_t map[n][n]) {
@@ -100,7 +100,7 @@ static void parseMap(const char *input, int n, uint8_t map[n][n]) {
100100
input += charsRead;
101101

102102
for (int x = 0; x < n; ++x) {
103-
map[y][x] = row[x] - '0';
103+
map[y][x] = (uint8_t)(row[x] - '0');
104104
}
105105
}
106106
}
@@ -114,17 +114,17 @@ static uint32_t aStarSearch(int n, const uint8_t map[n][n]) {
114114

115115
// Cost of the shortest path from start to node n currently known.
116116
uint32_t gScore[n][n];
117-
memset(gScore, UINT32_MAX, n * n * sizeof(gScore[0][0])); // Fill with "infinity".
117+
memset(gScore, INT32_MAX, (uint32_t)n * (uint32_t)n * sizeof(gScore[0][0])); // Fill with "infinity".
118118

119119
// Current best guess to how short a path from start to goal can be if it goes through node n.
120120
uint32_t fScore[n][n];
121-
memset(fScore, UINT32_MAX, n * n * sizeof(fScore[0][0])); // Fill with "infinity".
121+
memset(fScore, INT32_MAX, (uint32_t)n * (uint32_t)n * sizeof(fScore[0][0])); // Fill with "infinity".
122122

123123
// Set initial score to 0 at (0, 0)
124124
gScore[0][0] = 0;
125125

126126
// Best case scenario from (0, 0) to (n - 1, n - 1).
127-
fScore[0][0] = (n - 1 - 0) + (n - 1 - 0); // h((0,0))
127+
fScore[0][0] = (uint32_t)((n - 1 - 0) + (n - 1 - 0)); // h((0,0))
128128

129129
// Insert start node.
130130
PrioQueue_enqueue(fScore[0][0], (0 << 16) | 0, &nodeByLowestPrio);
@@ -158,21 +158,21 @@ static uint32_t aStarSearch(int n, const uint8_t map[n][n]) {
158158
if (tentativeGScore < gScore[ny][nx]) {
159159
// Found a cheaper path than recorded, record this one.
160160
gScore[ny][nx] = tentativeGScore;
161-
fScore[ny][nx] = tentativeGScore + ((n - 1 - ny) + (n - 1 - nx));
161+
fScore[ny][nx] = tentativeGScore + (uint32_t)((n - 1 - ny) + (n - 1 - nx));
162162

163163
// Insert neighbor node. O(log n).
164-
PrioQueue_enqueue(fScore[ny][nx], (ny << 16) | nx, &nodeByLowestPrio);
164+
PrioQueue_enqueue(fScore[ny][nx], (uint32_t)(ny << 16) | nx, &nodeByLowestPrio);
165165
}
166166
}
167167
}
168168
}
169169
}
170170

171-
static int partOne(int n, const uint8_t map[n][n]) {
171+
static uint32_t partOne(int n, const uint8_t map[n][n]) {
172172
return aStarSearch(n, map);
173173
}
174174

175-
static int partTwo(int n, const uint8_t tile[n][n]) {
175+
static uint32_t partTwo(int n, const uint8_t tile[n][n]) {
176176
int m = n * 5;
177177
uint8_t map[m][m];
178178

Day16.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define CAP 10000
44

55
static int parse(const char *input, uint8_t bits[CAP]) {
6-
int nInput = strlen(input);
6+
int nInput = (int)strlen(input);
77

88
int n = 0;
99

@@ -12,7 +12,7 @@ static int parse(const char *input, uint8_t bits[CAP]) {
1212
break;
1313
}
1414

15-
uint8_t nibble = input[i] > '9' ? input[i] - 'A' + 10 : input[i] - '0';
15+
uint8_t nibble = (uint8_t)(input[i] > '9' ? input[i] - 'A' + 10 : input[i] - '0');
1616
assert(nibble >= 0 && nibble <= 15);
1717

1818
for (int j = 3; j >= 0; --j) {
@@ -45,9 +45,9 @@ static int decode(int nBits, const uint8_t bits[nBits], int *versionSum, int64_t
4545
(bits[1] << 1) |
4646
bits[2];
4747

48-
uint8_t typeId = (bits[3] << 2) |
49-
(bits[4] << 1) |
50-
bits[5];
48+
uint8_t typeId = (uint8_t)((bits[3] << 2) |
49+
(bits[4] << 1) |
50+
bits[5]);
5151

5252
int bitIndex = 6;
5353

Day18.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ typedef struct {
1212
} Node;
1313

1414
typedef struct {
15-
int n;
15+
uint16_t n;
1616
Node ns[NODES_CAP];
17-
int nReuse;
17+
uint16_t nReuse;
1818
int reuse[NODES_REUSE_CAP];
1919
} Nodes;
2020

21-
static int parse(const char *input, Nodes *nodes, int numbers[NUMBERS_CAP]) {
21+
static int parse(const char *input, Nodes *nodes, uint16_t numbers[NUMBERS_CAP]) {
2222
int nNumbers = 0;
2323
nodes->n = 1;
2424

25-
int current = nodes->n;
25+
uint16_t current = nodes->n;
2626

2727
while (*input != 0) {
2828
numbers[nNumbers++] = nodes->n;
@@ -40,20 +40,20 @@ static int parse(const char *input, Nodes *nodes, int numbers[NUMBERS_CAP]) {
4040
if (c == '[') {
4141
assert(nodes->ns[current].left == 0 && "Left already occupied");
4242

43-
int left = nodes->n++;
43+
uint16_t left = nodes->n++;
4444
nodes->ns[left].parent = current;
4545

4646
nodes->ns[current].left = left;
4747

4848
current = left;
4949
} else if (c >= '0' && c <= '9') {
50-
nodes->ns[current].value = c - '0';
50+
nodes->ns[current].value = (uint16_t)(c - '0');
5151

5252
current = nodes->ns[current].parent;
5353
} else if (c == ',') {
5454
assert(nodes->ns[current].right == 0 && "Right already occupied");
5555

56-
int right = nodes->n++;
56+
uint16_t right = nodes->n++;
5757
nodes->ns[right].parent = current;
5858

5959
nodes->ns[current].right = right;
@@ -76,14 +76,14 @@ static int parse(const char *input, Nodes *nodes, int numbers[NUMBERS_CAP]) {
7676
return nNumbers;
7777
}
7878

79-
static inline int emptyNodeIndex(Nodes *nodes) {
80-
int i = (nodes->nReuse - 1) & (NODES_REUSE_CAP - 1);
79+
static inline uint16_t emptyNodeIndex(Nodes *nodes) {
80+
uint16_t i = (nodes->nReuse - 1) & (NODES_REUSE_CAP - 1);
8181
int j = nodes->reuse[i];
8282

8383
if (j) {
8484
nodes->reuse[i] = -1;
8585
nodes->nReuse = i;
86-
return j;
86+
return (uint16_t)j;
8787
} else {
8888
return nodes->n++;
8989
}
@@ -94,8 +94,8 @@ static inline void markReusableNodeIndex(int i, Nodes *nodes) {
9494
nodes->nReuse = (nodes->nReuse + 1) & (NODES_REUSE_CAP - 1);
9595
}
9696

97-
static int addition(int ia, int ib, Nodes *nodes) {
98-
int root = emptyNodeIndex(nodes);
97+
static uint16_t addition(uint16_t ia, uint16_t ib, Nodes *nodes) {
98+
uint16_t root = emptyNodeIndex(nodes);
9999

100100
nodes->ns[root].parent = 0;
101101
nodes->ns[root].left = ia;
@@ -220,15 +220,15 @@ static bool split(int i, Nodes *nodes) {
220220
int j = findNodeToSplit(i, nodes);
221221

222222
if (j != -1) {
223-
int left = emptyNodeIndex(nodes);
224-
nodes->ns[left].parent = j;
223+
uint16_t left = emptyNodeIndex(nodes);
224+
nodes->ns[left].parent = (uint16_t)j;
225225
nodes->ns[left].value = nodes->ns[j].value / 2;
226226

227227
assert(nodes->n < NODES_CAP);
228228

229-
int right = emptyNodeIndex(nodes);
230-
nodes->ns[right].parent = j;
231-
nodes->ns[right].value = ((float)nodes->ns[j].value / 2) + 0.5;
229+
uint16_t right = emptyNodeIndex(nodes);
230+
nodes->ns[right].parent = (uint16_t)j;
231+
nodes->ns[right].value = (uint16_t)(((float)nodes->ns[j].value / 2) + 0.5);
232232

233233
assert(nodes->n < NODES_CAP);
234234

@@ -270,11 +270,11 @@ static void dump(int i, const Nodes *nodes) {
270270
}
271271
}
272272

273-
static int partOne(const Nodes *nodes, int n, int numbers[n]) {
273+
static int partOne(const Nodes *nodes, int n, uint16_t numbers[n]) {
274274
Nodes ns;
275275
memcpy(&ns, nodes, sizeof(Nodes));
276276

277-
int number = numbers[0];
277+
uint16_t number = numbers[0];
278278

279279
for (int i = 1; i < n; ++i) {
280280
number = addition(number, numbers[i], &ns);
@@ -286,7 +286,7 @@ static int partOne(const Nodes *nodes, int n, int numbers[n]) {
286286
return magnitude(number, &ns);
287287
}
288288

289-
static int partTwo(const Nodes *nodes, int n, int numbers[n]) {
289+
static int partTwo(const Nodes *nodes, int n, uint16_t numbers[n]) {
290290
Nodes ns;
291291

292292
int largestMagnitude = INT_MIN;
@@ -296,7 +296,7 @@ static int partTwo(const Nodes *nodes, int n, int numbers[n]) {
296296
if (i != j) {
297297
memcpy(&ns, nodes, sizeof(Nodes));
298298

299-
int number = addition(numbers[i], numbers[j], &ns);
299+
uint16_t number = addition(numbers[i], numbers[j], &ns);
300300
reduce(number, &ns);
301301

302302
int m = magnitude(number, &ns);
@@ -315,7 +315,7 @@ int main() {
315315
const char *input = Helpers_readInputFile(__FILE__);
316316

317317
Nodes nodes = {0};
318-
int numbers[NUMBERS_CAP] = {0};
318+
uint16_t numbers[NUMBERS_CAP] = {0};
319319
int n = parse(input, &nodes, numbers);
320320

321321
Helpers_assert(PART1, Helpers_clock(),

0 commit comments

Comments
 (0)