From 8a25923ba64dbcea6012e3c4a6802abf18160243 Mon Sep 17 00:00:00 2001 From: Xamora Date: Wed, 11 Dec 2024 06:43:18 +0100 Subject: [PATCH] finish 11, but not give part 2, need more power --- 11/Makefile | 41 ++++++++++++++++++++++++ 11/_.h | 27 ++++++++++++++++ 11/main.txt | 1 + 11/mylib.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/part1.c | 61 +++++++++++++++++++++++++++++++++++ 11/part2.c | 75 +++++++++++++++++++++++++++++++++++++++++++ 11/test.txt | 1 + 7 files changed, 297 insertions(+) create mode 100755 11/Makefile create mode 100644 11/_.h create mode 100644 11/main.txt create mode 100644 11/mylib.c create mode 100644 11/part1.c create mode 100644 11/part2.c create mode 100644 11/test.txt diff --git a/11/Makefile b/11/Makefile new file mode 100755 index 0000000..1ad3e1c --- /dev/null +++ b/11/Makefile @@ -0,0 +1,41 @@ +CC := gcc +LD := $(CC) +LIBS := -lm +LDFLAGS := +CFLAGS := -Wall -Wextra -Wno-override-init -std=c2x -include_.h +SRC := part1.c mylib.c +ifeq ($(PART),2) + SRC := part2.c mylib.c +endif +OBJ_DIR := build +OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(SRC))) +DIR := . +RAW_NAME:= a.out +NAME := $(RAW_NAME) + +ifeq ($(RELEASE),1) + CFLAGS += -O3 + LDFLAGS += -O3 -s +else + CFLAGS += -O0 -g3 + LDFLAGS += -O0 -g +endif + +all: $(NAME) + +$(NAME): $(OBJ) + $(LD) $(LDFLAGS) -o $(DIR)/$(NAME) $(OBJ) $(LIBS) + +$(OBJ_DIR)/%.o: %.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + @rm -rf $(OBJ_DIR) + @rm -rf $(RAW_NAME) + +re: + @make --no-print clean + @make --no-print all + +.PHONY: all clean re diff --git a/11/_.h b/11/_.h new file mode 100644 index 0000000..c0a4ec4 --- /dev/null +++ b/11/_.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define ABS(X) ((X) >= 0 ? (X) : -(X)) + +char** str_split(char* a_str, const char a_delim); + +int str_index(char *str, char c); + +int number_of_element(char *str, char c); + +long gmc(long a, long b); +long lmc(long a, long b); +long lmcs(int array[], int n); + +long strslen(char **strs); diff --git a/11/main.txt b/11/main.txt new file mode 100644 index 0000000..e029d56 --- /dev/null +++ b/11/main.txt @@ -0,0 +1 @@ +5688 62084 2 3248809 179 79 0 172169 diff --git a/11/mylib.c b/11/mylib.c new file mode 100644 index 0000000..87fbf66 --- /dev/null +++ b/11/mylib.c @@ -0,0 +1,91 @@ +#include "_.h" + +// NOT MY CODE +// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c +char** str_split(char* str, const char a_delim) +{ + char *a_str = strdup(str); + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + count += last_comma < (a_str + strlen(a_str) - 1); + + count++; + + result = malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + free(a_str); + return result; +} + +int str_index(char *str, char c) { + + int i = 0; + while (str[i]) + if (str[i++] == c) + return i; + + return -1; +} + +int number_of_element(char *str, char c) { + int nbr = 0; + for (int i = 0; str[i] != '\0'; i++) + if (str[i] == c) + nbr++; + return nbr; +} + +long gmc(long a, long b) { + if (b == 0) + return a; + return gmc(b, a % b); +} + +long lmc(long a, long b) { + return (a / gmc(a, b)) * b; +} + +long lmcs(int array[], int n) { + long result = array[0]; + for (int i = 1; i < n; i++) + result = lmc(result, array[i]); + return result; +} + +long strslen(char **strs) { + long count = 0; + while (strs[count]) + count++; + return count; +} diff --git a/11/part1.c b/11/part1.c new file mode 100644 index 0000000..2ed0575 --- /dev/null +++ b/11/part1.c @@ -0,0 +1,61 @@ +#include "_.h" + +typedef struct structure { + long nbr; + struct structure *next; +} struc; + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), ' '); + struc *head = calloc(1, sizeof(struc)); + struc *cur = head; + + for (int i = 0; inputs[i] != NULL; i++) { + cur->nbr = atol(inputs[i]); + cur->next = calloc(1, sizeof(struc)); + cur = cur->next; + } + + for (int i = 0; i < 25; i++) { + cur = head; + for (int j = 0; cur->next != NULL; j++) { + if (cur->nbr == 0) { + cur->nbr = 1; + cur = cur->next; + } + else if (((long)floor(log10(ABS(cur->nbr))) + 1) % 2 == 0) { + double len = floor(log10(ABS(cur->nbr))) + 1; + long left = cur->nbr / (pow(10, len / 2)); + long right = cur->nbr % (long)(pow(10, len / 2)); + + struc *new = calloc(1, sizeof(struc)); + new->nbr = right; + new->next = cur->next; + + cur->nbr = left; + cur->next = new; + cur = new->next; + } + else { + cur->nbr *= 2024; + cur = cur->next; + } + } + } + + long count = 0; + cur = head; + while (cur->next != NULL) { + cur = cur->next; + count++; + } + printf("count: %ld\n", count); + + + return 0; +} diff --git a/11/part2.c b/11/part2.c new file mode 100644 index 0000000..6f32199 --- /dev/null +++ b/11/part2.c @@ -0,0 +1,75 @@ +#include "_.h" + +typedef struct structure { + int nbr; + struct structure *next; +} struc; + +long long lenght_struc(struc *head) { + long long count = 0; + while (head->next != NULL) { + head = head->next; + count++; + } + return count; +} + +void print_struc(struc *head) { + while (head->next != NULL) { + head = head->next; + printf("%d ", head->nbr); + } +} + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), ' '); + struc *head = calloc(1, sizeof(struc)); + struc *cur = head; + + for (int i = 0; inputs[i] != NULL; i++) { + cur->nbr = atol(inputs[i]); + cur->next = calloc(1, sizeof(struc)); + cur = cur->next; + } + + long long count = lenght_struc(head); + for (int i = 0; i < 40; i++) { + cur = head; + for (int j = 0; cur->next != NULL; j++) { + if (cur->nbr == 0) { + cur->nbr = 1; + cur = cur->next; + } + else if (((int)floor(log10(ABS(cur->nbr))) + 1) % 2 == 0) { + double len = floor(log10(ABS(cur->nbr))) + 1; + int left = cur->nbr / (pow(10, len / 2)); + int right = cur->nbr % (int)(pow(10, len / 2)); + + struc *new = calloc(1, sizeof(struc)); + new->nbr = right; + new->next = cur->next; + + cur->nbr = left; + cur->next = new; + cur = new->next; + + count++; + } + else { + cur->nbr *= 2024; + cur = cur->next; + } + } + } + + //print_struc(head); + printf("count: %lld\n", count); + + + return 0; +} diff --git a/11/test.txt b/11/test.txt new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/11/test.txt @@ -0,0 +1 @@ +125 17