From 0d7009b01ebc6603e8ba071c97f6115adaced525 Mon Sep 17 00:00:00 2001 From: Xamora Date: Sat, 7 Dec 2024 08:07:06 +0100 Subject: [PATCH] polished 07 --- 07/part1_polished.c | 50 ++++++++++++++++++++++++++++++++++ 07/part2_polished.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 07/part1_polished.c create mode 100644 07/part2_polished.c diff --git a/07/part1_polished.c b/07/part1_polished.c new file mode 100644 index 0000000..e09ec07 --- /dev/null +++ b/07/part1_polished.c @@ -0,0 +1,50 @@ +#include "_.h" + +typedef struct node { + long result; + struct node *next_mul; + struct node *next_add; +} node_t; + +int cal(char **nbrs, node_t *cur, long i, long result) { + if (nbrs[i] == NULL) + return cur->result == result; + + long nbr1 = atol(nbrs[i]); + + cur->next_mul = calloc(1, sizeof(node_t)); + cur->next_add = calloc(1, sizeof(node_t)); + cur->next_mul->result = cur->result * nbr1; + cur->next_add->result = cur->result + nbr1; + + return cal(nbrs, cur->next_mul, i + 1, result) || + cal(nbrs, cur->next_add, i + 1, result); +} + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + + long add = 0; + for (long i = 0; inputs[i] != NULL; i++) { + char **split_space = str_split(strdup(inputs[i]), ' '); + long result = atol(split_space[0]); + node_t *head = calloc(1, sizeof(node_t)); + head->result = atol(split_space[1]); + int find = cal(split_space, head, 2, result); + + //printf("result: %ld\n", result); + //printf("find: %ld\n", find); + if (find) + add += result; + } + + printf("add: %ld\n", add); + + return 0; +} diff --git a/07/part2_polished.c b/07/part2_polished.c new file mode 100644 index 0000000..a94f424 --- /dev/null +++ b/07/part2_polished.c @@ -0,0 +1,65 @@ + +#include "_.h" + +typedef struct node { + char *result; + struct node *next_mul; + struct node *next_add; + struct node *next_con; +} node_t; + +int cal(char **nbrs, node_t *cur, long long i, char* target) { + if (nbrs[i] == NULL) { + //printf("cal: %s\n", cur->result); + return strcmp(cur->result, target) == 0; + } + + long long nbr1 = atoll(nbrs[i]); + long long result = atoll(cur->result); + + cur->next_mul = calloc(1, sizeof(node_t)); + cur->next_mul->result = calloc(100, 1); + cur->next_add = calloc(1, sizeof(node_t)); + cur->next_add->result = calloc(100, 1); + cur->next_con = calloc(1, sizeof(node_t)); + cur->next_con->result = calloc(100, 1); + sprintf(cur->next_mul->result, "%lld", result * nbr1); + sprintf(cur->next_add->result, "%lld", result + nbr1); + cur->next_con->result = strcat(cur->result, nbrs[i]); + + if (cal(nbrs, cur->next_mul, i + 1, target)) + return 1; + if (cal(nbrs, cur->next_add, i + 1, target)) + return 1; + if (cal(nbrs, cur->next_con, i + 1, target)) + return 1; + return 0; +} + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + + long long add = 0; + for (long long i = 0; inputs[i] != NULL; i++) { + char **split_space = str_split(strdup(inputs[i]), ' '); + long long result = atoll(split_space[0]); + node_t *head = calloc(1, sizeof(node_t)); + head->result = split_space[1]; + int find = cal(split_space, head, 2, split_space[0]); + + //printf("result: %lld\n", result); + //printf("find: %d\n", find); + if (find) + add += result; + } + + printf("add: %lld\n", add); + + return 0; +}