From 14967ae07e7195a0336a3f2a51347aa779c34946 Mon Sep 17 00:00:00 2001 From: Michael Adler Date: Sun, 3 Dec 2023 22:43:41 +0100 Subject: [PATCH] Day 3: Refactor --- include/aoc/macros.h | 1 + src/day03/solve.c | 104 ++++++++++--------------------------------- 2 files changed, 24 insertions(+), 81 deletions(-) diff --git a/include/aoc/macros.h b/include/aoc/macros.h index 3596ee1..b31c940 100644 --- a/include/aoc/macros.h +++ b/include/aoc/macros.h @@ -31,3 +31,4 @@ (x) = (y); \ (y) = temp; \ } while (0) +#define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0])) diff --git a/src/day03/solve.c b/src/day03/solve.c index d1b87ce..2bd1d25 100644 --- a/src/day03/solve.c +++ b/src/day03/solve.c @@ -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) { @@ -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++; @@ -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]; } }