summaryrefslogtreecommitdiff
path: root/10
diff options
context:
space:
mode:
Diffstat (limited to '10')
-rw-r--r--10/input102
-rw-r--r--10/part1.c73
-rw-r--r--10/part2.c79
3 files changed, 254 insertions, 0 deletions
diff --git a/10/input b/10/input
new file mode 100644
index 0000000..5746f73
--- /dev/null
+++ b/10/input
@@ -0,0 +1,102 @@
+103
+131
+121
+151
+118
+12
+7
+2
+90
+74
+160
+58
+15
+83
+153
+140
+166
+1
+148
+33
+165
+39
+100
+135
+68
+77
+25
+9
+54
+94
+101
+55
+141
+22
+97
+35
+57
+117
+102
+64
+109
+114
+56
+51
+125
+82
+154
+142
+155
+45
+75
+158
+120
+5
+19
+61
+34
+128
+106
+88
+84
+137
+96
+136
+27
+6
+21
+89
+69
+162
+112
+127
+119
+161
+38
+42
+134
+20
+81
+48
+73
+87
+26
+95
+146
+113
+76
+32
+70
+8
+18
+67
+124
+80
+93
+29
+126
+147
+28
+152
+145
+159
diff --git a/10/part1.c b/10/part1.c
new file mode 100644
index 0000000..9b44cf5
--- /dev/null
+++ b/10/part1.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <malloc.h>
+#include <string.h>
+
+#define MAX_INPUT_LEN 128
+
+void insert_value_sorted(int *list, size_t *size, int value)
+{
+ long long low = 0, high = *size;
+ while (low < high) {
+ int m = low + (high - low) / 2;
+ if (list[m] == value) {
+ /* Name already exists,
+ return pointer to the already existsing string and free the new one.
+ */
+ return;
+ }
+ else if (list[m] < value) {
+ low = m + 1;
+ }
+ else {
+ high = m;
+ }
+ }
+
+ for (long long i = *size - 1; i >= low; i--) {
+ list[i + 1] = list[i];
+ }
+
+ (*size)++;
+ list[low] = value;
+}
+
+int bags_count(const char *filename)
+{
+ FILE *file = fopen(filename, "r");
+
+ // Include space for newline and string terminator
+ char buffer[128] = { 0 };
+
+ int input[MAX_INPUT_LEN] = { 0 };
+ size_t input_size = 1; // 0 is our start value
+
+ while (fgets(buffer, 128, file)) {
+ puts(buffer);
+ insert_value_sorted(input, &input_size, atoi(buffer));
+ }
+
+ fclose(file);
+
+ int diff1 = 0;
+ int diff3 = 1; // Diff with adapter
+
+ for (size_t i = 1; i < input_size; i++)
+ {
+ int diff = input[i] - input[i - 1];
+ if (diff == 1) {
+ diff1++;
+ }
+ else if (diff == 3) {
+ diff3++;
+ }
+ }
+
+ return diff1 * diff3;
+}
+
+int main(int argc, char *argv[])
+{
+ printf("%i\n", bags_count(argv[argc - 1]));
+}
diff --git a/10/part2.c b/10/part2.c
new file mode 100644
index 0000000..5053ae7
--- /dev/null
+++ b/10/part2.c
@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <malloc.h>
+#include <string.h>
+
+#define MAX_INPUT_LEN 128
+
+void insert_value_sorted(int *list, size_t *size, int value)
+{
+ long long low = 0, high = *size;
+ while (low < high) {
+ int m = low + (high - low) / 2;
+ if (list[m] == value) {
+ /* Name already exists,
+ return pointer to the already existsing string and free the new one.
+ */
+ return;
+ }
+ else if (list[m] < value) {
+ low = m + 1;
+ }
+ else {
+ high = m;
+ }
+ }
+
+ for (long long i = *size - 1; i >= low; i--) {
+ list[i + 1] = list[i];
+ }
+
+ (*size)++;
+ list[low] = value;
+}
+
+size_t bags_count(const char *filename)
+{
+ FILE *file = fopen(filename, "r");
+
+ // Include space for newline and string terminator
+ char buffer[16] = { 0 };
+
+ int input[MAX_INPUT_LEN] = { 0 };
+ size_t input_size = 1; // 0 is our start value
+
+ while (fgets(buffer, 16, file)) {
+ insert_value_sorted(input, &input_size, atoi(buffer));
+ }
+
+ // Add our device's built-in joltage adapter
+ insert_value_sorted(input, &input_size, input[input_size - 1] + 3);
+
+ fclose(file);
+
+ /* Removed the 2 first values so we don't need to add 2 to our index every time
+ Longest consecutive input is 5 */
+ const static int TRIB[] = { 1, 1, 2, 4, 7 };
+ int con = 0;
+ size_t res = 1;
+
+ for (size_t i = 1; i < input_size; i++)
+ {
+ int diff = input[i] - input[i - 1];
+ if (diff == 1) {
+ con++;
+ }
+ else {
+ res *= TRIB[con];
+ con = 0;
+ }
+ }
+
+ return res;
+}
+
+int main(int argc, char *argv[])
+{
+ printf("%zu\n", bags_count(argv[argc - 1]));
+}