aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2020-12-11 16:04:50 -0700
committernytpu <alex@nytpu.com>2020-12-11 16:06:06 -0700
commitd16080ce6e5aafe5839056465f71c2b2981ffa82 (patch)
tree381f08d160673561e88c4d3dbdc343525f71afc8
parentadd leading zeros (diff)
downloadadvent_of_code_2020-d16080ce6e5aafe5839056465f71c2b2981ffa82.tar.bz2
advent_of_code_2020-d16080ce6e5aafe5839056465f71c2b2981ffa82.zip
added day 10
-rw-r--r--day10/input.txt107
-rw-r--r--day10/problem_1.c72
-rw-r--r--day10/problem_2.c87
-rw-r--r--day10/test_input.txt11
4 files changed, 277 insertions, 0 deletions
diff --git a/day10/input.txt b/day10/input.txt
new file mode 100644
index 0000000..424bdcb
--- /dev/null
+++ b/day10/input.txt
@@ -0,0 +1,107 @@
+105
+124
+42
+52
+71
+41
+1
+85
+148
+90
+155
+112
+35
+134
+145
+39
+161
+160
+34
+54
+15
+165
+8
+20
+46
+49
+108
+151
+60
+7
+48
+154
+63
+147
+132
+98
+158
+33
+137
+45
+140
+121
+22
+62
+111
+141
+167
+131
+74
+93
+2
+142
+113
+21
+162
+61
+3
+19
+101
+9
+102
+115
+70
+12
+84
+6
+114
+107
+97
+133
+64
+80
+78
+91
+79
+14
+168
+87
+159
+30
+94
+77
+40
+125
+47
+27
+38
+166
+86
+26
+23
+67
+127
+28
+16
+169
+13
+92
+106
+57
+118
+126
+83
+146
+29
+130
+53
diff --git a/day10/problem_1.c b/day10/problem_1.c
new file mode 100644
index 0000000..40f00bc
--- /dev/null
+++ b/day10/problem_1.c
@@ -0,0 +1,72 @@
+/* advent of code day 10, problem 1
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+
+#define NUMBUF_LEN 108
+
+int read_numbers (int *buf, size_t buf_len, FILE *f);
+int compar (const void *a, const void *b);
+int find_differences (int *chk, size_t chk_len);
+
+int main (int argc, char *argv[])
+{
+ int buf[NUMBUF_LEN];
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ FILE *infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ printf("Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ const int end = read_numbers(buf, NUMBUF_LEN, infile);
+
+ printf("1s * 3s: %d\n", find_differences(buf, end));
+
+ if (fclose(infile) != 0) {
+ fprintf(stderr, "Could not close file %s", argv[1]);
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+}
+
+int read_numbers (int *buf, size_t buf_len, FILE *f)
+{
+ int i;
+ for (i=0; i<buf_len; ++i) {
+ fscanf(f, "%d\n", &buf[i]);
+ }
+ return i; // return end of read numbers
+}
+
+int compar (const void *a, const void *b)
+{
+ const int a_val = *(int *) a, b_val = *(int *) b;
+ return (a_val > b_val) - (a_val < b_val);
+}
+
+int find_differences (int *chk, size_t chk_len)
+{
+ int ones = 0, threes = 1;
+ int i;
+ qsort(&chk[0], chk_len, sizeof(int), compar);
+
+ for (i=1; i < chk_len; ++i) {
+ if (chk[i] - chk[i-1] == 3) {
+ ++threes;
+ } else {
+ ++ones;
+ }
+ }
+
+ return ones * threes;
+}
diff --git a/day10/problem_2.c b/day10/problem_2.c
new file mode 100644
index 0000000..526a701
--- /dev/null
+++ b/day10/problem_2.c
@@ -0,0 +1,87 @@
+/* advent of code day 10, problem 2
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+
+#define NUMBUF_LEN 111
+
+int read_numbers (int *buf, size_t buf_len, FILE *f);
+int compar (const void *a, const void *b);
+unsigned long calculate_permutations (const int *chk, int chk_len);
+
+int main (int argc, char *argv[])
+{
+ // so there isn't garbage if the entire buffer isn't filled
+ int *buf = (int *)calloc(NUMBUF_LEN, sizeof(int));
+ int end;
+ FILE *infile;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ printf("Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ end = read_numbers(buf, NUMBUF_LEN, infile);
+ qsort(&buf[0], end, sizeof(int), compar);
+ buf[end] = buf[end-1] + 3;
+ ++end;
+
+ printf("# of permutations: %lu\n", calculate_permutations(buf, end));
+
+ if (fclose(infile) != 0) {
+ fprintf(stderr, "Could not close file %s", argv[1]);
+ return EXIT_FAILURE;
+ }
+ free(buf);
+ return EXIT_SUCCESS;
+}
+
+int read_numbers (int *buf, size_t buf_len, FILE *f)
+{
+ int i;
+ buf[0] = 0; // initial plug
+ for (i=1; i<buf_len-1; ++i) {
+ if(fscanf(f, "%d\n", &buf[i]) < 0) {
+ break;
+ }
+ }
+ return i; // return end of read numbers
+}
+
+int compar (const void *a, const void *b)
+{
+ const int a_val = *(int *) a, b_val = *(int *) b;
+ return (a_val > b_val) - (a_val < b_val);
+}
+
+unsigned long calculate_permutations (const int *chk, int chk_len)
+{
+ int i;
+ unsigned long *permuts_up_to =
+ (unsigned long *)calloc(chk_len, sizeof(unsigned long));
+
+ permuts_up_to[0] = 1;
+ for (i=1; i < chk_len; ++i) {
+ permuts_up_to[i] = permuts_up_to[i-1];
+ if (i >= 2 && chk[i] - chk[i-2] <= 3) {
+ permuts_up_to[i] += permuts_up_to[i-2];
+ }
+ if (i >= 3 && chk[i] - chk[i-3] <= 3) {
+ permuts_up_to[i] += permuts_up_to[i-3];
+ }
+ }
+
+ unsigned long final = permuts_up_to[chk_len-1];
+ free(permuts_up_to);
+ return final;
+}
diff --git a/day10/test_input.txt b/day10/test_input.txt
new file mode 100644
index 0000000..ec4a03f
--- /dev/null
+++ b/day10/test_input.txt
@@ -0,0 +1,11 @@
+16
+10
+15
+5
+1
+11
+7
+19
+6
+12
+4