aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--day15/problem_1.c87
-rw-r--r--day15/problem_2.c87
2 files changed, 174 insertions, 0 deletions
diff --git a/day15/problem_1.c b/day15/problem_1.c
new file mode 100644
index 0000000..c38f3c4
--- /dev/null
+++ b/day15/problem_1.c
@@ -0,0 +1,87 @@
+/* advent of code day 15, problem 1
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <uthash.h>
+
+struct num {
+ int n;
+ int prev;
+ int prev_prev;
+ bool once;
+ UT_hash_handle hh;
+};
+
+
+//#define TESTING
+
+// just copy and paste the numbers in, is easier than reading from file
+#ifdef TESTING
+#define START_NUMS_LEN 3
+static int start_nums[START_NUMS_LEN] = {0, 3, 6};
+#else
+#define START_NUMS_LEN 6
+static int start_nums[START_NUMS_LEN] = {1, 0, 18, 10, 19, 6};
+#endif // TESTING
+
+
+int main (int argc, char *argv[])
+{
+ struct num *hsh = NULL, *prev, *tmp, *s;
+ int i, prev_num, cur_num;
+
+ if (argc != 1) {
+ printf("Usage: problem_[x]");
+ }
+
+ for (i = 0; i < START_NUMS_LEN; ++i) {
+ cur_num = start_nums[i];
+ s = malloc(sizeof(*s));
+ s->n = cur_num;
+ s->prev = i;
+ s->once = true;
+ HASH_ADD_INT(hsh, n, s);
+ prev_num = cur_num;
+ }
+
+ for (; i < 2020; ++i) {
+ HASH_FIND_INT(hsh, &prev_num, prev);
+ if (prev == NULL) {
+ printf("Error: number has never been spoken!\n");
+ exit(EXIT_FAILURE);
+ } else {
+ if (prev->once) {
+ cur_num = 0;
+ prev->once = false;
+ } else {
+ cur_num = prev->prev - prev->prev_prev;
+ }
+
+ HASH_FIND_INT(hsh, &cur_num, s);
+ if (s == NULL) {
+ tmp = malloc(sizeof(*tmp));
+ tmp->n = cur_num;
+ tmp->prev = i;
+ tmp->once = true;
+ HASH_ADD_INT(hsh, n, tmp);
+ } else {
+ s->prev_prev = s->prev;
+ s->prev = i;
+ s->once = false;
+ }
+ }
+ prev_num = cur_num;
+ }
+
+ printf("2020th number spoken: %d\n", cur_num);
+
+ HASH_ITER(hh, hsh, s, tmp) {
+ HASH_DEL(hsh, s);
+ free(s);
+ }
+ return EXIT_SUCCESS;
+}
diff --git a/day15/problem_2.c b/day15/problem_2.c
new file mode 100644
index 0000000..0fe711a
--- /dev/null
+++ b/day15/problem_2.c
@@ -0,0 +1,87 @@
+/* advent of code day 15, problem 2
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <uthash.h>
+
+struct num {
+ int n;
+ int prev;
+ int prev_prev;
+ bool once;
+ UT_hash_handle hh;
+};
+
+
+//#define TESTING
+
+// just copy and paste the numbers in, is easier than reading from file
+#ifdef TESTING
+#define START_NUMS_LEN 3
+static int start_nums[START_NUMS_LEN] = {0, 3, 6};
+#else
+#define START_NUMS_LEN 6
+static int start_nums[START_NUMS_LEN] = {1, 0, 18, 10, 19, 6};
+#endif // TESTING
+
+
+int main (int argc, char *argv[])
+{
+ struct num *hsh = NULL, *prev, *tmp, *s;
+ int i, prev_num, cur_num;
+
+ if (argc != 1) {
+ printf("Usage: problem_[x]");
+ }
+
+ for (i = 0; i < START_NUMS_LEN; ++i) {
+ cur_num = start_nums[i];
+ s = malloc(sizeof(*s));
+ s->n = cur_num;
+ s->prev = i;
+ s->once = true;
+ HASH_ADD_INT(hsh, n, s);
+ prev_num = cur_num;
+ }
+
+ for (; i < 30000000; ++i) {
+ HASH_FIND_INT(hsh, &prev_num, prev);
+ if (prev == NULL) {
+ printf("Error: number has never been spoken!\n");
+ exit(EXIT_FAILURE);
+ } else {
+ if (prev->once) {
+ cur_num = 0;
+ prev->once = false;
+ } else {
+ cur_num = prev->prev - prev->prev_prev;
+ }
+
+ HASH_FIND_INT(hsh, &cur_num, s);
+ if (s == NULL) {
+ tmp = malloc(sizeof(*tmp));
+ tmp->n = cur_num;
+ tmp->prev = i;
+ tmp->once = true;
+ HASH_ADD_INT(hsh, n, tmp);
+ } else {
+ s->prev_prev = s->prev;
+ s->prev = i;
+ s->once = false;
+ }
+ }
+ prev_num = cur_num;
+ }
+
+ printf("30000000th number spoken: %d\n", cur_num);
+
+ HASH_ITER(hh, hsh, s, tmp) {
+ HASH_DEL(hsh, s);
+ free(s);
+ }
+ return EXIT_SUCCESS;
+}