part 1 working, part 2 brute force up to max int, solution may be over max int

This commit is contained in:
2024-12-17 17:53:21 +01:00
parent cd4ef3ff9d
commit cb4b212ab4
5 changed files with 440 additions and 0 deletions

69
2024/17/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/12/17 16:41:42 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) -lm
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
2024/17/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);
}

131
2024/17/part1.c Normal file
View File

@ -0,0 +1,131 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/17 17:00:12 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include "libft/libft.h"
static long int get_register(char *str)
{
while(*str && *str != ':')
str++;
return(ft_atoi(str + 1));
}
static void parse_input(char **split, int *reg, uint8_t **ops, size_t *len)
{
char *tmp;
int i;
reg[0] = get_register(*split);
split++;
reg[1] = get_register(*split);
split++;
reg[2] = get_register(*split);
split++;
*len = 0;
tmp = *split;
while(*tmp)
{
if(ft_isdigit(*tmp))
(*len)++;
tmp++;
}
i = 0;
*ops = malloc(*len);
tmp = *split;
while(*tmp)
{
while(*tmp && !ft_isdigit(*tmp))
tmp++;
if(*tmp)
(*ops)[i] = (uint8_t)atoi(tmp);
i++;
while(ft_isdigit(*tmp))
tmp++;
}
}
static int combo(uint8_t val, int *reg)
{
if(val <= 3)
return(val);
if(val == 7)
{
printf("err\n");
exit(1);
}
return(*(reg + (val - 4)));
}
static void exec_instructions(uint8_t *ops, int *reg, size_t len)
{
size_t instr_ptr;
instr_ptr = 0;
while(instr_ptr < len)
{
// printf("op : %d, %d\n", ops[instr_ptr], ops[instr_ptr + 1]);
switch (ops[instr_ptr])
{
case 0:
reg[0] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
instr_ptr += 2;
break;
case 1:
reg[1] = reg[1] ^ ops[instr_ptr + 1];
instr_ptr += 2;
break;
case 2:
reg[1] = combo(ops[instr_ptr + 1], reg) % 8;
instr_ptr += 2;
break;
case 3:
if(reg[0])
instr_ptr = ops[instr_ptr + 1];
else
instr_ptr += 2;
break;
case 4:
reg[1] = reg[1] ^ reg[2];
instr_ptr += 2;
break;
case 5:
printf("%u,", combo(ops[instr_ptr + 1], reg) % 8);
instr_ptr += 2;
break;
case 6:
reg[1] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
instr_ptr += 2;
break;
case 7:
reg[2] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
instr_ptr += 2;
break;
}
}
}
long int resolve_part1(char *input, char **split)
{
(void)input;
int reg[3];
uint8_t *ops;
size_t len;
parse_input(split, reg, &ops, &len);
exec_instructions(ops, reg, len);
printf("\n");
return(0);
}

168
2024/17/part2.c Normal file
View File

@ -0,0 +1,168 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/17 17:50:43 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>
#include "libft/libft.h"
static long int get_register(char *str)
{
while(*str && *str != ':')
str++;
return(ft_atoi(str + 1));
}
static void parse_input(char **split, int *reg, uint8_t **ops, size_t *len)
{
char *tmp;
int i;
reg[0] = get_register(*split);
split++;
reg[1] = get_register(*split);
split++;
reg[2] = get_register(*split);
split++;
*len = 0;
tmp = *split;
while(*tmp)
{
if(ft_isdigit(*tmp))
(*len)++;
tmp++;
}
i = 0;
*ops = malloc(*len);
tmp = *split;
while(*tmp)
{
while(*tmp && !ft_isdigit(*tmp))
tmp++;
if(*tmp)
(*ops)[i] = (uint8_t)atoi(tmp);
i++;
while(ft_isdigit(*tmp))
tmp++;
}
}
static int combo(uint8_t val, int *reg)
{
if(val <= 3)
return(val);
if(val == 7)
{
printf("err\n");
exit(1);
}
return(*(reg + (val - 4)));
}
static size_t exec_instructions(uint8_t *ops, int *reg, size_t len, uint8_t *out)
{
size_t instr_ptr;
size_t out_ptr;
instr_ptr = 0;
out_ptr = 0;
while(instr_ptr < len)
{
// printf("op : %d, %d\n", ops[instr_ptr], ops[instr_ptr + 1]);
switch (ops[instr_ptr])
{
case 0:
reg[0] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
instr_ptr += 2;
break;
case 1:
reg[1] = reg[1] ^ ops[instr_ptr + 1];
instr_ptr += 2;
break;
case 2:
reg[1] = combo(ops[instr_ptr + 1], reg) % 8;
instr_ptr += 2;
break;
case 3:
if(reg[0])
instr_ptr = ops[instr_ptr + 1];
else
instr_ptr += 2;
break;
case 4:
reg[1] = reg[1] ^ reg[2];
instr_ptr += 2;
break;
case 5:
out[out_ptr++] = combo(ops[instr_ptr + 1], reg) % 8;
instr_ptr += 2;
break;
case 6:
reg[1] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
instr_ptr += 2;
break;
case 7:
reg[2] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
instr_ptr += 2;
break;
}
if(out_ptr == len)
break;
}
return(out_ptr);
}
void show_progress(struct timeval start, int cur)
{
struct timeval now;
double itps;
double sec_diff;
gettimeofday(&now, 0);
sec_diff = (((double)now.tv_usec - (double)start.tv_usec) / 1000000) + (now.tv_sec - start.tv_sec);
itps = cur / sec_diff;
printf("currently: %d, itps : %f, time remaining until max int : %fs \r", cur, itps, (2147483647 - cur) / itps);
fflush(stdout);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
int reg[3];
uint8_t *ops;
size_t len;
uint8_t *out;
size_t out_len;
int i;
struct timeval start;
parse_input(split, reg, &ops, &len);
out = malloc(len);
gettimeofday(&start, 0);
i = 1;
while(1)
{
reg[0] = i;
reg[1] = 0;
reg[2] = 0;
out_len = exec_instructions(ops, reg, len, out);
if(out_len == len && !memcmp(out, ops, len))
return(i);
i++;
if(i % 1000000 == 0)
show_progress(start, i);
}
return(0);
}

5
2024/17/test Normal file
View File

@ -0,0 +1,5 @@
Register A: 117440
Register B: 0
Register C: 0
Program: 0,3,5,4,3,0