From e9b7b4becf3ee3fa627f7baa1e813667c712f7c3 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Thu, 5 Dec 2024 10:33:43 -0800 Subject: [PATCH] 2024-04 --- src/2024/02.c | 12 +----- src/2024/04.c | 92 +++++++++++++++++++++++++++++++++++++++++--- src/advent_utility.c | 1 + 3 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/2024/02.c b/src/2024/02.c index 98d1e33..9617282 100644 --- a/src/2024/02.c +++ b/src/2024/02.c @@ -102,17 +102,7 @@ void advent2024day02(void) { } p1 += part1(a); - //p2 += part2(a); - - int s = part2(a); - if (s) { - LL_ITER(a) { - int *str = node->data; - printf("%d ", *str); - } - printf("\n"); - p2 += s; - } + p2 += part2(a); free(sp); ll_destroy(a); diff --git a/src/2024/04.c b/src/2024/04.c index e93100d..f09c6e0 100644 --- a/src/2024/04.c +++ b/src/2024/04.c @@ -1,10 +1,92 @@ #include -#include +#include #include "lfinput.h" -void advent2024day04(void) { - char *input = get_input("input/2024/04"); - printf("Solution for Day 04 of 2024 is not completed yet\n"); - free(input); +#define GRID_SZ 140 +char **gd; + +static int horz(int x, int y, const char *w) { + if (y <= GRID_SZ - 4) { + return strncmp(&gd[x][y], w, 4) == 0; + } + return 0; +} + +static int vert(int x, int y, const char *w) { + if (x <= GRID_SZ - 4) { + int c = 1; + for (int j = 0; j < 4; j++) { + if (gd[x + j][y] != w[j]) { + c = 0; + } + } + return c; + } + return 0; +} + +static int diag_right(int x, int y, const char *w) { + if (x <= GRID_SZ - 4 && y >= 3) { + int c = 1; + for (int j = 0; j < 4; j++) { + if (gd[x + j][y - j] != w[j]) { + c = 0; + } + } + return c; + } + return 0; +} + +static int diag_left(int x, int y, const char *w) { + if (x <= GRID_SZ - 4 && y <= GRID_SZ - 4) { + int c = 1; + for (int j = 0; j < 4; j++) { + if (gd[x + j][y + j] != w[j]) { + c = 0; + } + } + return c; + } + return 0; +} + +void advent2024day04(void) { + int p1 = 0, p2 = 0; + size_t sz = GRID_SZ; + gd = get_lines("input/2024/04", &sz); + + for (size_t x = 0; x < sz; ++x) { + for (size_t y = 0; y < sz; ++y) { + p1 += horz(x, y, "XMAS"); + p1 += horz(x, y, "SAMX"); + p1 += vert(x, y, "XMAS"); + p1 += vert(x, y, "SAMX"); + p1 += diag_left(x, y, "XMAS"); + p1 += diag_left(x, y, "SAMX"); + p1 += diag_right(x, y, "XMAS"); + p1 += diag_right(x, y, "SAMX"); + } + } + + printf("%d\n", p1); + + for (size_t x = 1; x < sz - 1; ++x) { + for (size_t y = 1; y < sz - 1; ++y) { + if (gd[x][y] == 'A') { + int c = 0; + if ((gd[x - 1][y - 1] == 'M' && gd[x + 1][y + 1] == 'S') || (gd[x - 1][y - 1] == 'S' && gd[x + 1][y + 1] == 'M')) { + if ((gd[x + 1][y - 1] == 'M' && gd[x - 1][y + 1] == 'S') || (gd[x + 1][y - 1] == 'S' && gd[x - 1][y + 1] == 'M')) { + c = 1; + } + } + p2 += c; + } + } + } + + printf("%d\n", p2); + + del_lines(gd); } diff --git a/src/advent_utility.c b/src/advent_utility.c index 6414f50..e8a942c 100644 --- a/src/advent_utility.c +++ b/src/advent_utility.c @@ -143,6 +143,7 @@ char **get_matches(char *in, char *pat, size_t *sz, size_t max_matches) { printf("PCRE2 match-matching error %d\n", rc); pcre2_match_data_free(match_data); pcre2_code_free(re); + free_matches(matches, *sz); return NULL; }