Skip to content

Commit

Permalink
Day 11, Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 11, 2023
1 parent bc492ed commit 485ab8b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Compiled using clang 16 and LTO.
| 8 | 462.4 µs | 1 ms |
| 9 | 256 µs | 464 µs |
| 10 | 5.8 ms | 16.4 ms |
| 11 | | 1.7 ms |

## 🙏 Acknowledgments and Resources

Expand Down
32 changes: 22 additions & 10 deletions src/day11/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

#define MAX_POINTS 1024

static const i64 expand_p2 = 1000000 - 1;

void solve(const char *buf, size_t buf_size, Solution *result) {

i64 part1 = 0, part2 = 0;
size_t pos = 0;

Expand Down Expand Up @@ -58,29 +61,38 @@ void solve(const char *buf, size_t buf_size, Solution *result) {
Point2D a = point[i];
for (int j = i + 1; j < point_count; j++) {
Point2D b = point[j];
i32 dist = Point2D_manhattan(a, b);
i64 p1_dist = Point2D_manhattan(a, b);
i64 p2_dist = p1_dist;

{ // account for empty cols
i32 count = 0;
i32 lower_x = MIN(a.x, b.x);
i32 upper_x = MAX(a.x, b.x);
i64 p1_count = 0, p2_count = 0;
i32 lower_x = MIN(a.x, b.x), upper_x = MAX(a.x, b.x);
for (i32 k = 0; k < empty_col_count; k++) {
i32 c = empty_col[k];
if (c > lower_x && c < upper_x) count++;
if (c > lower_x && c < upper_x) {
p1_count++;
p2_count += expand_p2;
}
}
dist += count;
p1_dist += p1_count;
p2_dist += p2_count;
}
{ // account for empty rows
i32 count = 0;
i64 p1_count = 0, p2_count = 0;
i32 lower_y = MIN(a.y, b.y);
i32 upper_y = MAX(a.y, b.y);
for (i32 k = 0; k < empty_row_count; k++) {
i32 r = empty_row[k];
if (r > lower_y && r < upper_y) count++;
if (r > lower_y && r < upper_y) {
p1_count++;
p2_count += expand_p2;
}
}
dist += count;
p1_dist += p1_count;
p2_dist += p2_count;
}
part1 += dist;
part1 += p1_dist;
part2 += p2_dist;
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/day11/solve_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ CTEST(day11, example) {
Solution solution;
solve(buf, strlen(buf), &solution);
ASSERT_STR("374", solution.part1);
// ASSERT_STR("0", solution.part2);
}

#ifdef HAVE_INPUTS
CTEST_SKIP(day11, real) {
CTEST(day11, real) {
Solution solution;
solve_input("input/" DAY ".txt", &solution);
ASSERT_STR("9274989", solution.part1);
// ASSERT_STR("0", solution.part2);
ASSERT_STR("357134560737", solution.part2);
}
#endif

Expand Down

0 comments on commit 485ab8b

Please sign in to comment.