aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-02-07 20:33:23 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-02-07 20:33:23 +0100
commite270077b999c4129874da382d77addc76460aa17 (patch)
tree0d673e0d3d161d95c2e6c54ef223554258311f1c
parentb7cd81ad7d65fa804a146fbedb56dbf76b8d3a95 (diff)
downloadperlweeklychallenge-club-e270077b999c4129874da382d77addc76460aa17.tar.gz
perlweeklychallenge-club-e270077b999c4129874da382d77addc76460aa17.tar.bz2
perlweeklychallenge-club-e270077b999c4129874da382d77addc76460aa17.zip
Week 151: C solutions
-rw-r--r--challenge-151/abigail/c/ch-1.c96
-rw-r--r--challenge-151/abigail/c/ch-2.c62
2 files changed, 158 insertions, 0 deletions
diff --git a/challenge-151/abigail/c/ch-1.c b/challenge-151/abigail/c/ch-1.c
new file mode 100644
index 0000000000..e4487ddaec
--- /dev/null
+++ b/challenge-151/abigail/c/ch-1.c
@@ -0,0 +1,96 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <stdbool.h>
+# include <ctype.h>
+
+/*
+ * See https://theweeklychallenge.org/blog/perl-weekly-challenge-151
+ */
+
+/*
+ * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file
+ */
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ char * line_ptr = line;
+ /*
+ * Count the number of '|' characters; this determines
+ * how many elements to allocate for our tree.
+ */
+ int size = 3;
+ while (* line_ptr) {
+ if (* line_ptr ++ == '|') {
+ size = 2 * size + 1;
+ }
+ }
+ bool * tree;
+ if ((tree = (bool *) malloc (size * sizeof (bool))) == NULL) {
+ perror ("Malloc tree failed");
+ exit (1);
+ }
+ for (size_t i = 0; i < size; i ++) {
+ tree [i] = false;
+ }
+
+ int D = 0;
+ int i = 0;
+ size_t offset = 0;
+ line_ptr = line;
+ /*
+ * Skip leading spaces
+ */
+ while (* line_ptr && isspace (* line_ptr)) {
+ line_ptr ++;
+ }
+ while (* line_ptr) {
+ if (* line_ptr == '|') {
+ D ++;
+ i = 0;
+ offset = 2 * offset + 1;
+ line_ptr ++;
+ continue;
+ }
+ if (* line_ptr == '*') {
+ i ++;
+ line_ptr ++;
+ continue;
+ }
+ if (isspace (* line_ptr)) {
+ while (* line_ptr && isspace (* line_ptr)) {
+ line_ptr ++;
+ }
+ continue;
+ }
+ tree [offset + i] = true;
+ i ++;
+ while (* line_ptr && !isspace (* line_ptr) && * line_ptr != '|') {
+ line_ptr ++;
+ }
+ }
+
+ int width = 1;
+ int k = 0;
+ bool done = false;
+ for (int d = 0; d <= D && !done; d ++) {
+ for (int i = 0; i < width && !done; i ++) {
+ if (tree [k] && !tree [2 * k + 1] && !tree [2 * k + 2]) {
+ printf ("%d\n", d + 1);
+ done = true;
+ }
+ k ++;
+ }
+ width *= 2;
+ }
+
+ free (tree);
+ }
+ free (line);
+
+ return (0);
+}
diff --git a/challenge-151/abigail/c/ch-2.c b/challenge-151/abigail/c/ch-2.c
new file mode 100644
index 0000000000..c2a62f6c92
--- /dev/null
+++ b/challenge-151/abigail/c/ch-2.c
@@ -0,0 +1,62 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+
+/*
+ * See https://theweeklychallenge.org/blog/perl-weekly-challenge-151
+ */
+
+/*
+ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
+ */
+
+int max (int a, int b) {
+ return a < b ? b : a;
+}
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ char * line_ptr = line;
+ int * houses = NULL;
+ int * best = NULL;
+ int val;
+ int offset;
+ int nr_of_houses = 0;
+
+ while (sscanf (line_ptr, "%d%n", &val, &offset) == 1) {
+ if ((houses = (int *)
+ realloc (houses, ++ nr_of_houses * sizeof (int))) == NULL) {
+ perror ("Recalloc failed");
+ exit (1);
+ }
+ houses [nr_of_houses - 1] = val;
+ line_ptr += offset;
+ }
+
+ if ((best = (int *) malloc (nr_of_houses * sizeof (int))) == NULL) {
+ perror ("Malloc failed");
+ exit (1);
+ }
+
+ for (int i = nr_of_houses - 1; i >= 0; i --) {
+ best [i] = nr_of_houses < 2 ? houses [i]
+ : i == nr_of_houses - 1 ? houses [i]
+ : i == 0 ? houses [i] + best [i + 2]
+ : i == nr_of_houses - 2 ? max (houses [i], best [i + 1])
+ : max (houses [i] + best [i + 2],
+ best [i + 1]);
+ }
+
+ printf ("%d\n", best [0]);
+
+ free (houses);
+ free (best);
+ }
+ free (line);
+
+ return (0);
+}