2024-05
This commit is contained in:
parent
e9b7b4becf
commit
2aeb547987
@ -9,13 +9,19 @@
|
|||||||
#define MIN(x, y) (x) < (y) ? (x) : (y)
|
#define MIN(x, y) (x) < (y) ? (x) : (y)
|
||||||
|
|
||||||
char *md5_str(const char *);
|
char *md5_str(const char *);
|
||||||
|
|
||||||
Vector *string_to_int_vector(const char *input_string, const char *delim);
|
Vector *string_to_int_vector(const char *input_string, const char *delim);
|
||||||
|
|
||||||
int int_comp(const void *a, const void *b);
|
int int_comp(const void *a, const void *b);
|
||||||
|
|
||||||
char **get_matches(char *in, char *pat, size_t *sz, size_t max_matches);
|
char **get_matches(char *in, char *pat, size_t *sz, size_t max_matches);
|
||||||
|
|
||||||
void free_matches(char **matches, size_t sz);
|
void free_matches(char **matches, size_t sz);
|
||||||
|
|
||||||
|
enum Direction {
|
||||||
|
DIR_NORTH = 0,
|
||||||
|
DIR_EAST = 1,
|
||||||
|
DIR_SOUTH = 2,
|
||||||
|
DIR_WEST = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
void turn_right(enum Direction *d);
|
||||||
|
void turn_left(enum Direction *d);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,10 +1,94 @@
|
|||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lfinput.h"
|
#include "lfinput.h"
|
||||||
|
|
||||||
void advent2024day05(void) {
|
#define RULES_SZ 1176
|
||||||
char *input = get_input("input/2024/05");
|
|
||||||
printf("Solution for Day 05 of 2024 is not completed yet\n");
|
struct Rule {
|
||||||
free(input);
|
int a;
|
||||||
|
int b;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int part_one(int *pages, size_t sz, struct Rule *rules) {
|
||||||
|
int midpoint = pages[sz / 2];
|
||||||
|
|
||||||
|
for (size_t ridx = 0; ridx < RULES_SZ; ++ridx) {
|
||||||
|
int a = -1;
|
||||||
|
int b = 500;
|
||||||
|
|
||||||
|
for (int pidx = 0; pidx < sz; ++pidx) {
|
||||||
|
if (pages[pidx] == rules[ridx].a) {
|
||||||
|
a = pidx;
|
||||||
|
}
|
||||||
|
if (pages[pidx] == rules[ridx].b) {
|
||||||
|
b = pidx;
|
||||||
|
}
|
||||||
|
if (a > b) {
|
||||||
|
midpoint = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return midpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rule_matrix[100][100] = {0};
|
||||||
|
|
||||||
|
static int sort(const void *a, const void *b) {
|
||||||
|
int ia = *(int*)a;
|
||||||
|
int ib = *(int*)b;
|
||||||
|
if (rule_matrix[ia][ib]) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (rule_matrix[ib][ia]) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int part_two(int *pages, size_t sz, struct Rule *rules) {
|
||||||
|
int s = 0;
|
||||||
|
int sorted[100];
|
||||||
|
memcpy(sorted, pages, sizeof(int) * sz);
|
||||||
|
qsort(sorted, sz, sizeof(int), sort);
|
||||||
|
if (memcmp(pages, sorted, sz * sizeof(int)) != 0) {
|
||||||
|
s += sorted[sz / 2];
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void advent2024day05(void) {
|
||||||
|
size_t sz = 0;
|
||||||
|
char **input = get_lines("input/2024/05", &sz);
|
||||||
|
char *errstr = NULL;
|
||||||
|
struct Rule rules[RULES_SZ];
|
||||||
|
int p1 = 0, p2 = 0;
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
for (; strlen(input[i]) == 5; ++i) {
|
||||||
|
sscanf(input[i], "%d|%d", &rules[i].a, &rules[i].b);
|
||||||
|
rule_matrix[rules[i].a][rules[i].b] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i < sz; ++i) {
|
||||||
|
size_t sz_sp = 0;
|
||||||
|
char **sp = split(input[i], &sz_sp, ",");
|
||||||
|
int *pages = malloc(sizeof(int) * sz_sp);
|
||||||
|
for (size_t j = 0; j < sz_sp; ++j) {
|
||||||
|
pages[j] = (int) strtonum(sp[j], INT_MIN, INT_MAX, &errstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
p1 += part_one(pages, sz_sp, rules);
|
||||||
|
p2 += part_two(pages, sz_sp, rules);
|
||||||
|
|
||||||
|
free(pages);
|
||||||
|
free(sp);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d\n", p1);
|
||||||
|
printf("%d\n", p2);
|
||||||
|
|
||||||
|
del_lines(input);
|
||||||
}
|
}
|
||||||
|
@ -167,4 +167,12 @@ void free_matches(char **matches, size_t sz) {
|
|||||||
free(matches[i]);
|
free(matches[i]);
|
||||||
}
|
}
|
||||||
free(matches);
|
free(matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void turn_right(enum Direction *d) {
|
||||||
|
*d = *d == DIR_WEST ? DIR_NORTH : *d + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void turn_left(enum Direction *d) {
|
||||||
|
*d = *d == DIR_NORTH ? DIR_WEST : *d - 1;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user