Forráskód Böngészése

Changing project structure

Vinicius Teshima 1 éve
szülő
commit
838fc6bf2d

+ 0 - 1
2015/c/Day_1/Makefile

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

+ 0 - 1
2015/c/Day_2/Makefile

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

+ 0 - 1
2024/c/Day_1/Makefile

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

+ 0 - 1
2024/c/Day_2/Makefile

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

+ 0 - 14
2024/c/Makefile

@@ -1,14 +0,0 @@
-
-SRC=$(wildcard *.c) $(wildcard *.h)
-BIN=$(shell basename $(shell pwd))
-
-CC=gcc
-CFLAGS=-ansi -m64 -Og -fsanitize=address -ggdb \
-       -pedantic -Wall -Wextra -Wshadow \
-       -Wcast-align -Wunused -Wconversion -Wmisleading-indentation \
-       -Wdouble-promotion -Wformat=2 -Wbad-function-cast \
-       -Wmissing-declarations \
-       -Wmissing-prototypes -Wnested-externs -Werror
-
-${BIN}: ${SRC}
-	${CC} ${CFLAGS} -o $@ $^ 

+ 0 - 167
2024/c/file.h

@@ -1,167 +0,0 @@
-#ifndef FILE_H
-#define FILE_H
-
-#include <stddef.h>
-#include <stdint.h>
-
-#define CONCAT(a, b) CONCAT_INNER(a, b)
-#define CONCAT_INNER(a, b) a ## b
-
-#define UNIQUE_NAME(base) CONCAT(base, __COUNTER__)
-
-enum file_err {
-	FILE_ERR_OK,
-	FILE_ERR_FAIL_OPEN,
-	FILE_ERR_FAIL_SEEK,
-	FILE_ERR_FAIL_READ,
-	FILE_ERR_FAIL_WRITE,
-	FILE_ERR_FAIL_CALLOC,
-	FILE_ERR_FAIL_CLOSE,
-	FILE_ERR_FILE_EMPTY,
-	FILE_ERR_FAIL_WROTE_MORE,
-	FILE_ERR_EMPTY
-};
-
-uint8_t *file_read_all(const char *filepath, size_t *ret_size,
-		       enum file_err *ret_err);
-void file_save(const char *filepath, const char *str, size_t str_size,
-	       enum file_err *ret_err);
-
-void file_err_set(enum file_err *err, enum file_err err_);
-const char *file_err_to_cstr(enum file_err err);
-
-#if defined(BMP_IMP) || defined(IMP)
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-
-uint8_t *
-file_read_all(const char *filepath, size_t *ret_size, enum file_err *ret_err)
-{
-	int32_t fd = -1;
-	off_t file_size = -1;
-	size_t buf_size = 0;
-	uint8_t *buf = NULL;
-
-	fd = open(filepath, O_RDONLY);
-	if ( fd < 0 ) {
-		file_err_set(ret_err, FILE_ERR_FAIL_OPEN);
-		return NULL;
-	}
-
-	file_size = lseek(fd, 0, SEEK_END);
-	if ( file_size < 0 ) {
-		file_err_set(ret_err, FILE_ERR_FAIL_SEEK);
-		close(fd);
-		return NULL;
-	}
-	lseek(fd, 0, SEEK_SET);
-
-	if ( file_size == 0 ) {
-		file_err_set(ret_err, FILE_ERR_EMPTY);
-		close(fd);
-		return NULL;
-	}
-
-	buf_size = ((size_t)file_size) + 1;
-	buf = calloc(buf_size, sizeof(uint8_t));
-	if ( buf == NULL ) {
-		file_err_set(ret_err, FILE_ERR_FAIL_CALLOC);
-		close(fd);
-		return NULL;
-	}
-
-	{
-		ssize_t rd = read(fd, buf, (size_t)file_size);
-		if ( rd < 0 ) {
-			file_err_set(ret_err, FILE_ERR_FAIL_READ);
-			close(fd);
-			free(buf);
-			return NULL;
-		}
-		if ( rd == 0 ) {
-			file_err_set(ret_err, FILE_ERR_FILE_EMPTY);
-			close(fd);
-			free(buf);
-			return NULL;
-		}
-	}
-
-	if ( close(fd) != 0 ) {
-		/* It should be possible to handle EIO */
-		file_err_set(ret_err, FILE_ERR_FAIL_CLOSE);
-		close(fd);
-		return NULL;
-	}
-
-	if ( ret_size != NULL ) {
-		*ret_size = buf_size;
-	}
-	file_err_set(ret_err, FILE_ERR_OK);
-	return buf;
-}
-
-void
-file_save(const char *filepath, const char *str, size_t str_size,
-	  enum file_err *ret_err)
-{
-	ssize_t wrote = -1;
-	int32_t fd = open(filepath,
-			  O_WRONLY | O_CREAT | O_TRUNC,
-			  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-	if ( fd < 0 ) {
-		file_err_set(ret_err, FILE_ERR_FAIL_OPEN);
-		return;
-	}
-
-	wrote = write(fd, str, str_size);
-	if ( wrote == -1 ) {
-		file_err_set(ret_err, FILE_ERR_FAIL_WRITE);
-		return;
-	}
-
-	if ( ((size_t) wrote) != str_size ) {
-		file_err_set(ret_err, FILE_ERR_FAIL_WROTE_MORE);
-		return;
-	}
-
-	if ( close(fd) != 0 ) {
-		/* It should be possible to handle EIO */
-		file_err_set(ret_err, FILE_ERR_FAIL_CLOSE);
-		close(fd);
-		return;
-	}
-
-	file_err_set(ret_err, FILE_ERR_OK);
-}
-
-void
-file_err_set(enum file_err *err, enum file_err err_)
-{
-	if ( err != NULL ) {
-		*err = err_;
-	}
-}
-
-const char *
-file_err_to_cstr(enum file_err err)
-{
-	switch ( err ) {
-	case FILE_ERR_OK: 		return "FILE_ERR_OK"; break;
-	case FILE_ERR_FAIL_OPEN:	return "FILE_ERR_FAIL_OPEN"; break;
-	case FILE_ERR_FAIL_SEEK:	return "FILE_ERR_FAIL_SEEK"; break;
-	case FILE_ERR_FAIL_READ:	return "FILE_ERR_FAIL_READ"; break;
-	case FILE_ERR_FAIL_WRITE:	return "FILE_ERR_FAIL_WRITE"; break;
-	case FILE_ERR_FAIL_CALLOC:	return "FILE_ERR_FAIL_CALLOC"; break;
-	case FILE_ERR_FAIL_CLOSE:	return "FILE_ERR_FAIL_CLOSE"; break;
-	case FILE_ERR_FILE_EMPTY:	return "FILE_ERR_FILE_EMPTY"; break;
-	case FILE_ERR_FAIL_WROTE_MORE:	return "FILE_ERR_FAIL_WROTE_MORE"; break;
-	case FILE_ERR_EMPTY:		return "FILE_ERR_EMPTY"; break;
-	}
-	return "FILE ERR INVALID";
-}
-
-#endif
-#endif

+ 1 - 0
C/2015/Day_1/Makefile

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

+ 1 - 1
2015/c/Day_1/main.c → C/2015/Day_1/main.c

@@ -3,7 +3,7 @@
 #include <stdbool.h>
 
 #define IMP
-#include "../file.h"
+#include "../../file.h"
 
 size_t cstr_char_count(const char *cstr, char c);
 int cmp_sort(const void *this, const void *other);

+ 1 - 0
C/2015/Day_2/Makefile

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

+ 2 - 2
2015/c/Day_2/main.c → C/2015/Day_2/main.c

@@ -3,8 +3,8 @@
 #include <stdbool.h>
 
 #define IMP
-#include "../file.h"
-#include "../str.h"
+#include "../../file.h"
+#include "../../str.h"
 
 struct rect {
 	long l;

+ 1 - 0
C/2024/Day_1/Makefile

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

+ 1 - 1
2024/c/Day_1/main.c → C/2024/Day_1/main.c

@@ -3,7 +3,7 @@
 #include <stdbool.h>
 
 #define IMP
-#include "../file.h"
+#include "../../file.h"
 
 size_t cstr_char_count(const char *cstr, char c);
 int cmp_sort(const void *this, const void *other);

+ 1 - 0
C/2024/Day_2/Makefile

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

+ 2 - 2
2024/c/Day_2/main.c → C/2024/Day_2/main.c

@@ -3,7 +3,7 @@
 #include <stdbool.h>
 
 #define IMP
-#include "../file.h"
+#include "../../file.h"
 
 struct report {
 	size_t cap;
@@ -82,7 +82,7 @@ main(int argc, char *argv[])
 			struct report *this = &reports[i];
 			long dir = this->data[0] - this->data[1];
 			if ( dir == 0 ) {
-				/* if dir is == 0 it is already unsafe */
+		../../* if dir is == 0 it is already unsafe */
 				continue;
 			}
 

+ 1 - 0
C/2024/Day_3/Makefile

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

+ 141 - 0
C/2024/Day_3/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_);
+}

+ 0 - 0
2015/c/Makefile → C/Makefile


+ 0 - 0
2015/c/file.h → C/file.h


+ 0 - 0
2015/c/str.h → C/str.h