Make AocHashmap more versatile

This commit is contained in:
Bill Rossi 2024-12-25 11:58:51 -05:00
parent bd3ea98f7b
commit 1612cd1f04
2 changed files with 6 additions and 6 deletions

View File

@ -25,20 +25,20 @@ bool towels_valid_design(Towels *t, char *design) {
long towels_valid_designs(Towels *t, char *design) { long towels_valid_designs(Towels *t, char *design) {
AocHashmapNode *memo = aoc_hashmap_get(t->map, design); AocHashmapNode *memo = aoc_hashmap_get(t->map, design);
if (memo != NULL) return memo->value; if (memo != NULL) return *(long *)memo->value;
if (strlen(design) == 0) return 1; if (strlen(design) == 0) return 1;
long valid_designs = 0; long *valid_designs = malloc(sizeof(long));
for (int i = 0; i < t->towels_count; i++) { for (int i = 0; i < t->towels_count; i++) {
if (strstr(design, t->towels[i]) == design) { if (strstr(design, t->towels[i]) == design) {
valid_designs += towels_valid_designs(t, design + strlen(t->towels[i])); *valid_designs += towels_valid_designs(t, design + strlen(t->towels[i]));
} }
} }
aoc_hashmap_put(t->map, design, valid_designs); aoc_hashmap_put(t->map, design, valid_designs);
return valid_designs; return *valid_designs;
} }
int main() { int main() {

View File

@ -16,7 +16,7 @@ typedef struct AocHashmapNode AocHashmapNode;
typedef struct AocHashmapNode { typedef struct AocHashmapNode {
char *key; char *key;
long value; void *value;
AocHashmapNode *next; AocHashmapNode *next;
} AocHashmapNode; } AocHashmapNode;
@ -31,7 +31,7 @@ AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) {
return node; return node;
} }
void *aoc_hashmap_put(AocHashmap *map, char *key, long value) { void *aoc_hashmap_put(AocHashmap *map, char *key, void *value) {
int hash = aoc_hash(key); int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash]; AocHashmapNode *node = map->buckets[hash];
while (node != NULL) node = node->next; while (node != NULL) node = node->next;