#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);
}