C 2024 day 19 part 1
This commit is contained in:
parent
62ae713bd2
commit
f5f0d6328c
49
c/2024/19/linen_layout.c
Normal file
49
c/2024/19/linen_layout.c
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user