c 2024 day 5 part 2
This commit is contained in:
parent
f71ba6fb32
commit
5036432898
@ -42,10 +42,29 @@ int middle_page_number(Update *update) {
|
||||
return update->page[update->length / 2];
|
||||
}
|
||||
|
||||
void reorder_update(Rules *rules, Update *update) {
|
||||
bool dirty = true;
|
||||
while (dirty) {
|
||||
dirty = false;
|
||||
for (int i = 0; i < update->length - 1; i++) {
|
||||
for (int j = 0; j < rules->length; j++) {
|
||||
Rule rule = rules->rule[j];
|
||||
if (update->page[i] == rule.last && update->page[i + 1] == rule.first) {
|
||||
dirty = true;
|
||||
update->page[i] = rule.first;
|
||||
update->page[i + 1] = rule.last;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char *line;
|
||||
|
||||
int sum = 0;
|
||||
int reorder_sum = 0;
|
||||
|
||||
Rules rules;
|
||||
int rule_index = 0;
|
||||
@ -72,11 +91,15 @@ int main() {
|
||||
update_element = strtok(NULL, ",");
|
||||
}
|
||||
if (valid_update(&rules, &update)) sum += middle_page_number(&update);
|
||||
else {
|
||||
reorder_update(&rules, &update);
|
||||
reorder_sum += middle_page_number(&update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Part 1: %d\n", sum);
|
||||
// printf("Part 2: %d\n", xmas_sum);
|
||||
printf("Part 2: %d\n", reorder_sum);
|
||||
|
||||
aoc_free();
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user