add 2022 day 10 and 11

This commit is contained in:
2024-12-19 23:23:16 +01:00
parent f1d95ff41f
commit 0f0aa51baf
11 changed files with 1023 additions and 0 deletions

69
2022/10/Makefile Normal file
View File

@ -0,0 +1,69 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
# Updated: 2024/10/29 20:43:12 by tomoron ### ########.fr #
# #
# **************************************************************************** #
SRCS = main.c\
part1.c\
part2.c
OBJS = $(SRCS:.c=.o)
FLAGS = -Wextra -Werror -Wall -g
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

67
2022/10/main.c Normal file
View File

@ -0,0 +1,67 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.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: %ld\n", resolve_part2(input, split));
ft_free_str_arr(split);
free(input);
free(input_cpy);
return(0);
}

59
2022/10/part1.c Normal file
View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/19 21:17:39 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static long int get_nbr(char *line)
{
while(*line && !ft_isdigit(*line) && *line != '-')
line++;
return(atol(line));
}
static long int nb_at_cycle(char **split, int cycle)
{
long int reg;
reg = 1;
cycle--;
while(*split && cycle >= 2)
{
if(**split == 'a')
{
if(cycle <= 1)
break;
reg += get_nbr(*split);
cycle -= 2;
}
else
cycle--;
split++;
}
return(reg);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
long int res;
int cycle;
res = 0;
cycle = 20;
while(cycle <= 220)
{
res += nb_at_cycle(split, cycle) * cycle;
cycle += 40;
}
return(res);
}

83
2022/10/part2.c Normal file
View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/19 21:40:11 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static long int get_nbr(char *line)
{
while(*line && !ft_isdigit(*line) && *line != '-')
line++;
return(atol(line));
}
static char **create_map(void)
{
char **res;
int i;
res = malloc(7 * sizeof(char *));
res[6] = 0;
i = 0;
while(i < 6)
{
res[i] = malloc(41);
res[i][40] = 0;
ft_memset(res[i], '.', 40);
i++;
}
return(res);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
long int reg;
int cycle;
char **map;
int flag;
map = create_map();
cycle = 0;
reg = 1;
flag = 0;
if(reg - (cycle % 40) >= -1 && reg - (cycle % 40) <= 1)
map[cycle / 40][cycle % 40] = '#';
while(*split)
{
if(**split == 'a')
{
if(!flag)
flag = 1;
else
{
flag = 0;
reg += get_nbr(*split);
split++;
}
}
else
split++;
cycle++;
if(reg - (cycle % 40) >= -1 && reg - (cycle % 40) <= 1)
map[cycle / 40][cycle % 40] = '#';
}
cycle = 0;
while(map[cycle])
{
printf("%s\n", map[cycle]);
cycle ++;
}
ft_free_str_arr(map);
return(-1);
}

146
2022/10/test Normal file
View File

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

69
2022/11/Makefile Normal file
View File

@ -0,0 +1,69 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
# Updated: 2024/10/29 20:43:12 by tomoron ### ########.fr #
# #
# **************************************************************************** #
SRCS = main.c\
part1.c\
part2.c
OBJS = $(SRCS:.c=.o)
FLAGS = -Wextra -Werror -Wall -g
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

67
2022/11/main.c Normal file
View File

@ -0,0 +1,67 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.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: %ld\n", resolve_part2(input, split));
ft_free_str_arr(split);
free(input);
free(input_cpy);
return(0);
}

214
2022/11/part1.c Normal file
View File

@ -0,0 +1,214 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/19 23:22:50 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
typedef enum e_op
{
MUL,
ADD,
SQR
} t_op;
typedef struct s_items
{
int value;
struct s_items *next;
} t_items;
typedef struct s_monkey
{
t_items *items;
t_op op;
int op_nb;
int test_div;
int to_true;
int to_false;
long int nb_inspect;
} t_monkey;
static int count_monkeys(char **split)
{
int len;
len = 0;
while(*split)
{
if(**split == 'M')
len++;
split++;
}
return(len);
}
static t_items *get_items(char *line)
{
t_items *res;
t_items *new;
t_items *last;
last = 0;
res = 0;
while(*line)
{
new = malloc(sizeof(t_items));
while(*line && !ft_isdigit(*line))
line++;
new->value = ft_atoi(line);
new->next = 0;
if(!last)
res = new;
else
last->next = new;
last = new;
while(ft_isdigit(*line))
line++;
}
return(res);
}
static void get_op(t_monkey *monkey, char *line)
{
while(*line && *line != '*' && *line != '+')
line++;
if(!*line)
exit(1);
if(!ft_strcmp(line, "* old"))
{
monkey->op = SQR;
monkey->op_nb = 0;
return ;
}
if(*line == '*')
monkey->op = MUL;
else
monkey->op = ADD;
line++;
monkey->op_nb = ft_atoi(line);
}
static int get_first_nb(char *line)
{
while(*line && !ft_isdigit(*line))
line++;
return(ft_atoi(line));
}
static t_monkey *get_monkeys(char **split, int *len)
{
t_monkey *res;
int i;
*len = count_monkeys(split);
res = malloc(*len * sizeof(t_monkey));
i = 0;
while(*split)
{
split++;
res[i].items = get_items(*split);
split++;
get_op(res + i, *split);
split++;
res[i].test_div = get_first_nb(*split);
split++;
res[i].to_true = get_first_nb(*split);
split++;
res[i].to_false = get_first_nb(*split);
split++;
i++;
}
return(res);
}
void give_item(t_monkey *from, t_monkey *to)
{
t_items *item;
t_items *dest;
item = from->items;
from->items = item->next;
item->next = 0;
if(!to->items)
to->items = item;
else
{
dest = to->items;
while(dest->next)
dest = dest->next;
dest->next = item;
}
}
void handle_items(t_monkey *monkeys, t_monkey *cur)
{
while(cur->items)
{
cur->nb_inspect++;
if(cur->op == MUL)
cur->items->value *= cur->op_nb;
else if(cur->op == ADD)
cur->items->value += cur->op_nb;
else if(cur->op == SQR)
cur->items->value *= cur->items->value;
cur->items->value /= 3;
if(cur->items->value % cur->test_div == 0)
give_item(cur, monkeys + cur->to_true);
else
give_item(cur, monkeys + cur->to_false);
}
}
static void loop_all_monkeys(t_monkey *monkeys, int nb_monkey)
{
int i = 0;
while(i < nb_monkey)
{
handle_items(monkeys, monkeys + i);
i++;
}
}
long int resolve_part1(char *input, char **split)
{
(void)input;
int nb_monkey;
int i;
long int top[2];
t_monkey *monkeys;
monkeys = get_monkeys(split, &nb_monkey);
i = 0;
while(i < 20)
{
loop_all_monkeys(monkeys, nb_monkey);
i++;
}
top[0] = 0;
top[1] = 0;
i = 0;
while(i < nb_monkey)
{
if(monkeys[i].nb_inspect > top[0])
{
top[1] = top[0];
top[0] = monkeys[i].nb_inspect;
}
else if(monkeys[i].nb_inspect > top[1])
top[1] = monkeys[i].nb_inspect;
i++;
}
return(top[0] * top[1]);
}

221
2022/11/part2.c Normal file
View File

@ -0,0 +1,221 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/19 23:22:45 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "libft/libft.h"
typedef enum e_op
{
MUL,
ADD,
SQR
} t_op;
typedef struct s_items
{
uint64_t value;
struct s_items *next;
} t_items;
typedef struct s_monkey
{
t_items *items;
t_op op;
int op_nb;
int test_div;
int to_true;
int to_false;
long int nb_inspect;
} t_monkey;
static int count_monkeys(char **split)
{
int len;
len = 0;
while(*split)
{
if(**split == 'M')
len++;
split++;
}
return(len);
}
static t_items *get_items(char *line)
{
t_items *res;
t_items *new;
t_items *last;
last = 0;
res = 0;
while(*line)
{
new = malloc(sizeof(t_items));
while(*line && !ft_isdigit(*line))
line++;
new->value = ft_atoi(line);
new->next = 0;
if(!last)
res = new;
else
last->next = new;
last = new;
while(ft_isdigit(*line))
line++;
}
return(res);
}
static void get_op(t_monkey *monkey, char *line)
{
while(*line && *line != '*' && *line != '+')
line++;
if(!*line)
exit(1);
if(!ft_strcmp(line, "* old"))
{
monkey->op = SQR;
monkey->op_nb = 0;
return ;
}
if(*line == '*')
monkey->op = MUL;
else
monkey->op = ADD;
line++;
monkey->op_nb = ft_atoi(line);
}
static int get_first_nb(char *line)
{
while(*line && !ft_isdigit(*line))
line++;
return(ft_atoi(line));
}
static t_monkey *get_monkeys(char **split, int *len, uint64_t *mod)
{
t_monkey *res;
int i;
*len = count_monkeys(split);
res = malloc(*len * sizeof(t_monkey));
i = 0;
while(*split)
{
split++;
res[i].items = get_items(*split);
split++;
get_op(res + i, *split);
split++;
res[i].test_div = get_first_nb(*split);
if(!*mod)
*mod = res[i].test_div;
else
*mod *= res[i].test_div;
split++;
res[i].to_true = get_first_nb(*split);
split++;
res[i].to_false = get_first_nb(*split);
split++;
i++;
}
return(res);
}
static void give_item(t_monkey *from, t_monkey *to)
{
t_items *item;
t_items *dest;
item = from->items;
from->items = item->next;
item->next = 0;
if(!to->items)
to->items = item;
else
{
dest = to->items;
while(dest->next)
dest = dest->next;
dest->next = item;
}
}
static void handle_items(t_monkey *monkeys, t_monkey *cur, uint64_t mod)
{
while(cur->items)
{
cur->nb_inspect++;
if(cur->op == MUL)
cur->items->value *= cur->op_nb;
else if(cur->op == ADD)
cur->items->value += cur->op_nb;
else if(cur->op == SQR)
cur->items->value *= cur->items->value;
cur->items->value = cur->items->value % mod;
if(cur->items->value % cur->test_div == 0)
give_item(cur, monkeys + cur->to_true);
else
give_item(cur, monkeys + cur->to_false);
}
}
static void loop_all_monkeys(t_monkey *monkeys, int nb_monkey, uint64_t mod)
{
int i = 0;
while(i < nb_monkey)
{
handle_items(monkeys, monkeys + i, mod);
i++;
}
}
long int resolve_part2(char *input, char **split)
{
(void)input;
int nb_monkey;
uint64_t mod;
int i;
long int top[2];
t_monkey *monkeys;
mod = 0;
monkeys = get_monkeys(split, &nb_monkey, &mod);
i = 0;
while(i < 10000)
{
loop_all_monkeys(monkeys, nb_monkey, mod);
i++;
}
top[0] = 0;
top[1] = 0;
i = 0;
while(i < nb_monkey)
{
if(monkeys[i].nb_inspect > top[0])
{
top[1] = top[0];
top[0] = monkeys[i].nb_inspect;
}
else if(monkeys[i].nb_inspect > top[1])
top[1] = monkeys[i].nb_inspect;
i++;
}
return(top[0] * top[1]);
}

27
2022/11/test Normal file
View File

@ -0,0 +1,27 @@
Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1

View File

@ -15,6 +15,7 @@ CC = cc
SRCS = ft_atoi.c\ SRCS = ft_atoi.c\
ft_bzero.c\ ft_bzero.c\
ft_calloc.c\ ft_calloc.c\
ft_swap.c\
ft_isalnum.c\ ft_isalnum.c\
ft_isalpha.c\ ft_isalpha.c\
ft_isascii.c\ ft_isascii.c\