Prechádzať zdrojové kódy

[2024][c][Day_2] Adding solution for Part 1

Vinicius Teshima 1 rok pred
rodič
commit
2f9bfe6507
2 zmenil súbory, kde vykonal 142 pridanie a 0 odobranie
  1. 1 0
      2024/c/Day_2/Makefile
  2. 141 0
      2024/c/Day_2/main.c

+ 1 - 0
2024/c/Day_2/Makefile

@@ -0,0 +1 @@
+../Makefile

+ 141 - 0
2024/c/Day_2/main.c

@@ -0,0 +1,141 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#define IMP
+#include "../file.h"
+
+struct report {
+	size_t cap;
+	size_t size;
+	long *data;
+};
+
+
+size_t cstr_char_count(const char *cstr, char c);
+int cmp_sort(const void *this, const void *other);
+
+int
+main(int argc, char *argv[])
+{
+	enum file_err ferr;
+	size_t line_num = 0;
+	size_t i = 0;
+	long res = 0;
+
+	struct report *reports = NULL;
+	size_t reports_size = 0;
+
+	char *raw_input = NULL;
+	size_t raw_input_size = 0;
+	raw_input = (char *) file_read_all("./input.txt", 
+					   &raw_input_size,
+					   &ferr);
+	if ( ferr != FILE_ERR_OK ) {
+		fprintf(stderr, "Failed to open file: %s\n", 
+			file_err_to_cstr(ferr));
+		exit(EXIT_FAILURE);
+	}
+
+	line_num = cstr_char_count(raw_input, '\n');
+
+	reports = calloc(line_num+1, sizeof(*reports));
+
+	{
+	long ret = -1;
+	char *str = raw_input;
+	char *nstr = NULL;
+	struct report *this = &reports[reports_size++];
+	if ( this->data == NULL ) {
+		this->cap = 10;
+		this->size = 0;
+		this->data = calloc(this->cap, sizeof(*this->data));
+	}
+
+	while ( true ) {
+		ret = strtol(str, &nstr, 10);
+		if ( ret == 0 ) {
+			break;
+		}
+		str = nstr;
+
+		if ( this->size+1 >= this->cap ) {
+			this->cap <<= 1;
+			this->data = realloc(this->data, this->cap * sizeof(*this->data));
+		}
+		this->data[this->size++] = ret;
+
+		if ( *str == '\n' ) {
+			this = &reports[reports_size++];
+			if ( this->data == NULL ) {
+				this->cap = 10;
+				this->size = 0;
+				this->data = calloc(this->cap, sizeof(*this->data));
+			}
+		}
+	}
+	}
+
+	if ( argc == 1 ) {
+		for ( i = 0; i < reports_size; ++i ) {
+			size_t j = 1;
+			struct report *this = &reports[i];
+			long dir = this->data[0] - this->data[1];
+			if ( dir == 0 ) {
+				/* if dir is == 0 it is already unsafe */
+				continue;
+			}
+
+			++res;
+			for ( j = 1; j < this->size; ++j ) {
+				long diff = this->data[j-1] - this->data[j];
+				long diff_abs = labs(diff);
+
+				if ( diff_abs < 1 || diff_abs > 3 ) {
+					--res;
+					break;
+				}
+
+				if ( ( diff > 0 && dir < 0 ) || ( diff < 0 && dir > 0 ) ) {
+					--res;
+					break;
+				}
+			}
+		}
+	} else {
+	}
+
+
+	printf("Result: %ld\n", res);
+
+	for ( i = 0; i < reports_size; ++i ) {
+		free(reports[i].data);
+	}
+
+	free(reports);
+
+	free(raw_input);
+
+	(void) argc; (void) argv;
+	return 0;
+}
+
+size_t 
+cstr_char_count(const char *cstr, char c)
+{
+	size_t ret = 0;
+	while ( *cstr != '\0' ) {
+		ret += ( *cstr == c );
+		cstr++;
+	}
+	return ret;
+}
+
+int 
+cmp_sort(const void *this, const void *other)
+{
+	long this_ = *((long*) this);
+	long other_ = *((long*) other);
+
+	return (int) (this_ - other_);
+}