Skip to content

Commit 3c263fd

Browse files
committed
Solve day 4 part 2
1 parent 79e763b commit 3c263fd

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

day04/src/day04.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,57 @@
33
#include <stdlib.h>
44
#include <string.h>
55

6-
const char XMAS[] = "XMAS";
7-
86
#define GRID_SIZE 140
9-
#define XMAS_LENGTH (sizeof(XMAS) / sizeof(char) - 1)
7+
#define LENGTH(S) (sizeof(S) / sizeof(char) - 1)
108

119
struct Grid {
1210
char data[GRID_SIZE][GRID_SIZE];
1311
int width;
1412
int height;
1513
};
1614

15+
struct Solution {
16+
int part1;
17+
int part2;
18+
};
19+
1720
bool in_bounds(struct Grid grid, int i, int j) {
1821
return i >= 0 && i < grid.height && j >= 0 && j < grid.width;
1922
}
2023

21-
bool has_xmas_at(struct Grid grid, int i, int j, int di, int dj) {
22-
for (int k = 0; k < XMAS_LENGTH; k++) {
24+
bool matches_at(struct Grid grid, int i, int j, int di, int dj, const char pattern[], int length) {
25+
for (int k = 0; k < length; k++) {
2326
int ni = i + k * di;
2427
int nj = j + k * dj;
25-
if (!in_bounds(grid, ni, nj) || grid.data[ni][nj] != XMAS[k]) {
28+
if (!in_bounds(grid, ni, nj) || grid.data[ni][nj] != pattern[k]) {
2629
return false;
2730
}
2831
}
2932
return true;
3033
}
3134

32-
int count_xmas_at(struct Grid grid, int i, int j) {
33-
return has_xmas_at(grid, i, j, 1, 0) + has_xmas_at(grid, i, j, 0, 1)
34-
+ has_xmas_at(grid, i, j, -1, 0) + has_xmas_at(grid, i, j, 0, -1)
35-
+ has_xmas_at(grid, i, j, 1, 1) + has_xmas_at(grid, i, j, -1, -1)
36-
+ has_xmas_at(grid, i, j, -1, 1) + has_xmas_at(grid, i, j, 1, -1);
35+
bool matches_xmas_at(struct Grid grid, int i, int j, int di, int dj) {
36+
const char XMAS[] = "XMAS";
37+
return matches_at(grid, i, j, di, dj, XMAS, LENGTH(XMAS));
38+
}
39+
40+
int count_xmas1_at(struct Grid grid, int i, int j) {
41+
return matches_xmas_at(grid, i, j, 1, 0) + matches_xmas_at(grid, i, j, 0, 1)
42+
+ matches_xmas_at(grid, i, j, -1, 0) + matches_xmas_at(grid, i, j, 0, -1)
43+
+ matches_xmas_at(grid, i, j, 1, 1) + matches_xmas_at(grid, i, j, -1, -1)
44+
+ matches_xmas_at(grid, i, j, -1, 1) + matches_xmas_at(grid, i, j, 1, -1);
45+
}
46+
47+
bool matches_mas_at(struct Grid grid, int i, int j, int di, int dj) {
48+
const char MAS[] = "MAS";
49+
return matches_at(grid, i, j, di, dj, MAS, LENGTH(MAS));
50+
}
51+
52+
bool has_xmas2_at(struct Grid grid, int i, int j) {
53+
return (matches_mas_at(grid, i - 1, j - 1, 1, 1) && matches_mas_at(grid, i + 1, j - 1, -1, 1))
54+
|| (matches_mas_at(grid, i + 1, j - 1, -1, 1) && matches_mas_at(grid, i + 1, j + 1, -1, -1))
55+
|| (matches_mas_at(grid, i - 1, j - 1, 1, 1) && matches_mas_at(grid, i - 1, j + 1, 1, -1))
56+
|| (matches_mas_at(grid, i - 1, j + 1, 1, -1) && matches_mas_at(grid, i + 1, j + 1, -1, -1));
3757
}
3858

3959
struct Grid parse_grid(FILE *input) {
@@ -54,14 +74,15 @@ struct Grid parse_grid(FILE *input) {
5474
return grid;
5575
}
5676

57-
int count_xmas(struct Grid grid) {
58-
int count = 0;
77+
struct Solution solve(struct Grid grid) {
78+
struct Solution solution = { .part1 = 0, .part2 = 0 };
5979
for (int i = 0; i < grid.height; i++) {
6080
for (int j = 0; j < grid.width; j++) {
61-
count += count_xmas_at(grid, i, j);
81+
solution.part1 += count_xmas1_at(grid, i, j);
82+
solution.part2 += has_xmas2_at(grid, i, j);
6283
}
6384
}
64-
return count;
85+
return solution;
6586
}
6687

6788
int main(int argc, const char *argv[]) {
@@ -81,8 +102,9 @@ int main(int argc, const char *argv[]) {
81102
printf("Grid is of width %d and height %d\n", grid.width, grid.height);
82103
fclose(input);
83104

84-
int part1 = count_xmas(grid);
85-
printf("Part 1: %d\n", part1);
105+
struct Solution solution = solve(grid);
106+
printf("Part 1: %d\n", solution.part1);
107+
printf("Part 2: %d\n", solution.part2);
86108

87109
return 0;
88110
}

0 commit comments

Comments
 (0)