Skip to content

Commit

Permalink
Day 3: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 3, 2023
1 parent 6b06e4d commit 14967ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 81 deletions.
1 change: 1 addition & 0 deletions include/aoc/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
(x) = (y); \
(y) = temp; \
} while (0)
#define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))
104 changes: 23 additions & 81 deletions src/day03/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
#define MAX_SYMBOLS 4096
#define EMPTY -1

typedef struct {
int x;
int y;
char value;
} Symbol;

static inline bool is_symbol(char s) {
if (s >= '0' && s <= '9') { return false; }
if (s == '.') { return false; }
return true;
return !(s >= '0' && s <= '9') && s != '.';
}

static int collect(int8_t numbers[MAX_NUMBERS][MAX_NUMBERS], int cols,
int candidate_x, int candidate_y) {
static inline int collect(int8_t numbers[MAX_NUMBERS][MAX_NUMBERS], int cols,
int candidate_x, int candidate_y) {
// go left as far as possible to find start digit
while (candidate_x - 1 >= 0 &&
numbers[candidate_y][candidate_x - 1] != EMPTY) {
Expand All @@ -30,12 +34,6 @@ static int collect(int8_t numbers[MAX_NUMBERS][MAX_NUMBERS], int cols,
return value;
}

typedef struct {
int x;
int y;
char value;
} Symbol;

void solve(const char *buf, size_t buf_size, Solution *result) {
int cols = 0;
while (buf[cols] != '\n') cols++;
Expand Down Expand Up @@ -71,82 +69,26 @@ void solve(const char *buf, size_t buf_size, Solution *result) {
int values[8];
int value_idx = 0;

{ // top left
int candidate_y = y - 1;
int candidate_x = x - 1;
if (candidate_y >= 0 && candidate_x >= 0 &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}
{ // top
int candidate_y = y - 1;
int candidate_x = x;
if (candidate_y >= 0 &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}
{ // top right
int candidate_y = y - 1;
int candidate_x = x + 1;
if (candidate_y >= 0 && candidate_x < cols &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}

{ // left
int candidate_y = y;
int candidate_x = x - 1;
if (candidate_x >= 0 &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}
{ // right
int candidate_y = y;
int candidate_x = x + 1;
if (candidate_x < cols &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}
Point2D deltas[] = {
{-1, -1}, // top left
{0, -1}, // top
{1, -1}, // top right
{-1, 0}, // left
{1, 0}, // right
{-1, 1}, // bottom left
{0, 1}, // bottom
{1, 1} // bottom right
};

{ // bottom left
int candidate_y = y + 1;
int candidate_x = x - 1;
if (candidate_y < rows && candidate_x >= 0 &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}
{ // bottom
int candidate_y = y + 1;
int candidate_x = x;
if (candidate_y < rows &&
for (size_t i = 0; i < ARRAY_LENGTH(deltas); i++) {
int candidate_x = x + deltas[i].x, candidate_y = y + deltas[i].y;
if (candidate_x >= 0 && candidate_y >= 0 && candidate_x < cols &&
candidate_y < rows &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
}
}
{ // bottom right
int candidate_y = y + 1;
int candidate_x = x + 1;
if (candidate_y < rows && candidate_x < cols &&
numbers[candidate_y][candidate_x] != EMPTY) {
values[value_idx++] =
collect(numbers, cols, candidate_x, candidate_y);
;
}
}

for (int i = 0; i < value_idx; i++) { part1 += values[i]; }
if (is_star && value_idx == 2) { part2 += values[0] * values[1]; }
}
Expand Down

0 comments on commit 14967ae

Please sign in to comment.