finish 11, but not give part 2, need more power

This commit is contained in:
Xamora 2024-12-11 06:43:18 +01:00
parent e07a4f6f93
commit 8a25923ba6
7 changed files with 297 additions and 0 deletions

41
11/Makefile Executable file
View file

@ -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

27
11/_.h Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <strings.h>
#include <wctype.h>
#include <math.h>
#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);

1
11/main.txt Normal file
View file

@ -0,0 +1 @@
5688 62084 2 3248809 179 79 0 172169

91
11/mylib.c Normal file
View file

@ -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;
}

61
11/part1.c Normal file
View file

@ -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;
}

75
11/part2.c Normal file
View file

@ -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;
}

1
11/test.txt Normal file
View file

@ -0,0 +1 @@
125 17