c 2024 day 12 part 2

This commit is contained in:
Bill Rossi 2024-12-12 03:38:38 -05:00
parent 6a191037e0
commit 5987591a2a

View File

@ -45,10 +45,92 @@ int region_perimeter(Region *r) {
return perimeter;
}
int region_inner_corner_count(Region *r) {
int inner_corners = 0;
for (int i = 0; i < r->positions_count; i++) {
int position = r->positions[i];
bool up = false, down = false, left = false, right = false;
bool up_left = false, down_left = false, up_right = false, down_right = false;
if (position - r->map->width - 1 >= 0 && r->map->data[position - r->map->width - 1] == r->plant - 'A' + 'a') {
up_left = true;
}
if (position - r->map->width >= 0 && r->map->data[position - r->map->width] == r->plant - 'A' + 'a') {
up = true;
}
if (position - r->map->width + 1 >= 0 && r->map->data[position - r->map->width + 1] == r->plant - 'A' + 'a') {
up_right = true;
}
if (position - 1 > 0 && r->map->data[position - 1] == r->plant - 'A' + 'a') {
left = true;
}
if (position + 1 < r->map->input_length && r->map->data[position + 1] == r->plant - 'A' + 'a') {
right = true;
}
if (position + r->map->width - 1 < r->map->input_length && r->map->data[position + r->map->width - 1] == r->plant - 'A' + 'a') {
down_left = true;
}
if (position + r->map->width < r->map->input_length && r->map->data[position + r->map->width] == r->plant - 'A' + 'a') {
down = true;
}
if (position + r->map->width + 1 < r->map->input_length && r->map->data[position + r->map->width + 1] == r->plant - 'A' + 'a') {
down_right = true;
}
if (up && left && !up_left) inner_corners++;
if (up && right && !up_right) inner_corners++;
if (down && left && !down_left) inner_corners++;
if (down && right && !down_right) inner_corners++;
}
return inner_corners;
}
int region_outer_corner_count(Region *r) {
int outer_corners = 0;
for (int i = 0; i < r->positions_count; i++) {
int position = r->positions[i];
int fences = 0;
bool top_fence = false, left_fence = false, right_fence = false, bottom_fence = false;
if (position - r->map->width < 0 || r->map->data[position - r->map->width] != r->plant - 'A' + 'a') {
top_fence = true;
}
if (position - 1 < 0 || r->map->data[position - 1] != r->plant - 'A' + 'a') {
left_fence = true;
}
if (position + 1 > r->map->input_length || r->map->data[position + 1] != r->plant - 'A' + 'a') {
right_fence = true;
}
if (position + r->map->width > r->map->input_length || r->map->data[position + r->map->width] != r->plant - 'A' + 'a') {
bottom_fence = true;
}
if (top_fence && left_fence) outer_corners++;
if (bottom_fence && left_fence) outer_corners++;
if (top_fence && right_fence) outer_corners++;
if (bottom_fence && right_fence) outer_corners++;
}
return outer_corners;
}
int region_price(Region *r) {
int rp = region_perimeter(r);
int price = rp * r->positions_count;
return price;
return region_perimeter(r) * r->positions_count;
}
int region_price_2(Region *r) {
int roc = region_outer_corner_count(r);
int ric = region_inner_corner_count(r);
return (roc + ric) * r->positions_count;
}
int main() {
@ -121,14 +203,15 @@ int main() {
}
int cost = 0;
int cost_2 = 0;
for (int i = 0; i < m.regions_count; i++) {
cost += region_price(&m.regions[i]);
// printf("%c: %d\n", m.regions[i].plant, m.regions[i].positions_count);
cost_2 += region_price_2(&m.regions[i]);
}
printf("Part 1: %d\n", cost);
// printf("Part 2: %lld\n", big_stone_count_sum);
printf("Part 2: %lld\n", cost_2);
aoc_free();
return 0;