add day 19
This commit is contained in:
69
2024/19/Makefile
Normal file
69
2024/19/Makefile
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
|
||||||
|
# Updated: 2024/12/19 12:54:51 by tomoron ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
SRCS = main.c\
|
||||||
|
part1.c\
|
||||||
|
part2.c
|
||||||
|
|
||||||
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
FLAGS = -Wextra -Werror -Wall -g -O3
|
||||||
|
|
||||||
|
LIB = libft/libft.a
|
||||||
|
|
||||||
|
all: $(LIB) a.out test
|
||||||
|
cp test cur_input
|
||||||
|
./a.out
|
||||||
|
|
||||||
|
f: $(LIB) a.out input
|
||||||
|
cp input cur_input
|
||||||
|
./a.out
|
||||||
|
|
||||||
|
test:
|
||||||
|
touch test
|
||||||
|
|
||||||
|
input:
|
||||||
|
touch input
|
||||||
|
|
||||||
|
cp:
|
||||||
|
cp part1.c part2.c
|
||||||
|
sed -i 's/resolve_part1/resolve_part2/g' part2.c
|
||||||
|
|
||||||
|
$(LIB): libft
|
||||||
|
make -C libft
|
||||||
|
|
||||||
|
libft:
|
||||||
|
cp -r ~/Desktop/aoc/libft .
|
||||||
|
|
||||||
|
main.c:
|
||||||
|
cp ~/Desktop/aoc/main.c .
|
||||||
|
|
||||||
|
part1.c:
|
||||||
|
cp ~/Desktop/aoc/part1.c .
|
||||||
|
|
||||||
|
part2.c:
|
||||||
|
cp ~/Desktop/aoc/part2.c .
|
||||||
|
|
||||||
|
a.out: main.c $(OBJS) $(LIB)
|
||||||
|
clang $(FLAGS) $(OBJS) $(LIB)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(OBJS)
|
||||||
|
rm -rf libft
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -rf a.out
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
clang $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
.PHONY: t all clean fclean cp
|
68
2024/19/main.c
Normal file
68
2024/19/main.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split);
|
||||||
|
long int resolve_part2(char *input, char **split);
|
||||||
|
|
||||||
|
char *get_input(char *file)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int rd_len;
|
||||||
|
char buffer[1000];
|
||||||
|
int len;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
fd = open(file, O_RDONLY);
|
||||||
|
len = 0;
|
||||||
|
if(fd < 0)
|
||||||
|
return(0);
|
||||||
|
rd_len = -1;
|
||||||
|
while(rd_len)
|
||||||
|
{
|
||||||
|
rd_len = read(fd, buffer, 1000);
|
||||||
|
len+= rd_len;
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
res = malloc(len + 1);
|
||||||
|
if(!res)
|
||||||
|
return(0);
|
||||||
|
fd = open(file, O_RDONLY);
|
||||||
|
if(fd < 0)
|
||||||
|
return(0);
|
||||||
|
rd_len = read(fd,res, len);
|
||||||
|
res[len] = 0;
|
||||||
|
close(fd);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *input;
|
||||||
|
char *input_cpy;
|
||||||
|
char **split;
|
||||||
|
|
||||||
|
input = get_input("cur_input");
|
||||||
|
input_cpy = ft_strdup(input);
|
||||||
|
if(!input || !input_cpy)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "file read error\n");
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
split = ft_split(input, '\n');
|
||||||
|
if(!split)
|
||||||
|
return(1);
|
||||||
|
printf("result part 1: %ld\n", resolve_part1(input, split));
|
||||||
|
ft_free_str_arr(split);
|
||||||
|
split = ft_split(input_cpy, '\n');
|
||||||
|
if(!split)
|
||||||
|
return(1);
|
||||||
|
printf("result part 2: %lu\n", resolve_part2(input, split));
|
||||||
|
ft_free_str_arr(split);
|
||||||
|
free(input);
|
||||||
|
free(input_cpy);
|
||||||
|
return(0);
|
||||||
|
}
|
54
2024/19/part1.c
Normal file
54
2024/19/part1.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part1.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/12/19 11:46:36 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
static int check_line(char *line, char **colors)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if(!*line)
|
||||||
|
return(1);
|
||||||
|
while(colors[i])
|
||||||
|
{
|
||||||
|
len = ft_strlen(colors[i]);
|
||||||
|
if(!ft_strncmp(line, colors[i], len))
|
||||||
|
if(check_line(line + len, colors))
|
||||||
|
return(1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
char **colors;
|
||||||
|
long int res;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
if(!*split)
|
||||||
|
return(-1);
|
||||||
|
colors = ft_split_set(*split, ", ");
|
||||||
|
split++;
|
||||||
|
while(*split)
|
||||||
|
{
|
||||||
|
res += check_line(*split, colors);
|
||||||
|
split++;
|
||||||
|
}
|
||||||
|
ft_free_str_arr(colors);
|
||||||
|
return(res);
|
||||||
|
}
|
107
2024/19/part2.c
Normal file
107
2024/19/part2.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/12/19 13:06:39 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
typedef struct s_mem
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
long int value;
|
||||||
|
struct s_mem *next;
|
||||||
|
} t_mem;
|
||||||
|
|
||||||
|
static long int mem_get(t_mem *mem, char *line)
|
||||||
|
{
|
||||||
|
while(mem)
|
||||||
|
{
|
||||||
|
if(mem->line == line)
|
||||||
|
return(mem->value);
|
||||||
|
mem = mem->next;
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mem_add(t_mem **mem, char *line, long int val)
|
||||||
|
{
|
||||||
|
t_mem *new;
|
||||||
|
|
||||||
|
new = malloc(sizeof(t_mem));
|
||||||
|
new->line = line;
|
||||||
|
new->value = val;
|
||||||
|
new->next = *mem;
|
||||||
|
*mem = new;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void free_mem(t_mem **mem)
|
||||||
|
{
|
||||||
|
t_mem *tmp;
|
||||||
|
|
||||||
|
while(*mem)
|
||||||
|
{
|
||||||
|
tmp = (*mem)->next;
|
||||||
|
free(*mem);
|
||||||
|
*mem = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int check_line(char *line, char **colors, int rec)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int len;
|
||||||
|
long int res;
|
||||||
|
static t_mem *mem;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if(!*line)
|
||||||
|
return(1);
|
||||||
|
res = mem_get(mem, line);
|
||||||
|
if(res)
|
||||||
|
return(res);
|
||||||
|
while(colors[i])
|
||||||
|
{
|
||||||
|
len = ft_strlen(colors[i]);
|
||||||
|
if(!ft_strncmp(line, colors[i], len))
|
||||||
|
res += check_line(line + len, colors, 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
(void)rec;
|
||||||
|
(void)mem_add;
|
||||||
|
(void)free_mem;
|
||||||
|
if(rec)
|
||||||
|
mem_add(&mem, line, res);
|
||||||
|
else
|
||||||
|
free_mem(&mem);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part2(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
char **colors;
|
||||||
|
long int res;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
if(!*split)
|
||||||
|
return(-1);
|
||||||
|
colors = ft_split_set(*split, ", ");
|
||||||
|
split++;
|
||||||
|
while(*split)
|
||||||
|
{
|
||||||
|
res += check_line(*split, colors, 0);
|
||||||
|
split++;
|
||||||
|
}
|
||||||
|
ft_free_str_arr(colors);
|
||||||
|
return(res);
|
||||||
|
}
|
3
2024/19/test
Normal file
3
2024/19/test
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ubg, grgw, uwgb, rubwug, urbbw, uuuuu, uruwbug, bgw, wwwr, ug, gbwrug, bug, ubw, gbb, rur, grrurw, brbu, ggrr, bugrw, ggrbg, rwu, gruru, guur, rbw, brwg, rru, gwurbu, bwwur, urgb, rrb, rgug, uwwr, wuu, gbbuu, uurbuw, guwww, uwub, rrbr, gruubugg, rrrw, uugr, rrgu, gwuu, rurrr, wwuu, rwbww, ubbbb, wbrug, wrwuww, urg, uuurbrw, wbgug, gwrbg, ugr, rwgbub, bwg, wbrrgr, uwuw, wuwgbwrb, wbw, urur, ubrgrr, rgw, ggugww, grb, bru, gubr, bgrr, ugw, rgbwg, ugwgrr, rguwwr, rubu, rurbrgu, gbgwbr, ugg, wwrugbr, wbruw, ru, rww, rgrgwbr, uwgr, bbwb, wgwwggu, gwr, wgw, rrrubgu, rrwbgbb, burw, rrr, gg, ubbwbwwb, bgg, brgb, uwuru, rrw, uuu, wur, rrwg, buug, bubr, rurrg, uwwb, wu, rugb, rguub, buuw, gwgub, wwug, rubrrw, uguu, bgbbbub, ggwgbw, rug, uurwr, wburrb, wbu, ugu, gw, ggrw, gwrbwr, rbggrwbu, wugbg, gbg, rw, wbrwurb, rguuugrb, bbb, ugb, bwguwuw, ggr, uru, brr, urw, brrgg, uub, rbwu, bugrg, bwrgu, rrbbgw, wggrbgwu, wrbbgb, grrbgb, brruwr, rbgwrb, guw, rr, wgguuwg, rbg, brwgrrrg, gwwu, gggwb, wwbggwbg, ggb, wrg, uwgrg, gwb, gww, rurbbw, wgub, gu, u, rwgub, gbr, bbrbuwbw, grw, gwu, rrgrgw, uuwgrwr, bwbruggb, wggb, bwb, wrrugggb, ubbuubg, rubwu, wbbr, gbubr, rbggww, bbg, uwbbu, bbgb, brw, wrwggwr, gr, uubwwu, gbrb, wgwwrb, urwgb, brgbbg, rwbgwg, www, rbuw, bruwg, grwbu, gbww, rbuugwrw, wrbb, bwgb, gbubuu, bu, bgb, ugwbru, gwgb, rbrwbgu, wwubg, uw, grubw, wrw, wrb, ubuw, bgr, grg, bww, uwggr, bugg, wugb, urbwgb, gggw, ubrgruw, uwrrb, rbuu, rwuubu, guu, gugu, wbgggu, bbggggr, gug, wbgub, bb, uww, uug, bgbgrbr, gggr, bgurg, wrr, rubgg, bwrru, ubguu, wg, rbgrr, guugg, ubu, wruu, wgb, rgrburu, wbwgr, wb, grbru, bgwu, gub, brwr, ggg, wbggb, wuwurwb, uuw, ubbbr, gggbbg, ruur, bgug, wwwg, uggg, rgg, wwb, ggwrgww, bub, wubg, rggbbb, brgbu, ruuu, wrub, gur, uggbr, uur, rgbbb, gubruwwu, bwu, burugg, rwg, ugbw, bwbgggw, rburbgug, wug, ubgrb, rurbb, rwuug, wr, ggww, rgwgu, wrugu, gwwbr, uggbug, buruuu, guwgrg, wru, wwrbugg, grgg, rrbrr, urgrgbg, rbu, uwu, bugub, gwru, bbrg, w, wgu, buw, brbgwrb, bgrwbwgu, uwr, wwurbr, wuw, wbgbgwb, urbw, guuggub, rggggwu, rrg, rwbg, rbgg, ggwwbb, gruruu, gbwgr, gubuu, bgbr, ururg, wbr, rwb, rrrguug, rgu, wgru, rubuwr, ugbrg, guuw, urb, wwwuurgb, wwg, urbrw, grww, brug, bruu, ggu, grrurb, rgwgwr, guwrbrgu, wrgugr, gb, wwr, uwbguw, brggub, ugbb, wgbg, g, wrrrgw, ubbrb, brb, ruwrgub, bgrwuwwg, wgr, bbgwrr, ur, buu, ubr, rrwwb, wubbg, uubug, rrgw, wwu, wgwrw, uggur, urr, rwr, rgwrgw, brggrr, bbrubbw, bgbgu, guguw, bbw, gru, bg, ubwugu, ubb, brubug, wugg, brub, ggwb, bugrwgwr, wgg, ggw, rrwr, rub, gbw, rbbg, gwrwgwr, rbr, grwr, rgb, b, bbubrw, bwwgu, gwg, uuguwgg, brwgu, rwuuguw, ub, ruw, bwbb, gbu, uu, bgrrggw, uwwbgrg, bw, bbu, wbb, bwbu, bbr, wuuw, grr, rrbgr, rgr, gggbrru, bgu, rrguu, uwg, wbgw, gbbbbggr, rbrg, wbbb, rbrbrw, brgbb, uggb, wwgu, uugu, ruu, rrgb, wub, guwwww, bbugwgu, buwr, rg, uwubr, gwwbbrr, br, bwr, grrwrg, wbbub, ubugbr, uugb, rrgg
|
||||||
|
|
||||||
|
rgrruuurwuggbwgrggbrwruugrrrguwuubuwwrruwbbuubrwuwubgbwgur
|
Reference in New Issue
Block a user