add part 2 (good)
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/12/17 17:00:12 by tomoron ### ########.fr */
|
/* Updated: 2024/12/17 18:40:12 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -126,6 +126,7 @@ long int resolve_part1(char *input, char **split)
|
|||||||
|
|
||||||
parse_input(split, reg, &ops, &len);
|
parse_input(split, reg, &ops, &len);
|
||||||
exec_instructions(ops, reg, len);
|
exec_instructions(ops, reg, len);
|
||||||
|
printf("\nreg : %d, %d, %d", reg[0], reg[1], reg[2]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
101
2024/17/part2.c
101
2024/17/part2.c
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/12/17 17:50:43 by tomoron ### ########.fr */
|
/* Updated: 2024/12/18 01:15:31 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,18 +14,16 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include "libft/libft.h"
|
#include "libft/libft.h"
|
||||||
|
|
||||||
static long int get_register(char *str)
|
static long int get_register(char *str)
|
||||||
{
|
{
|
||||||
while(*str && *str != ':')
|
while(*str && *str != ':')
|
||||||
str++;
|
str++;
|
||||||
return(ft_atoi(str + 1));
|
return(atol(str + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_input(char **split, int *reg, uint8_t **ops, size_t *len)
|
static void parse_input(char **split, long int *reg, uint8_t **ops, size_t *len)
|
||||||
{
|
{
|
||||||
char *tmp;
|
char *tmp;
|
||||||
int i;
|
int i;
|
||||||
@ -59,7 +57,7 @@ static void parse_input(char **split, int *reg, uint8_t **ops, size_t *len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int combo(uint8_t val, int *reg)
|
static long int combo(uint8_t val, long int *reg)
|
||||||
{
|
{
|
||||||
if(val <= 3)
|
if(val <= 3)
|
||||||
return(val);
|
return(val);
|
||||||
@ -71,7 +69,7 @@ static int combo(uint8_t val, int *reg)
|
|||||||
return(*(reg + (val - 4)));
|
return(*(reg + (val - 4)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t exec_instructions(uint8_t *ops, int *reg, size_t len, uint8_t *out)
|
static size_t exec_instructions(uint8_t *ops, long int *reg, size_t len, uint8_t *out)
|
||||||
{
|
{
|
||||||
size_t instr_ptr;
|
size_t instr_ptr;
|
||||||
size_t out_ptr;
|
size_t out_ptr;
|
||||||
@ -84,7 +82,7 @@ static size_t exec_instructions(uint8_t *ops, int *reg, size_t len, uint8_t *out
|
|||||||
switch (ops[instr_ptr])
|
switch (ops[instr_ptr])
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
reg[0] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
|
reg[0] = reg[0] >> combo(ops[instr_ptr + 1], reg);
|
||||||
instr_ptr += 2;
|
instr_ptr += 2;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
@ -110,59 +108,78 @@ static size_t exec_instructions(uint8_t *ops, int *reg, size_t len, uint8_t *out
|
|||||||
instr_ptr += 2;
|
instr_ptr += 2;
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
reg[1] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
|
reg[1] = reg[0] >> combo(ops[instr_ptr + 1], reg);
|
||||||
instr_ptr += 2;
|
instr_ptr += 2;
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
reg[2] = reg[0] / pow(2, combo(ops[instr_ptr + 1], reg));
|
reg[2] = reg[0] >> combo(ops[instr_ptr + 1], reg);
|
||||||
instr_ptr += 2;
|
instr_ptr += 2;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(out_ptr == len)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return(out_ptr);
|
return(out_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_progress(struct timeval start, int cur)
|
int is_good(long int tmp, uint8_t *ops, size_t op_len, size_t cur_len)
|
||||||
{
|
{
|
||||||
struct timeval now;
|
long int reg[3];
|
||||||
double itps;
|
uint8_t *out;
|
||||||
double sec_diff;
|
size_t out_len;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
gettimeofday(&now, 0);
|
reg[0] = tmp;
|
||||||
sec_diff = (((double)now.tv_usec - (double)start.tv_usec) / 1000000) + (now.tv_sec - start.tv_sec);
|
reg[1] = 0;
|
||||||
itps = cur / sec_diff;
|
reg[2] = 0;
|
||||||
printf("currently: %d, itps : %f, time remaining until max int : %fs \r", cur, itps, (2147483647 - cur) / itps);
|
out = malloc(128);
|
||||||
fflush(stdout);
|
out_len = exec_instructions(ops, reg, op_len, out);
|
||||||
|
if(out_len != cur_len)
|
||||||
|
return(0);
|
||||||
|
i = 0;
|
||||||
|
while(i < out_len)
|
||||||
|
{
|
||||||
|
if(out[out_len - i - 1] != ops[op_len - i - 1])
|
||||||
|
return(0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int get_res(long int cur, uint8_t *ops, size_t len, size_t depth)
|
||||||
|
{
|
||||||
|
long int nb;
|
||||||
|
long int tmp;
|
||||||
|
long int res;
|
||||||
|
|
||||||
|
nb = 0;
|
||||||
|
tmp = 0;
|
||||||
|
while(nb < 8)
|
||||||
|
{
|
||||||
|
tmp = (cur << 3 ) | nb;
|
||||||
|
if(is_good(tmp, ops, len, depth + 1))
|
||||||
|
{
|
||||||
|
if(depth < len)
|
||||||
|
{
|
||||||
|
res = get_res(tmp, ops, len, depth + 1);
|
||||||
|
if(res)
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
nb++;
|
||||||
|
}
|
||||||
|
if(nb == 8)
|
||||||
|
return(0);
|
||||||
|
return(cur);
|
||||||
}
|
}
|
||||||
|
|
||||||
long int resolve_part2(char *input, char **split)
|
long int resolve_part2(char *input, char **split)
|
||||||
{
|
{
|
||||||
(void)input;
|
(void)input;
|
||||||
int reg[3];
|
long int reg[3];
|
||||||
uint8_t *ops;
|
uint8_t *ops;
|
||||||
size_t len;
|
size_t len;
|
||||||
uint8_t *out;
|
|
||||||
size_t out_len;
|
|
||||||
int i;
|
|
||||||
struct timeval start;
|
|
||||||
|
|
||||||
parse_input(split, reg, &ops, &len);
|
parse_input(split, reg, &ops, &len);
|
||||||
out = malloc(len);
|
return(get_res(0, ops, len, 0));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
Register A: 117440
|
Register A: 939520
|
||||||
Register B: 0
|
Register B: 0
|
||||||
Register C: 0
|
Register C: 0
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user