advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs

commit d6019316d35a75a2bbdb94fbb8d615c6fd8fd08b
parent 0cb07f7525c02b174048f0e3c5698314af318979
Author: bsandro <brian.drosan@gmail.com>
Date:   Wed, 15 Dec 2021 02:28:08 +0200

Day 14 cleanup

Diffstat:
Mday14/puzzle.c | 87+++----------------------------------------------------------------------------
1 file changed, 3 insertions(+), 84 deletions(-)

diff --git a/day14/puzzle.c b/day14/puzzle.c @@ -29,18 +29,14 @@ struct count_t { long long parse_rule(struct array_t *rules, const char *str); void process_rules(struct array_t *rules); -void print_rules(struct array_t *rules); struct rule_t * get_rule(struct array_t *rules, const char name[3]); struct array_t make_sections_count(struct array_t *sections); void add_section_count(struct array_t *counts, struct rule_t *section, unsigned long long count); void add_symbol_count(struct array_t *counts, char symbol, unsigned long long multiplier); unsigned long long get_min_count(const struct array_t *counts); unsigned long long get_max_count(const struct array_t *counts); -struct array_t get_sections(struct array_t *orig_rules, const char *str); +struct array_t make_sections(struct array_t *orig_rules, const char *str); struct array_t iterate_sections_count(struct array_t *rules, struct array_t counts); -unsigned long long get_count(struct array_t *counts, struct rule_t *section); -void print_sections(struct array_t *sections); -void print_sections_count(struct array_t *rules, struct array_t *sections); unsigned long long get_char_counts(struct array_t counts); void puzzle(const char *filename, unsigned long long *result1, unsigned long long *result2) { @@ -75,18 +71,11 @@ void puzzle(const char *filename, unsigned long long *result1, unsigned long lon } process_rules(&rules); - //printf("input str: %s\n", str); - //print_rules(&rules); - - struct array_t sections = get_sections(&rules, str); - //print_sections(&sections); - + struct array_t sections = make_sections(&rules, str); struct array_t sections_count = make_sections_count(&sections); - //print_sections_count(&rules, &sections_count); for (int i = 0; i < 40; ++i) { struct array_t sections_count1 = iterate_sections_count(&rules, sections_count); - //print_sections_count(&rules, &sections_count1); free(sections_count.data); sections_count = sections_count1; @@ -114,10 +103,6 @@ struct array_t iterate_sections_count(struct array_t *rules, struct array_t coun for (size_t i = 0; i < counts.count; ++i) { struct rule_t *rule = counts_data[i].section; if (counts_data[i].count > 0) { - //printf("\n%s spawning %s+%s\n", rule->name, rule->children[0]->name, rule->children[1]->name); - //long long c1 = get_count(&counts, rule->children[0]); - //long long c2 = get_count(&counts, rule->children[1]); - //printf("%s count %lld x %lld and %lld\n", rule->name, counts_data[i].count, c1, c2); add_section_count(&new_counts, rule->children[0], counts_data[i].count); add_section_count(&new_counts, rule->children[1], counts_data[i].count); } @@ -126,31 +111,6 @@ struct array_t iterate_sections_count(struct array_t *rules, struct array_t coun return new_counts; } -unsigned long long get_count(struct array_t *counts, struct rule_t *section) { - struct count_t *counts_data = counts->data; - for (size_t i = 0; i < counts->count; ++i) { - if (counts_data[i].section == section) { - //printf("\nSECTION FOUND: %lld\n", counts_data[i].count); - return counts_data[i].count; - } - } - - return 0; -} - -void print_sections(struct array_t *sections) { - assert(sections != NULL); - struct rule_t **data = sections->data; - for (size_t i = 0; i < sections->count; ++i) { - if (i == 0) { - printf("%s", data[i]->name); - } else { - printf("%c", data[i]->name[1]); - } - } - printf("\n"); -} - struct array_t make_sections_count(struct array_t *sections) { struct array_t counts = { .data = NULL }; array_init(&counts, sizeof(struct count_t), 10); @@ -168,50 +128,18 @@ unsigned long long get_char_counts(struct array_t counts) { struct count_t *counts_data = counts.data; for (size_t i = 0; i < counts.count; ++i) { - //printf("%s: %lld\n", counts_data[i].section->name, counts_data[i].count); if (i == 0) { add_symbol_count(&char_counts, counts_data[i].section->name[0], 1); } add_symbol_count(&char_counts, counts_data[i].section->name[1], counts_data[i].count); } - //printf("\n"); - - struct count_t *char_counts_data = char_counts.data; - unsigned long long total_count = 0; - for (size_t i = 0; i < char_counts.count; ++i) { - //printf("%c: %lld\n", char_counts_data[i].symbol, char_counts_data[i].count); - total_count += char_counts_data[i].count; - } unsigned long long count_min = get_min_count(&char_counts); unsigned long long count_max = get_max_count(&char_counts); - //printf("\nmin: %llu, max: %llu, total: %llu\n", count_min, count_max, total_count); - return count_max - count_min; } -void print_sections_count(struct array_t *rules, struct array_t *counts) { - struct count_t *counts_data = counts->data; - struct rule_t *rules_data = rules->data; - for (size_t r = 0; r < rules->count; ++r) { - bool section_found = false; - for (size_t i = 0; i < counts->count; ++i) { - if (counts_data[i].section == &rules_data[r]) { - printf("%s: %3llu ", counts_data[i].section->name, counts_data[i].count); - //printf("%5lld", counts_data[i].count); - section_found = true; - break; - } - } - if (!section_found) { - printf("%s: %3d ", rules_data[r].name, 0); - //printf("%5d", 0); - } - } - printf("\n"); -} - void add_section_count(struct array_t *counts, struct rule_t *section, unsigned long long count) { assert(counts != NULL); assert(section != NULL); @@ -302,15 +230,6 @@ long long parse_rule(struct array_t *rules, const char *str) { return rule.score; } -void print_rules(struct array_t *rules) { - assert(rules != NULL); - struct rule_t *data = rules->data; - for (size_t i = 0; i < rules->count; ++i) { - printf("(%lld) %s -> %c", data[i].score, data[i].name, data[i].child); - printf(" (%s, %s)\n", data[i].children[0]->name, data[i].children[1]->name); - } -} - void process_rules(struct array_t *rules) { assert(rules != NULL); struct rule_t *data = rules->data; @@ -341,7 +260,7 @@ struct rule_t * get_rule(struct array_t *rules, const char name[3]) { return NULL; } -struct array_t get_sections(struct array_t *rules, const char *str) { +struct array_t make_sections(struct array_t *rules, const char *str) { assert(str != NULL); assert(rules != NULL); size_t len = strlen(str);