finish 10, part2 is only one line

This commit is contained in:
Xamora 2024-12-10 11:12:58 +01:00
parent a14d1dd8c0
commit e07a4f6f93
9 changed files with 317 additions and 1 deletions

41
10/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

29
10/_.h Normal file
View file

@ -0,0 +1,29 @@
#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);
char **strsdup(char **strs);

57
10/main.txt Normal file
View file

@ -0,0 +1,57 @@
789865630100349810122456760105654587789823498301678705410
876696543231256721201309801238765496876012387432549812323
905787654340875430332219872389852345965430456541232101454
014765449850961045443818765456721231050121243210345696569
123814310761252036756903456765890102341567654400446787478
239901223450343129847812309857701213210478912321239870112
378210123411234980130123211098654312344327805410123721203
567411054100945673221045622567345403458910416569014874314
454302165257899854378132783451276234567234567878325965485
567214678346501765469221694340980129656105678963456875696
498543089655432356454330543298765498746569127632127704787
301672198790354567843894356132100384565478034543098013210
232987567581203210978765287045091223478328967656765123876
143823475676217891569987196786789310789110878947859874965
056710389564323765401076005499876501278012365432948765454
102345234410498654302105412386787432367873456721035439341
201276105321587663213234100345498101656921089810126928210
367888986987676565304566781234545432765430876712017817432
454993677896501476569645290123636721896321963103134506501
323034568765412389678730126788729800654403454214021676501
412126789892321018565021035499610914783212234565120989452
101215430701438989432106544321067323898302105456734012343
012107621890121876510127665233458734567676246789843109654
983498543701430701221678978102189676548985456799854328765
876501235612565652434507879213064581231234567878765016765
945432344013476543589410765015673290890103201967805679854
237651233023989610678321234324382106721090116789914789743
128940142104898720123451234543293445434589223654323876012
001230653415765431018960789632104345305679454986542945765
125021798596554104567875658734043216212368761077831034854
456189897687563245452106549025652207301454502560921003923
327234781076432236743089432110701108454563213461234212912
018985650165501149801676561229813239323645678678965307802
189176543234690032118745670107656545018732109569870456521
308012560125780145029634589018743896579887603432101567410
216543479876571236734556476129012987781094512321012198323
327612988967652987812347365437761016592323321434783084012
498909867658943887900198215678854123483415490549894563213
567678100147012096513278904589923567876506787658765454704
543563231230423123454562123238212450956543897601321067895
612154190321345278921043050120104321567872998512452367896
703013085430256567432234567431234982438901807403965456987
894102176321187456541107678432345670120156712394876435434
085013089434091076560128989841026761010205421085476528923
176324576543782185431032176721010859854312352376789117610
265434675345691298128745085432327998763456767465069006541
367645589210000341049876194309458709612567858564178123432
498596432100119657238789801218769612501678949653265678741
567487323765228798145698745657612523410987038734652169650
434321019874334567034561236764503412341879121025643050789
345010121089654321921650109853012501954348710110789841234
236789210128789110870743215432107607865219627896730932345
107898543279658012561894196501018216974008536105821801796
765467654789543653465445087454329345989123445234936767887
894321045601234743872346765585018211098512458965445216970
345470153456789812901349854396327602367401367872324105561
456789012109892101201256701287434501456513210121013234432

98
10/mylib.c Normal file
View file

@ -0,0 +1,98 @@
#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;
}
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;
}

39
10/part1.c Normal file
View file

@ -0,0 +1,39 @@
#include "_.h"
int find_way(char **inputs, int y, int x, char before) {
if (inputs[y][x] == '9') {
inputs[y][x] = 'X';
return 1;
}
int count = 0;
if (inputs[y + 1] != NULL && inputs[y + 1][x] == before + 1)
count += find_way(inputs, y + 1, x, before + 1);
if (y > 0 && inputs[y - 1][x] == before + 1)
count += find_way(inputs, y - 1, x, before + 1);
if (x < (int)strlen(inputs[y]) && inputs[y][x + 1] == before + 1)
count += find_way(inputs, y, x + 1, before + 1);
if (x > 0 && inputs[y][x - 1] == before + 1)
count += find_way(inputs, y, x - 1, before + 1);
return count;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int count = 0;
for (int i = 0; inputs[i] != NULL; i++) {
for (int j = 0; inputs[i][j] != '\0'; j++) {
if (inputs[i][j] == '0')
count += find_way(strsdup(inputs), i, j, '0');
}
}
printf("count: %d\n", count);
return 0;
}

37
10/part2.c Normal file
View file

@ -0,0 +1,37 @@
#include "_.h"
int find_way(char **inputs, int y, int x, char before) {
if (inputs[y][x] == '9')
return 1;
int count = 0;
if (inputs[y + 1] != NULL && inputs[y + 1][x] == before + 1)
count += find_way(inputs, y + 1, x, before + 1);
if (y > 0 && inputs[y - 1][x] == before + 1)
count += find_way(inputs, y - 1, x, before + 1);
if (x < (int)strlen(inputs[y]) && inputs[y][x + 1] == before + 1)
count += find_way(inputs, y, x + 1, before + 1);
if (x > 0 && inputs[y][x - 1] == before + 1)
count += find_way(inputs, y, x - 1, before + 1);
return count;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int count = 0;
for (int i = 0; inputs[i] != NULL; i++) {
for (int j = 0; inputs[i][j] != '\0'; j++) {
if (inputs[i][j] == '0')
count += find_way(strsdup(inputs), i, j, '0');
}
}
printf("count: %d\n", count);
return 0;
}

8
10/test.txt Normal file
View file

@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

7
10/test2.txt Normal file
View file

@ -0,0 +1,7 @@
10..9..
2...8..
3...7..
4567654
...8..3
...9..2
.....01

View file

@ -2,4 +2,4 @@
test "$1" == "" && exit 1
cp -r template "$1" || exit 1
cd "$1" || exit 1
chmod +x *.sh || exit 1
chmod +x Makefile || exit 1