Skip to content

Commit

Permalink
day 4, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 4, 2023
1 parent 6c72ca3 commit 1e14b2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
26 changes: 18 additions & 8 deletions src/day04/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/day04/solve_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1e14b2b

Please sign in to comment.