2022 day 13 (C) final version - more cleanup needed

This commit is contained in:
2023-01-10 22:14:59 +01:00
parent 111fde4fbd
commit 5cde9051ec
2 changed files with 21 additions and 104 deletions

View File

@@ -273,3 +273,17 @@ aoc.bash: res=399
aoc-c: res=399 aoc-c: res=399
time: 0:00.00 real, 0.00 user, 0.00 sys time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+171 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

View File

@@ -16,7 +16,6 @@
#include <ctype.h> #include <ctype.h>
#include "br.h" #include "br.h"
#include "debug.h"
#include "list.h" #include "list.h"
#include "pool.h" #include "pool.h"
@@ -60,35 +59,14 @@ static node_t *getnode()
node_t *node = pool_get(pool_node); node_t *node = pool_get(pool_node);
node->car_t = NIL; node->car_t = NIL;
INIT_LIST_HEAD(&node->cdr); INIT_LIST_HEAD(&node->cdr);
log_f(4, "\n");
return node; 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) static int compare_tree(struct list_head *h1, struct list_head *h2)
{ {
struct list_head *cur1, *cur2; struct list_head *cur1, *cur2;
node_t *n1, *n2; node_t *n1, *n2;
int res; int res;
log_f(3, "h1=%p h2=%p\n", h1, h2);
/* get lists first entries */ /* get lists first entries */
cur1 = h1->next; 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 == n2->car_t) {
if (n1->car_t == INT) { if (n1->car_t == INT) {
if (n1->car.value < n2->car.value) { if (n1->car.value < n2->car.value) {
log(3, "car1=%d < car2=%d, returning 1\n", n1->car.value,
n2->car.value);
return 1; return 1;
} else if (n1->car.value > n2->car.value) { } 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; return -1;
} }
log(3, "car1 == car2 == %d, skipping\n", n1->car.value);
} else { /* both sublists */ } else { /* both sublists */
res = compare_tree(&n1->car.sub, &n2->car.sub); res = compare_tree(&n1->car.sub, &n2->car.sub);
if (res) if (res)
@@ -117,11 +90,9 @@ static int compare_tree(struct list_head *h1, struct list_head *h2)
} }
} else { /* one number, one list */ } else { /* one number, one list */
if (n1->car_t == INT) { if (n1->car_t == INT) {
log(3, "car1 == INT, using dummy\n");
dummy.car.value = n1->car.value; dummy.car.value = n1->car.value;
res = compare_tree(&dummy_list, &n2->car.sub); res = compare_tree(&dummy_list, &n2->car.sub);
} else { } else {
log(3, "car2 == INT, using dummy\n");
dummy.car.value = n2->car.value; dummy.car.value = n2->car.value;
res = compare_tree(&n1->car.sub, &dummy_list); 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 */ /* at least one list came to end */
if (cur1 == h1 && cur2 == h2) { /* both are ending */ if (cur1 == h1 && cur2 == h2) { /* both are ending */
log(3, "Both sides Left side ran out of items, undecided\n");
return 0; return 0;
} else if (cur1 == h1) { /* first list did end */ } 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; return 1;
} else { } else {
log(3, "Right side ran out of items, so inputs are not in the right order\n");
return -1; 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) static int add_node(nodes_t *h)
{ {
nodes_t *first, *iter, *prev; nodes_t *first, *iter;
struct list_head *node_next = &nodes; struct list_head *node_next = &nodes;
int num = 1; int num = 1;
log_f(3, "adding "); if (list_empty(&nodes))
print_tree(&h->node, 0);
if (list_empty(&nodes)) {
log_f(3, "adding first entry\n");
goto ins_node; goto ins_node;
}
first = iter = list_first_entry(&nodes, nodes_t, list); first = iter = list_first_entry(&nodes, nodes_t, list);
do { do {
if (compare_tree(&h->node, &iter->node) > 0) { if (compare_tree(&h->node, &iter->node) > 0) {
node_next = &iter->list; node_next = &iter->list;
break; break;
} }
prev = iter;
iter = list_entry(iter->list.next, nodes_t, list); iter = list_entry(iter->list.next, nodes_t, list);
num++; num++;
} while (iter != first); } while (iter != first);
ins_node: 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); list_add_tail(&h->list, node_next);
return num; return num;
} }
@@ -195,17 +140,10 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l
node_t *node = NULL; node_t *node = NULL;
LIST_HEAD(sub); LIST_HEAD(sub);
int num = 0, val, depth = 0, subconsumed; int num = 0, val, depth = 0, subconsumed;
//LIST_HEAD(head);
*consumed = 1; *consumed = 1;
//if (*s != '[') {
// printf("error 0\n");
// exit(0);
//}
INIT_LIST_HEAD(head); INIT_LIST_HEAD(head);
//head = getnode();
log_f(3, "create_tree(%s)\n", s);
for (; *s; s++, (*consumed)++) { for (; *s; s++, (*consumed)++) {
log(4, "L=%d examining %c num=%d depth=%d\n", level, *s, num, depth);
val = 0; val = 0;
switch (*s) { switch (*s) {
case '[': 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); create_tree(s, &subconsumed, level + 1, &node->car.sub);
s += subconsumed - 1; s += subconsumed - 1;
*consumed += subconsumed - 1; *consumed += subconsumed - 1;
log(4, "after create_tree: cons=%d s=%s\n", subconsumed, s); list_add_tail(&node->cdr, head);
list_add_tail(&node->cdr, head); /* add node to current level */
//print_tree(&node->car.sub, 0);
//print_tree(head, 0);
depth--; depth--;
num++; num++;
break; break;
@@ -231,7 +166,6 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l
exit(0); exit(0);
break; /* not reached */ break; /* not reached */
} }
//num++;
break; break;
case ',': case ',':
break; 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); sscanf(s, "%d%n", &val, &subconsumed);
*consumed += subconsumed - 1; *consumed += subconsumed - 1;
s += subconsumed - 1; s += subconsumed - 1;
log(4, "got integer=%d num=%d depth=%d chars=%d\n", val, num, depth, subconsumed);
node = getnode(); node = getnode();
node->car_t = INT; node->car_t = INT;
node->car.value = val; node->car.value = val;
@@ -253,7 +186,6 @@ static struct list_head *create_tree(char *s, int *consumed, int level, struct l
} }
} }
end: end:
log(4, "returning from create_tree, consumed=%d head=%p\n", *consumed, head);
return head; return head;
} }
@@ -263,9 +195,8 @@ static int parse()
ssize_t buflen; ssize_t buflen;
char *buf = NULL; char *buf = NULL;
int i = 0; int i = 0;
//struct list_head h[2];
nodes_t *head[2]; nodes_t *head[2];
int dummy, group = 1, res = 0, tmp; int dummy, group = 1, res = 0;
while (1) { while (1) {
buflen = getline(&buf, &alloc, stdin); buflen = getline(&buf, &alloc, stdin);
@@ -273,23 +204,11 @@ static int parse()
head[ i % 2] = pool_get(pool_nodes); head[ i % 2] = pool_get(pool_nodes);
create_tree(buf, &dummy, 0, &head[i % 2]->node); create_tree(buf, &dummy, 0, &head[i % 2]->node);
add_node(head[i %2]); 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 (buflen != 0) {
if (i % 2) { if (i % 2) {
printf("++++ node 0 = %p\n", &head[0]); if (compare_tree(&head[0]->node, &head[1]->node) == 1)
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) {
res += group; res += group;
printf("group = %d: right order res:%d -> %d\n", group, res - group, res);
printf("\n\n");
}
group++; group++;
} }
i++; i++;
@@ -320,29 +239,13 @@ static int part2()
return res; 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 main(int ac, char **av)
{ {
int part = parseargs(ac, av); int part = parseargs(ac, av);
pool_node = pool_create("node", 512, sizeof(node_t)); pool_node = pool_create("node", 512, sizeof(node_t));
pool_nodes = pool_create("nodes", 512, sizeof(nodes_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()); printf("%s: res=%d\n", *av, part == 1? part1(): part2());
print_nodes();
pool_destroy(pool_node); pool_destroy(pool_node);
pool_destroy(pool_nodes); pool_destroy(pool_nodes);
exit(0); exit(0);