finish 13 part 1, too much to resolve part 2 maybe later

This commit is contained in:
Xamora 2024-12-13 07:13:19 +01:00
parent c4d7c86038
commit af397ec3db
10 changed files with 1566 additions and 22 deletions

View file

@ -89,3 +89,10 @@ long strslen(char **strs) {
count++; count++;
return count; return count;
} }
char **strsdup(char **strs) {
char **new_strs = calloc(strslen(strs) + 1, sizeof(char*));
for (int i = 0; strs[i] != NULL; i++)
new_strs[i] = strdup(strs[i]);
return new_strs;
}

View file

@ -41,7 +41,7 @@ int test(int ***already, int ***sides_lines, int check, int *sides, int y, int x
(*sides_lines)[y][x] = 0; (*sides_lines)[y][x] = 0;
for (; i >= 0; i--) for (; i >= 0; i--)
(*sides_lines)[y + i][x] = 0; (*sides_lines)[y + (i * abs_x)][x + (i * abs_y)] = 0;
i = 0; i = 0;
break; break;
} }
@ -51,24 +51,28 @@ int test(int ***already, int ***sides_lines, int check, int *sides, int y, int x
int top = 0, bottom = 0, left = 0, right = 0; int top = 0, bottom = 0, left = 0, right = 0;
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
top = top ? 1 : (*already)[y - 1 + (j * abs_x)][x + (j * abs_y)] == check; top = top ? 1 : (*already)[y - 1 + (j * abs_x)][x + (j * abs_y)] == check;
if (top && add_y != 1) { if (top && add_y != 0) {
test(already, sides_lines, check, sides, y + 1, x, h, w, 0, 1); for (int w = 0; ((*already)[y + w][x + 1] == check
test(already, sides_lines, check, sides, y + 1, x, h, w, 0, -1); ||(*already)[y + w][x - 1] == check)
&& y + w < h
&& (*already)[y + w][x] != check
; w++)
(*sides_lines)[y + w][x] = *sides + 1;
} }
bottom = bottom ? 1 : (*already)[y + 1 + (j * abs_x)][x + (j * abs_y)] == check; bottom = bottom ? 1 : (*already)[y + 1 + (j * abs_x)][x + (j * abs_y)] == check;
if (bottom && add_y != -1) { if (bottom && add_y != 0) {
test(already, sides_lines, check, sides, y - 1, x, h, w, 0, 1); for (int w = 0; ((*already)[y + 1][x - w] == check
test(already, sides_lines, check, sides, y - 1, x, h, w, 0, -1); ||(*already)[y - 1][x - w] == check)
&& y + w < h
&& (*already)[y][x - w] != check
; w++)
(*sides_lines)[y - w][x] = *sides + 1;
} }
right = right ? 1 : (*already)[y + (j * abs_x)][x + 1 + (j * abs_y)] == check; right = right ? 1 : (*already)[y + (j * abs_x)][x + 1 + (j * abs_y)] == check;
if (right && add_x != 1) { if (right && add_x != 1) {
test(already, sides_lines, check, sides, y, x, h, w, 1, 0);
test(already, sides_lines, check, sides, y, x, h, w, -1, 0);
} }
left = left ? 1 : (*already)[y + (j * abs_x)][x - 1 + (j * abs_y)] == check; left = left ? 1 : (*already)[y + (j * abs_x)][x - 1 + (j * abs_y)] == check;
if (left && add_x != -1) { if (left && add_x != -1) {
test(already, sides_lines, check, sides, y, x, h, w, 1, 0);
test(already, sides_lines, check, sides, y, x, h, w, -1, 0);
} }
} }
*sides += (top + bottom + left + right); *sides += (top + bottom + left + right);
@ -104,10 +108,14 @@ int get_side(int ***already, int check, int h, int w) {
} }
} }
test(already, &sides_lines, check, &sides, y, x, h, w, 1, 0); if (test(already, &sides_lines, check, &sides, y, x, h, w, 1, 0))
test(already, &sides_lines, check, &sides, y, x, h, w, -1, 0); continue;
test(already, &sides_lines, check, &sides, y, x, h, w, 0, 1); if (test(already, &sides_lines, check, &sides, y, x, h, w, -1, 0))
test(already, &sides_lines, check, &sides, y, x, h, w, 0, -1); continue;
if (test(already, &sides_lines, check, &sides, y, x, h, w, 0, 1))
continue;
if (test(already, &sides_lines, check, &sides, y, x, h, w, 0, -1))
continue;
} }
} }

View file

@ -1,7 +1,5 @@
CCCJFFF VIVCC
VCJJCFE VIIIC
VCCJJEE IIIII
IICJJEE IIISI
IIIJJEE MMISS
ISIJEEE
ISSJEEE

41
13/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
13/_.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);

1279
13/main.txt Normal file

File diff suppressed because it is too large Load diff

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

44
13/part1.c Normal file
View file

@ -0,0 +1,44 @@
#include "_.h"
int get_number_input_to_wint(int A[], int B[], int prize[]) {
int i = 0, j = 0;
int min = 1000;
for (; i < 1000; i++) {
j = 0;
for (; j < 1000; j++) {
if ((A[0] * i) + (B[0] * j) == prize[0]
&& (A[1] * i) + (B[1] * j) == prize[1])
min = MIN(i * 3 + j, min);
}
}
//printf("tokens: %d\n\n", min);
return min;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int token_spent = 0;
for (int i = 0; inputs[i] != NULL; i+=3) {
char **button_A = str_split(strdup(inputs[i]), ' ');
int A[2] = {atoi(button_A[0]), atoi(button_A[1])};
char **button_B = str_split(strdup(inputs[i + 1]), ' ');
int B[2] = {atoi(button_B[0]), atoi(button_B[1])};
char **prizes = str_split(strdup(inputs[i + 2]), ' ');
int prize[2] = {atoi(prizes[0]), atoi(prizes[1])};
int number = get_number_input_to_wint(A, B, prize);
token_spent += number == 1000 ? 0 : number;
}
printf("token_spent: %d\n", token_spent);
return 0;
}

34
13/part2.c Normal file
View file

@ -0,0 +1,34 @@
#include "_.h"
long long solve(long long A[], long long B[], long long prize[]) {
return 0;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
long long token_spent = 0;
for (long long i = 0; inputs[i] != NULL; i+=3) {
char **button_A = str_split(strdup(inputs[i]), ' ');
long long A[2] = {atoi(button_A[0]), atoi(button_A[1])};
char **button_B = str_split(strdup(inputs[i + 1]), ' ');
long long B[2] = {atoi(button_B[0]), atoi(button_B[1])};
char **prizes = str_split(strdup(inputs[i + 2]), ' ');
long long prize[2] = {atoi(prizes[0]), atoi(prizes[1])};
solve(A, B, prize);
}
printf("token_spent: %lld\n", token_spent);
return 0;
}

15
13/test.txt Normal file
View file

@ -0,0 +1,15 @@
94 34
22 67
8400 5400
26 66
67 21
12748 12176
17 86
84 37
7870 6450
69 23
27 71
18641 10279