diff --git a/src/day04/solve.c b/src/day04/solve.c index 2a123f1..f745ecd 100644 --- a/src/day04/solve.c +++ b/src/day04/solve.c @@ -39,22 +39,32 @@ void solve(const char *buf, size_t buf_size, Solution *result) { int_tim_sort(winning_cards, winning_cards_idx); int_tim_sort(mine_cards, mine_cards_idx); - for (int i = 0; i < winning_cards_idx; i++) { - printf("%d ", winning_cards[i]); + int i = 0, j = 0; + int count = 0; + while (i < winning_cards_idx && j < mine_cards_idx) { + if (winning_cards[i] == mine_cards[j]) { + log_debug("match: %d ", winning_cards[i]); + count++; + i++; + j++; + } else if (winning_cards[i] < mine_cards[j]) { + i++; + } else { + j++; + } } - printf("\n"); - for (int i = 0; i < mine_cards_idx; i++) { - printf("%d ", mine_cards[i]); + if (count > 0) { + int score = 1 << (count - 1); + log_debug("count: %d, score: %d", count, score); + part1 += score; } - printf("\n"); - // TODO: solve it } stbsp_snprintf(result->part1, sizeof(result->part1), "%d", part1); stbsp_snprintf(result->part2, sizeof(result->part2), "%d", part2); } int solve_input(const char *fname, Solution *result) { - char buf[1 << 14]; + char buf[1 << 15]; int n = read_input(fname, buf, sizeof(buf)); if (n <= 0) { fprintf(stderr, "Failed to read %s\n", fname); diff --git a/src/day04/solve_test.c b/src/day04/solve_test.c index a7c5f76..2d5635d 100644 --- a/src/day04/solve_test.c +++ b/src/day04/solve_test.c @@ -17,11 +17,12 @@ Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11\n"; } #ifdef HAVE_INPUTS -CTEST_SKIP(day04, real) { +CTEST(day04, real) { Solution solution; solve_input("input/" DAY ".txt", &solution); - ASSERT_STR("0", solution.part1); - ASSERT_STR("0", solution.part2); + ASSERT_NOT_STR("199", solution.part1); // too low + // ASSERT_STR("0", solution.part1); + // ASSERT_STR("0", solution.part2); } #endif