c 2024 day 10 part 2

This commit is contained in:
Bill Rossi 2024-12-10 01:00:29 -05:00
parent f941950ecf
commit 80a315131b

View File

@ -15,7 +15,7 @@ typedef struct Map {
int positions_count;
} Map;
void ascend(Map *map) {
void ascend(Map *map, bool dedupe) {
char search_for = map->current_level + 1 + '0';
int positions[10000];
int positions_count = 0;
@ -27,31 +27,37 @@ void ascend(Map *map) {
if (p + map->width < map->data_length && map->data[p + map->width] == search_for) positions[positions_count++] = p + map->width;
}
qsort(positions, positions_count, sizeof(int), aoc_sort_int);
if (dedupe) {
qsort(positions, positions_count, sizeof(int), aoc_sort_int);
int deduped_positions[10000];
int deduped_positions_count = 0;
int deduped_positions[10000];
int deduped_positions_count = 0;
for (int i = 0; i < positions_count - 1; i++) {
if (positions[i] != positions[i+1]) {
deduped_positions[deduped_positions_count++] = positions[i];
for (int i = 0; i < positions_count - 1; i++) {
if (positions[i] != positions[i+1]) {
deduped_positions[deduped_positions_count++] = positions[i];
}
}
}
deduped_positions[deduped_positions_count++] = positions[positions_count - 1];
deduped_positions[deduped_positions_count++] = positions[positions_count - 1];
map->positions_count = deduped_positions_count;
memcpy(&(map->positions), &deduped_positions, deduped_positions_count * sizeof(int));
map->current_level++;
map->positions_count = deduped_positions_count;
memcpy(&(map->positions), &deduped_positions, deduped_positions_count * sizeof(int));
map->current_level++;
} else {
map->positions_count = positions_count;
memcpy(&(map->positions), &positions, positions_count * sizeof(int));
map->current_level++;
}
}
int calculate_score(Map *map, int index) {
int calculate_score(Map *map, int index, bool dedupe) {
if (map->data[index] != '0') return 0;
map->current_level = 0;
map->positions[0] = index;
map->positions_count = 1;
for (int i = 0; i < 9; i++) {
ascend(map);
ascend(map, dedupe);
}
return map->positions_count;
}
@ -67,13 +73,15 @@ int main() {
m.height = input_length - m.width;
int score = 0;
int big_score = 0;
for (int i = 0; i < input_length; i++) {
score += calculate_score(&m, i);
score += calculate_score(&m, i, true);
big_score += calculate_score(&m, i, false);
}
printf("Part 1: %ld\n", score);
// printf("Part 2: %ld\n", second_checksum);
printf("Part 2: %ld\n", big_score);
aoc_free();
return 0;