finish 09 in suffer

This commit is contained in:
Xamora 2024-12-10 10:39:35 +01:00
parent febcd39546
commit a14d1dd8c0
7 changed files with 331 additions and 0 deletions

41
09/Makefile Normal 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
09/_.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
09/main.txt Normal file

File diff suppressed because one or more lines are too long

91
09/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;
}

77
09/part1.c Normal file
View file

@ -0,0 +1,77 @@
#include "_.h"
void display(char **numbers) {
for (int i = 0; numbers[i] != NULL; i++) {
for (int j = 0; numbers[i][j] != '\0'; j++)
printf("%c", numbers[i][j]);
printf(" ");
}
printf("\n");
}
char **first_line(char *input) {
char **numbers = calloc(pow(strlen(input), 2) + 1, sizeof(char*));
int count = 0;
int id = 0;
for (int i = 0; input[i] != '\0'; i++) {
int nbr = atoi(strndup(&input[i], 1));
char *add = calloc(11, 1);
add[0] = '.';
if (i % 2 == 0)
sprintf(add, "%d", id++);
for (int j = 0; j < nbr; j++)
numbers[count++] = strdup(add);
}
//display(numbers);
return numbers;
}
char **finish(char **numbers) {
long i = 1;
long lens = strslen(numbers);
for (; i < lens; i++) {
char *to_place;
long j = lens - 1;
for (;numbers[j][0] == '.'; j--);
to_place = numbers[j];
for (long k = 0; k < lens; k++) {
if (numbers[k][0] == '.') {
for (int w = 0; w < strlen(numbers[j]); w++)
numbers[k][w] = to_place[w];
break;
}
}
for (int k = 0; numbers[j][k] != '\0'; k++)
numbers[j][k] = '.';
}
//display(numbers);
return numbers;
}
long cal(char **numbers) {
long calc = 0;
int i = 0;
for (; numbers[i] != NULL && numbers[i][0] != '.'; i++)
calc += i * atol(numbers[i]);
return calc;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **numbers = first_line(input);
long result = cal(finish(numbers));
//display(numbers);
printf("result: %ld\n", result);
return 0;
}

93
09/part2.c Normal file
View file

@ -0,0 +1,93 @@
#include "_.h"
void display(char **numbers) {
for (int i = 0; numbers[i] != NULL; i++) {
for (int j = 0; numbers[i][j] != '\0'; j++)
printf("%c", numbers[i][j]);
printf(" ");
}
printf("\n");
}
char **first_line(char *input) {
char **numbers = calloc(pow(strlen(input), 2) + 1, sizeof(char*));
int count = 0;
int id = 0;
for (int i = 0; input[i] != '\0'; i++) {
int nbr = atoi(strndup(&input[i], 1));
char *add = calloc(255, 1);
add[0] = '.';
if (i % 2 == 0)
sprintf(add, "%d", id++);
for (int j = 0; j < nbr; j++)
numbers[count++] = strdup(add);
}
//display(numbers);
return numbers;
}
char **finish(char **numbers) {
long i = 0;
long lens = strslen(numbers);
for (; i < lens - 1;) {
char *to_place[10] = { [0 ... 9] = NULL };
long j = lens - 1 - i;
for (;numbers[j][0] == '.' && j > 0; j--);
i = lens - j - 1;
int w = 0;
for (; j - w > 0 && strcmp(numbers[j], numbers[j - w]) == 0; w++) {
to_place[w] = numbers[j - w];
}
int placed = 0;
for (long k = 0; k < lens && k < j; k++) {
if (numbers[k][0] == '.') {
int y = 0;
for (; numbers[k + y] != NULL && numbers[k + y][0] == '.'; y++);
if (y < w)
continue;
placed = 1;
for (int v = 0; to_place[v] != NULL; v++) {
for (int x = 0; to_place[v][x] != '\0'; x++) {
numbers[k + v][x] = to_place[v][x];
}
}
break;
}
}
if (placed)
for (int k = 0; to_place[k] != NULL; k++)
for (int l = 0; to_place[k][l] != '\0'; l++)
to_place[k][l] = '.';
i += strslen(to_place);
}
//display(numbers);
return numbers;
}
long cal(char **numbers) {
long calc = 0;
int i = 0;
for (; numbers[i] != NULL; i++)
calc += i * atol(numbers[i]);
return calc;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **numbers = first_line(input);
long result = cal(finish(numbers));
//display(numbers);
printf("result: %ld\n", result);
return 0;
}

1
09/test.txt Normal file
View file

@ -0,0 +1 @@
2333133121414131402