From 5cde9051ec5cb03950439b1b129f133c0a178306 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Tue, 10 Jan 2023 22:14:59 +0100 Subject: [PATCH] 2022 day 13 (C) final version - more cleanup needed --- 2022/RESULTS.txt | 14 ++++++ 2022/day13/aoc-c.c | 111 +++------------------------------------------ 2 files changed, 21 insertions(+), 104 deletions(-) diff --git a/2022/RESULTS.txt b/2022/RESULTS.txt index 99ea4db..50b86f0 100644 --- a/2022/RESULTS.txt +++ b/2022/RESULTS.txt @@ -273,3 +273,17 @@ aoc.bash: res=399 aoc-c: res=399 time: 0:00.00 real, 0.00 user, 0.00 sys context-switch: 0+1, page-faults: 0+171 + +========================================= +================= day13 ================= +========================================= + ++++++++++++++++++ part 1 +aoc-c: res=5843 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+192 + ++++++++++++++++++ part 2 +aoc-c: res=26289 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+190 diff --git a/2022/day13/aoc-c.c b/2022/day13/aoc-c.c index 0eb7154..3dc7c81 100644 --- a/2022/day13/aoc-c.c +++ b/2022/day13/aoc-c.c @@ -16,7 +16,6 @@ #include #include "br.h" -#include "debug.h" #include "list.h" #include "pool.h" @@ -60,35 +59,14 @@ static node_t *getnode() node_t *node = pool_get(pool_node); node->car_t = NIL; INIT_LIST_HEAD(&node->cdr); - log_f(4, "\n"); return node; } -static void print_tree(struct list_head *head, int level) -{ - struct node *cur; - log_f(4, "head=%p lev=%d\n", head, level); - printf("( "); - //if (!list_empty(&node->cdr)) { - list_for_each_entry(cur, head, cdr) { - if (cur->car_t == INT) - printf("%d ", cur->car.value); - else if (cur->car_t == NIL) - printf("ERROR "); - else - print_tree(&cur->car.sub, level + 2); - } - printf(") "); - if (!level) - printf("\n"); -} - static int compare_tree(struct list_head *h1, struct list_head *h2) { struct list_head *cur1, *cur2; node_t *n1, *n2; int res; - log_f(3, "h1=%p h2=%p\n", h1, h2); /* get lists first entries */ cur1 = h1->next; @@ -101,15 +79,10 @@ static int compare_tree(struct list_head *h1, struct list_head *h2) if (n1->car_t == n2->car_t) { if (n1->car_t == INT) { if (n1->car.value < n2->car.value) { - log(3, "car1=%d < car2=%d, returning 1\n", n1->car.value, - n2->car.value); return 1; } else if (n1->car.value > n2->car.value) { - log(3, "car1=%d > car2=%d, returning -1\n", n1->car.value, - n2->car.value); return -1; } - log(3, "car1 == car2 == %d, skipping\n", n1->car.value); } else { /* both sublists */ res = compare_tree(&n1->car.sub, &n2->car.sub); if (res) @@ -117,11 +90,9 @@ static int compare_tree(struct list_head *h1, struct list_head *h2) } } else { /* one number, one list */ if (n1->car_t == INT) { - log(3, "car1 == INT, using dummy\n"); dummy.car.value = n1->car.value; res = compare_tree(&dummy_list, &n2->car.sub); } else { - log(3, "car2 == INT, using dummy\n"); dummy.car.value = n2->car.value; res = compare_tree(&n1->car.sub, &dummy_list); } @@ -133,58 +104,32 @@ static int compare_tree(struct list_head *h1, struct list_head *h2) } /* at least one list came to end */ if (cur1 == h1 && cur2 == h2) { /* both are ending */ - log(3, "Both sides Left side ran out of items, undecided\n"); return 0; } else if (cur1 == h1) { /* first list did end */ - log(3, "Left side ran out of items, so inputs are in the right order\n"); return 1; } else { - log(3, "Right side ran out of items, so inputs are not in the right order\n"); return -1; } } -static void print_nodes() -{ - nodes_t *cur; - log_f(3, "+++++++++++\n"); - list_for_each_entry(cur, &nodes, list) { - print_tree(&cur->node, 0); - } -} - static int add_node(nodes_t *h) { - nodes_t *first, *iter, *prev; + nodes_t *first, *iter; struct list_head *node_next = &nodes; int num = 1; - log_f(3, "adding "); - print_tree(&h->node, 0); - - if (list_empty(&nodes)) { - log_f(3, "adding first entry\n"); + if (list_empty(&nodes)) goto ins_node; - } first = iter = list_first_entry(&nodes, nodes_t, list); do { if (compare_tree(&h->node, &iter->node) > 0) { node_next = &iter->list; break; } - prev = iter; iter = list_entry(iter->list.next, nodes_t, list); num++; } while (iter != first); ins_node: - log_f(3, "adding entry before "); - if (node_next == &nodes) - log(3, "head\n"); - else - print_tree(&list_entry(node_next, nodes_t, list)->node, 0); - //&n->node, 0); - // list_add_tail(&h->list, &n->list); - log_f(3, "position = %d\n", num); list_add_tail(&h->list, node_next); return num; } @@ -195,17 +140,10 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l node_t *node = NULL; LIST_HEAD(sub); int num = 0, val, depth = 0, subconsumed; - //LIST_HEAD(head); + *consumed = 1; - //if (*s != '[') { - // printf("error 0\n"); - // exit(0); - //} INIT_LIST_HEAD(head); - //head = getnode(); - log_f(3, "create_tree(%s)\n", s); for (; *s; s++, (*consumed)++) { - log(4, "L=%d examining %c num=%d depth=%d\n", level, *s, num, depth); val = 0; switch (*s) { case '[': @@ -219,10 +157,7 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l create_tree(s, &subconsumed, level + 1, &node->car.sub); s += subconsumed - 1; *consumed += subconsumed - 1; - log(4, "after create_tree: cons=%d s=%s\n", subconsumed, s); - list_add_tail(&node->cdr, head); /* add node to current level */ - //print_tree(&node->car.sub, 0); - //print_tree(head, 0); + list_add_tail(&node->cdr, head); depth--; num++; break; @@ -231,7 +166,6 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l exit(0); break; /* not reached */ } - //num++; break; case ',': break; @@ -243,7 +177,6 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l sscanf(s, "%d%n", &val, &subconsumed); *consumed += subconsumed - 1; s += subconsumed - 1; - log(4, "got integer=%d num=%d depth=%d chars=%d\n", val, num, depth, subconsumed); node = getnode(); node->car_t = INT; node->car.value = val; @@ -253,7 +186,6 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l } } end: - log(4, "returning from create_tree, consumed=%d head=%p\n", *consumed, head); return head; } @@ -263,9 +195,8 @@ static int parse() ssize_t buflen; char *buf = NULL; int i = 0; - //struct list_head h[2]; nodes_t *head[2]; - int dummy, group = 1, res = 0, tmp; + int dummy, group = 1, res = 0; while (1) { buflen = getline(&buf, &alloc, stdin); @@ -273,23 +204,11 @@ static int parse() head[ i % 2] = pool_get(pool_nodes); create_tree(buf, &dummy, 0, &head[i % 2]->node); add_node(head[i %2]); - //create_tree(buf, &dummy, 0, &head[i % 2]); - - printf("setting node %d\n\n", i % 2); } if (buflen != 0) { if (i % 2) { - printf("++++ node 0 = %p\n", &head[0]); - print_tree(&head[0]->node, 0); - printf("++++ node 1 = %p\n", &head[1]); - print_tree(&head[1]->node, 0); - tmp = compare_tree(&head[0]->node, &head[1]->node); - printf("group = %d compare_tree() = %d\n", group, tmp); - if (tmp == 1) { + if (compare_tree(&head[0]->node, &head[1]->node) == 1) res += group; - printf("group = %d: right order res:%d -> %d\n", group, res - group, res); - printf("\n\n"); - } group++; } i++; @@ -320,29 +239,13 @@ static int part2() return res; } -/* -static struct list_head dummy_head; -static node_t dummy = { - .car_t = INT, .car.value = 0, .cdr = LIST_HEAD_INIT(dummy_head) -}; -static struct list_head dummy_head = LIST_HEAD_INIT(dummy.car.sub); -*/ - int main(int ac, char **av) { int part = parseargs(ac, av); pool_node = pool_create("node", 512, sizeof(node_t)); pool_nodes = pool_create("nodes", 512, sizeof(nodes_t)); - //u32 foo=0x12345678; - printf("&dummy->cdr=%p next=%p prev=%p\n", - &dummy.cdr, dummy.cdr.next, dummy.cdr.prev); - printf("&dummy_head=%p next=%p prev=%p\n", - &dummy_list, dummy_list.next, dummy_list.prev); - //parse(); - //printf("&dummy->cdr=%p next=%p prev=%p\n", - // &dummy.cdr, dummy.cdr.next, dummy.cdr.prev); + printf("%s: res=%d\n", *av, part == 1? part1(): part2()); - print_nodes(); pool_destroy(pool_node); pool_destroy(pool_nodes); exit(0);