80 lines
1.8 KiB
C
80 lines
1.8 KiB
C
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
char name;
|
||
|
|
bool completed;
|
||
|
|
char requirements[50];
|
||
|
|
char requirements_count;
|
||
|
|
} Step;
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
Step steps[50];
|
||
|
|
int size;
|
||
|
|
} StepList;
|
||
|
|
|
||
|
|
Step *step_list_find_step(StepList *list, char key) {
|
||
|
|
for (int i = 0; i < list->size; i++) {
|
||
|
|
if (list->steps[i].name == key) {
|
||
|
|
return &(list->steps[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
void step_list_add_step(StepList *list, char name) {
|
||
|
|
list->steps[list->size++].name = name;
|
||
|
|
}
|
||
|
|
|
||
|
|
void step_add_requirement(Step *step, char requirement) {
|
||
|
|
step->requirements[step->requirements_count++] = requirement;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read_line(FILE *input, StepList *list) {
|
||
|
|
char name;
|
||
|
|
char requirement;
|
||
|
|
bool success = fscanf(input, "Step %c must be finished before step %c can begin.\n", &name, &requirement) == 2;
|
||
|
|
if (!success) return false;
|
||
|
|
|
||
|
|
Step *step = step_list_find_step(list, name);
|
||
|
|
if (step == NULL) {
|
||
|
|
step_list_add_step(list, name);
|
||
|
|
step = step_list_find_step(list, name);
|
||
|
|
}
|
||
|
|
step_add_requirement(step, requirement);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
int part_1(FILE *input) {
|
||
|
|
rewind(input);
|
||
|
|
StepList step_list = {0};
|
||
|
|
while (read_line(input, &step_list)) {}
|
||
|
|
for (int i = 0; i < step_list.size; i++) {
|
||
|
|
Step step = step_list.steps[i];
|
||
|
|
for (int j = 0; j < step.requirements_count; j++) {
|
||
|
|
printf("Step %c must be finished before step %c can begin.\n", step.name, step.requirements[j]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
int part_2(FILE *input) {
|
||
|
|
rewind(input);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
char *input_file_path = "../data/2018/7/input.txt";
|
||
|
|
FILE *input = fopen(input_file_path, "r");
|
||
|
|
if (input == NULL) {
|
||
|
|
printf("Can't open file %s\n", input_file_path);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
printf("Part 1: %d\n", part_1(input));
|
||
|
|
printf("Part 2: %d\n", part_2(input));
|
||
|
|
fclose(input);
|
||
|
|
}
|