c 2024 day 5 part 1

This commit is contained in:
Bill Rossi 2024-12-05 01:07:31 -05:00
parent 8e5f922f63
commit f71ba6fb32

83
c/2024/5/print_queue.c Normal file
View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "../../lib/aoc.h"
typedef struct Rule {
int first;
int last;
} Rule;
typedef struct Rules {
Rule rule[1200];
int length;
} Rules;
typedef struct Update {
int page[100];
int length;
} Update;
bool valid_rule(Rule *rule, Update *update) {
int first = -1, last = -1;
for (int i = 0; i < update->length; i++) {
if (rule->first == update->page[i]) first = i;
if (rule->last == update->page[i]) last = i;
if (first != -1 && last != -1) return first < last;
}
return true;
}
bool valid_update(Rules *rules, Update *update) {
for (int i = 0; i < rules->length; i++) {
if (!valid_rule(&(rules->rule[i]), update)) return false;
}
return true;
}
int middle_page_number(Update *update) {
return update->page[update->length / 2];
}
int main() {
char *line;
int sum = 0;
Rules rules;
int rule_index = 0;
int first, last;
Update update;
char *update_element;
bool scanning_rules = true;
while ((line = aoc_read_line()) != NULL) {
if (scanning_rules) {
if (strlen(line) == 0) scanning_rules = false;
else {
sscanf(line, "%d|%d", &first, &last);
rules.rule[rules.length].first = first;
rules.rule[rules.length].last = last;
rules.length++;
}
} else {
update.length = 0;
update_element = strtok(line, ",");
while(update_element != NULL) {
update.page[update.length++] = atoi(update_element);
update_element = strtok(NULL, ",");
}
if (valid_update(&rules, &update)) sum += middle_page_number(&update);
}
}
printf("Part 1: %d\n", sum);
// printf("Part 2: %d\n", xmas_sum);
aoc_free();
return 0;
}