From 0344a9162fb73052145e8f145c5e4c4d8791b56a Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Fri, 7 Jan 2022 21:08:56 +0100 Subject: [PATCH] day 18 part 2 (before cleaning) --- 2021/day18/README.txt | 8 ++++++-- 2021/day18/aoc-c.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/2021/day18/README.txt b/2021/day18/README.txt index 9d9a95c..e5a2791 100644 --- a/2021/day18/README.txt +++ b/2021/day18/README.txt @@ -162,8 +162,6 @@ The magnitude of this final sum is 4140. Add up all of the snailfish numbers from the homework assignment in the order they appear. What is the magnitude of the final sum? Your puzzle answer was 3987. - -The first half of this puzzle is complete! It provides one gold star: * --- Part Two --- You notice a second question on the back of the homework assignment: @@ -188,3 +186,9 @@ Again considering the last example homework assignment above: The largest magnitude of the sum of any two snailfish numbers in this list is 3993. This is the magnitude of [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] + [[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]], which reduces to [[[[7,8],[6,6]],[[6,0],[7,7]]],[[[7,8],[8,8]],[[7,9],[0,6]]]]. What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment? + +Your puzzle answer was 4500. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. diff --git a/2021/day18/aoc-c.c b/2021/day18/aoc-c.c index 1483f37..6e562df 100644 --- a/2021/day18/aoc-c.c +++ b/2021/day18/aoc-c.c @@ -155,7 +155,6 @@ static int node_explode(node_t *node) } if (depth == 4) { node_t *left = node->tree[LEFT], *right = node->tree[RIGHT]; - node_t *tmp; //struct list_head *tmp; /* skip leaves */ @@ -287,8 +286,10 @@ static node_t *node_read(char **p, int depth)//, node_t *parent) node->depth = depth; - if (!depth) /* root node */ + if (!depth) { /* root node */ root = node; + //log_f(1, "%s\n", *p); + } //log_f(2, "str=%s depth=%d\n", *p, depth); switch (**p) { @@ -308,6 +309,9 @@ static node_t *node_read(char **p, int depth)//, node_t *parent) list_add_tail(&node->leaf_list, &root->leaf_list); } (*p)++; + //if (!depth) { /* root node */ + // log_f(1, "%s\n", *p); + //} return node; } @@ -363,7 +367,6 @@ static s64 node_magnitude(node_t *node) static s64 part1() { node_t *head = NULL, *next = NULL; - s64 res = 1; head = node_read(lines + 0, 0); for (int i = 1; i < nlines; ++i) { @@ -376,8 +379,26 @@ static s64 part1() static s64 part2() { - s64 res = 2; - return res; + node_t *l1 = NULL, *l2 = NULL; + s64 max = 0, cur; + char *tmp; + + for (int i = 0; i < nlines; ++i) { + for (int j = 0; j < nlines; ++j) { + if (j == i) + continue; + tmp = lines[i]; + l1 = node_read(&tmp, 0); + tmp = lines[j]; + l2 = node_read(&tmp, 0); + cur = node_magnitude(node_reduce(node_add(l1, l2))); + if (cur > max) { + log(2, "NEW BEST: %s + %s\n", lines[i], lines[j]); + max = cur; + } + } + } + return max; } static int usage(char *prg)