diff --git a/c/2024/19/linen_layout.c b/c/2024/19/linen_layout.c new file mode 100644 index 0000000..1f720d0 --- /dev/null +++ b/c/2024/19/linen_layout.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include "../../lib/aoc.h" + +typedef char Towel[10]; + +typedef struct Towels { + Towel towels[2000]; + int towels_count; +} Towels; + +bool towels_valid_design(Towels *t, char *design) { + if (strlen(design) == 0) return true; + for (int i = 0; i < t->towels_count; i++) { + if (strstr(design, t->towels[i]) == design) { + if (towels_valid_design(t, design + strlen(t->towels[i]))) return true; + } + } + return false; +} + +int main() { + char *line; + + Towels towels; + towels.towels_count = 0; + + line = aoc_read_line(); + char *token = strtok(line, ", "); + while (token != NULL) { + strcpy(towels.towels[towels.towels_count++], token); + token = strtok(NULL, ", "); + } + + aoc_read_line(); // blank line + + int valid_designs = 0; + while ((line = aoc_read_line()) != NULL) { + if (towels_valid_design(&towels, line)) valid_designs++; + } + + printf("Part 1: %d\n", valid_designs); + + aoc_free(); + exit(0); +}