This commit is contained in:
Evan Burkey 2024-12-05 10:33:43 -08:00
parent 69bed5d6a8
commit e9b7b4becf
3 changed files with 89 additions and 16 deletions

View File

@ -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);

View File

@ -1,10 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

View File

@ -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;
}