Compare commits

...

10 Commits

5 changed files with 264 additions and 28 deletions

50
ex00/gen_util.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gen_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:41:44 by smatthes #+# #+# */
/* Updated: 2023/03/25 22:52:55 by smatthes ### ########.fr */
/* */
/* ************************************************************************** */
#include<unistd.h>
int str_len(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
void put_error()
{
write(1,"Error\n",6);
}
int *find_position(int **board, int n, int *pos)
{
int i;
int j;
i = -1;
j = -1;
while(++i < n)
{
while(++j < n)
if(board[i][j] == 0)
{
pos[0] = i;
pos[1] = j;
return pos;
}
}
return (0);
}

87
ex00/handle_input.c Normal file
View File

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 18:58:21 by smatthes #+# #+# */
/* Updated: 2023/03/25 23:29:41 by smatthes ### ########.fr */
/* */
/* ************************************************************************** */
int check_inp_valid(int argc, char **argv, int n);
int str_len(char *str);
void atoi_arr(char **argv, int *borders, int n);
int check_opposite_sum(int *borders, int n);
int handle_input(int argc, char **argv, int *borders, int n)
{
if (check_inp_valid(argc, argv, n) == 0)
return (0);
atoi_arr(argv, borders, n);
if (check_opposite_sum(borders, n) == 0)
return (0);
return (1);
}
int check_inp_valid(int argc, char **argv, int n)
{
int allowed_char_num;
int i;
char *inp;
if (argc != 2)
return (0);
inp = argv[1];
allowed_char_num = n * n * 2 - 1;
if (str_len(inp) != allowed_char_num)
return (0);
i = 0;
while (i < allowed_char_num)
{
if (i % 2 != 0 && inp[i] != ' ')
{
return (0);
}
if (i % 2 == 0 && (inp[i] < '1' || inp[i] > '4'))
return (0);
i++;
}
return (1);
}
void atoi_arr(char **argv, int *borders, int n)
{
int i;
i = 0;
while (i < n * n * 2 -1)
{
borders[i / 2] = argv[1][i] - '0';
i = i + 2;
}
}
int check_opposite_sum(int *borders, int n)
{
int i;
int val;
int opp_val;
i = 0;
while (i < 3 * n)
{
val = borders[1];
opp_val = borders[i + n];
if ((val + opp_val) > (n + 1))
{
return (0);
}
i++;
if (i == n)
i = 2 * n;
}
return (1);
}

View File

@ -1,21 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* backtrack.c :+: :+: :+: */
/* handle_mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 16:23:31 by tischmid #+# #+# */
/* Updated: 2023/03/25 16:28:59 by tischmid ### ########.fr */
/* Created: 2023/03/25 19:45:42 by smatthes #+# #+# */
/* Updated: 2023/03/25 22:41:35 by smatthes ### ########.fr */
/* */
/* ************************************************************************** */
void backtrack()
# include<stdlib.h>
# include <stdio.h>
int **allo_mem(int **board, int n)
{
int i;
board = malloc(n * 8);
i = -1;
while(++i < n)
{
board[i] = malloc(4);
}
return board;
}
int main(void)
{
return (0);
void free_mem(int *borders, int **board, int n)
{
int i;
i = -1;
while(++i < n)
{
free(board[i]);
}
free(board);
free(borders);
}

42
ex00/main.c Normal file
View File

@ -0,0 +1,42 @@
# include<unistd.h>
# include<stdio.h>
# include<stdlib.h>
int **allo_mem(int **board, int n);
void free_mem(int *borders, int **board, int n);
int handle_input(int argc, char **argv, int *borders, int n);
void put_error();
int main(int argc, char **argv)
{
int n;
int *borders;
int **board;
n = 4;
borders = malloc(n * 4 * 4);
board = allo_mem(board, n);
// handle sum of opposite inputs
if (handle_input(argc, argv, borders, n) == 0)
{
put_error();
return (1);
}
/*
if (backtrack(borders, board, n) == 0)
{
put_error();
return (1);
}
print_stuff
*/
free_mem(borders, board, n);
return (0);
}
// to do get position

View File

@ -2,8 +2,11 @@
import os
import sys
from time import sleep
PADDING = int(os.popen('tput cols').read()) // 3 # '// 2 - 6' for exact middle for n=4
# PADDING = int(os.popen('tput cols').read()) // 3 # '// 2 - 6' for exact middle for n=4
PADDING = 5
DEBUG = False
def get_board_dim(board: list) -> int:
return int(len(board) ** 0.5)
@ -50,22 +53,46 @@ def get_constraints(board: list, border: list, pos: int) -> list:
board_dim = get_board_dim(board)
col_idx = pos // board_dim
row_idx = pos % board_dim
constraints = []
print(f'{board_dim=}, {pos=}, {col_idx=}, {row_idx=}')
print(f'{border=}')
colup = border[row_idx]
coldown = border[row_idx]
rowleft = border[row_idx]
rowright = border[row_idx]
coldown = border[row_idx + board_dim]
rowleft = border[col_idx + board_dim * 2]
rowright = border[col_idx + board_dim * 3]
constraints = [colup, coldown, rowleft, rowright]
return constraints
def check_constraint(slice: list, constraint: int, n: int) -> bool:
constraint = int(constraint)
if 0 in slice:
return True
level = 0
visible_towers = 0
# no board_dim in 0 to i-2 (inclusive)
# no board_dim-1 in 0 to i-3
# no board_dim-2 in 0 to i-4
# no board_dim-3 in 0 to i-5
# ...
# no board_dim-i+2 in 0 to 0
board_dim = len(slice)
for i, tower in enumerate(slice):
if tower > level:
level = tower
visible_towers += 1
if
return visible_towers == constraint
def is_valid_state(board: list, border: list, next_candidate_index: int) -> bool:
print_board(board, border)
row = get_row(board, next_candidate_index)
column = get_col(board, next_candidate_index)
constraints = get_constraints(board, border, next_candidate_index)
print(f'{constraints=}, {row=}, {column=}')
return True
satisfies_constraints = all([
check_constraint(column, constraints[0]),
check_constraint(column[::-1], constraints[1]),
check_constraint(row, constraints[2]),
check_constraint(row[::-1], constraints[3]),
])
return satisfies_constraints
def print_board(board: list, border: list) -> None:
padding = ' ' * PADDING
@ -83,23 +110,25 @@ def print_board(board: list, border: list) -> None:
print(' '.join(border[board_dim:board_dim * 2]))
print()
def backtrack(board: list, sols: list, border: list) -> None:
def backtrack_skyscrapers(board: list, sols: list, border: list) -> None:
if board.count(0) == 0:
sols.append(board)
print("Solutions")
print_board(sols[0], border)
print("Done")
sys.exit(0)
next_candidate_index = board.index(0)
candidates = get_candidates(board, next_candidate_index)
for candidate in candidates:
board[next_candidate_index] = candidate
if DEBUG:
# sleep(2)
input()
os.system('clear')
print_board(board, border)
if is_valid_state(board, border, next_candidate_index):
backtrack(board, sols, border)
continue
backtrack_skyscrapers(board, sols, border)
board[next_candidate_index] = 0
def main(*args, **kwargs) -> None:
if kwargs:
print('Error (kwargs)')
sys.exit(1)
@ -122,8 +151,16 @@ def main(*args, **kwargs) -> None:
board = [
0 for _ in range(board_dim ** 2)
]
number_of_sols = backtrack(board, [], border)
solutions = []
backtrack_skyscrapers(board, solutions, border)
# print(solutions)
if __name__ == '__main__':
sys.exit(main('4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2'))
# sys.exit(main(('0 ' * 81)[:-1]))
# main('1 1 1 1')
# main('2 1 1 2 2 1 1 2')
# main('3 2 1 1 2 2 3 2 1 1 2 2')
# main('4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2') # original problem
# main('3 2 2 1 1 2 1 1 4 2 1 2 1 2 2 2') # henri's problem
# main('2 1 2 3 3 2 3 1 3 2 2 1 3 3 2 3 4 2 1 2')
# main('1 2 2 4 3 5 4 4 2 2 2 1 1 2 3 4 2 4 5 3 3 2 2 1')
# main('6 3 1 3 3 3 2 1 2 3 3 3 3 3 3 7 3 4 3 2 1 2 1 2 2 3 3 4')