-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
82 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
# By: dde-jesu <[email protected]> +#+ +:+ +#+ # | ||
# +#+#+#+#+#+ +#+ # | ||
# Created: 2018/11/30 10:00:57 by dde-jesu #+# #+# # | ||
# Updated: 2019/05/11 17:33:37 by dde-jesu ### ########.fr # | ||
# Updated: 2019/05/12 13:38:01 by dde-jesu ### ########.fr # | ||
# # | ||
# **************************************************************************** # | ||
|
||
|
@@ -54,6 +54,6 @@ fclean: clean | |
|
||
re: fclean $(NAME) | ||
|
||
include $(wildcard $(DEP_DIR)/**/*.d) | ||
include $(wildcard $(DEP_DIR)/*.d) | ||
|
||
.PHONY: all clean fclean re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,15 @@ | |
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2019/03/20 11:47:52 by dde-jesu #+# #+# */ | ||
/* Updated: 2019/04/26 14:28:32 by dde-jesu ### ########.fr */ | ||
/* Updated: 2019/05/12 13:39:29 by dde-jesu ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef HASHTABLE_H | ||
# define HASHTABLE_H | ||
|
||
# include "lem_in.h" | ||
# include <stdint.h> | ||
# include <stddef.h> | ||
|
||
struct s_entry { | ||
char *key; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2019/03/20 09:03:08 by dde-jesu #+# #+# */ | ||
/* Updated: 2019/05/11 11:44:20 by dde-jesu ### ########.fr */ | ||
/* Updated: 2019/05/12 14:28:01 by dde-jesu ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -37,10 +37,10 @@ struct s_room { | |
char *comments; | ||
int32_t x; | ||
int32_t y; | ||
bool end; | ||
struct s_room_vec *links; | ||
bool mark; | ||
bool broken; | ||
size_t depth; | ||
struct s_room *prev; | ||
struct s_room *old_prev; | ||
}; | ||
|
@@ -73,6 +73,7 @@ struct s_anthil { | |
char *end_comments; | ||
size_t ants; | ||
struct s_room *start; | ||
struct s_room *end; | ||
struct s_path_vec *paths; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2019/03/20 08:28:35 by dde-jesu #+# #+# */ | ||
/* Updated: 2019/05/11 17:44:35 by dde-jesu ### ########.fr */ | ||
/* Updated: 2019/05/12 13:37:21 by dde-jesu ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -90,18 +90,18 @@ int main(void) | |
error("No start link\n"); | ||
return (1); | ||
} | ||
if (anthil.start->end) | ||
if (anthil.start == anthil.end) | ||
{ | ||
error("Start is end\n"); | ||
return (1); | ||
} | ||
print_anthil(anthil); | ||
find_all_paths(&anthil); | ||
if (!anthil.paths) | ||
{ | ||
error("No paths found\n"); | ||
return (1); | ||
} | ||
print_anthil(anthil); | ||
size_t turns = print_moves(&anthil); | ||
fprintf(stderr, "Turns: %zu, Expected: %zd\n", turns, expected_turns(&anthil)); | ||
if (turns > expected_turns(&anthil)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2019/03/20 08:57:12 by dde-jesu #+# #+# */ | ||
/* Updated: 2019/05/11 11:47:28 by dde-jesu ### ########.fr */ | ||
/* Updated: 2019/05/12 09:40:46 by dde-jesu ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -219,6 +219,7 @@ struct s_anthil read_anthil(t_reader *r) | |
char *comments; | ||
|
||
anthil.start = NULL; | ||
anthil.end = NULL; | ||
anthil.paths = NULL; | ||
anthil.start_comments = read_comments(r); | ||
if (!io_readnum(r, &ants) || ants <= 0 || !io_expect(r, "\n")) | ||
|
@@ -246,10 +247,16 @@ struct s_anthil read_anthil(t_reader *r) | |
warning("Start redefined taking new value \"%s\" old was \"%s\"\n", room->name, anthil.start->name); | ||
anthil.start = room; | ||
} | ||
room->end = has_command(comments, "#end"); | ||
if (has_command(comments, "#end")) | ||
{ | ||
if (anthil.end) | ||
warning("End redefined taking new value \"%s\" old was \"%s\"\n", room->name, anthil.end->name); | ||
anthil.end = room; | ||
} | ||
room->links = create_room_vec(1); | ||
room->comments = comments; | ||
room->mark = false; | ||
room->depth = 0; | ||
room->broken = false; | ||
room->prev = NULL; | ||
room->old_prev = NULL; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2019/04/29 10:23:07 by dde-jesu #+# #+# */ | ||
/* Updated: 2019/05/11 17:09:32 by dde-jesu ### ########.fr */ | ||
/* Updated: 2019/05/12 12:42:30 by dde-jesu ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,18 @@ | |
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2019/04/28 16:37:29 by dde-jesu #+# #+# */ | ||
/* Updated: 2019/05/11 17:55:44 by dde-jesu ### ########.fr */ | ||
/* Updated: 2019/05/12 14:28:53 by dde-jesu ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "queue.h" | ||
#include "lem_in.h" | ||
#include <stdlib.h> | ||
#include <limits.h> | ||
|
||
void unmark(struct s_room *room); | ||
|
||
struct s_room *find_path(struct s_anthil *anthil) | ||
struct s_room *find_path(struct s_anthil *anthil, size_t max_depth) | ||
{ | ||
struct s_queue *queue; | ||
struct s_room *room; | ||
|
@@ -29,25 +30,37 @@ struct s_room *find_path(struct s_anthil *anthil) | |
while (!queue_empty(queue)) | ||
{ | ||
room = queue_pop(queue); | ||
if (room->end) | ||
if (room == anthil->end) | ||
break ; | ||
if (!room->old_prev) | ||
/*if (room->depth >= max_depth) | ||
continue ;*/ | ||
if (!room->old_prev || room->broken) | ||
{ | ||
i = 0; | ||
while (i < room->links->len) | ||
{ | ||
if (!room->links->rooms[i].ptr->mark) | ||
if (!room->links->rooms[i].ptr->mark | ||
&& room->links->rooms[i].ptr->old_prev != room) | ||
{ | ||
room->links->rooms[i].ptr->mark = true; | ||
room->links->rooms[i].ptr->prev = room; | ||
room->links->rooms[i].ptr->depth = room->depth + 1; | ||
*queue_push(&queue) = room->links->rooms[i].ptr; | ||
} | ||
i++; | ||
} | ||
} | ||
else if (!room->old_prev->mark) | ||
{ | ||
room->old_prev->prev = room; | ||
room->old_prev->mark = true; | ||
room->old_prev->broken = true; | ||
*queue_push(&queue) = room->old_prev; | ||
} | ||
} | ||
free(queue); | ||
if (room && room->end) | ||
fprintf(stderr, "Last depth %zu max %zu\n", room->depth, max_depth); | ||
if (room == anthil->end) | ||
return (room); | ||
return (NULL); | ||
} | ||
|
@@ -68,31 +81,41 @@ void print_path(struct s_room_vec *vec) | |
|
||
void collect_path(struct s_anthil *anthil, struct s_room_vec **vec, struct s_room *room) | ||
{ | ||
if (room->prev) | ||
collect_path(anthil, vec, room->prev); | ||
if (room->old_prev) | ||
collect_path(anthil, vec, room->old_prev); | ||
add_room(vec)->ptr = room; | ||
} | ||
|
||
void distribute_ants(struct s_anthil *anthil) | ||
size_t distribute_ants(struct s_anthil *anthil) | ||
{ | ||
size_t i; | ||
size_t diff; | ||
size_t ants; | ||
size_t min; | ||
size_t min_val; | ||
|
||
i = 0; | ||
min = 0; | ||
min_val = anthil->paths->paths[0].path->len; | ||
while (i < anthil->paths->len) | ||
{ | ||
if (anthil->paths->paths[i].path->len < min_val) | ||
{ | ||
min = i; | ||
min_val = anthil->paths->paths[i].path->len; | ||
} | ||
anthil->paths->paths[i].ants = 0; | ||
i++; | ||
} | ||
//fprintf(stderr, "Min %zu with %zu len\n", min, min_val); | ||
ants = anthil->ants; | ||
while (ants) | ||
{ | ||
i = 0; | ||
while (ants && i < anthil->paths->len) | ||
{ | ||
diff = anthil->paths->paths[i].path->len - anthil->paths->paths[0].path->len; | ||
if (anthil->paths->paths[0].ants >= diff) | ||
diff = anthil->paths->paths[i].path->len - anthil->paths->paths[min].path->len; | ||
if (diff == 0 || anthil->paths->paths[min].ants > diff) | ||
{ | ||
anthil->paths->paths[i].ants++; | ||
ants--; | ||
|
@@ -103,39 +126,50 @@ void distribute_ants(struct s_anthil *anthil) | |
i = 0; | ||
while (i < anthil->paths->len) | ||
{ | ||
fprintf(stderr, "Ants in %zu: %zu (turns: %zu)\n", i, anthil->paths->paths[i].ants, anthil->paths->paths[i].ants + anthil->paths->paths[i].path->len - 2); | ||
if (anthil->paths->paths[i].ants) | ||
fprintf(stderr, "Ants in %zu: %zu (turns: %zu)\n", i, anthil->paths->paths[i].ants, anthil->paths->paths[i].ants + anthil->paths->paths[i].path->len - 2); | ||
i++; | ||
} | ||
return (anthil->paths->paths[min].ants + anthil->paths->paths[min].path->len - 2); | ||
} | ||
|
||
void find_all_paths(struct s_anthil *anthil) | ||
{ | ||
struct s_room *room; | ||
struct s_room_vec *current; | ||
struct s_path *path; | ||
size_t i; | ||
size_t max_depth; | ||
|
||
fprintf(stderr, "Ants: %zu\n", anthil->ants); | ||
while ((room = find_path(anthil))) | ||
max_depth = SIZE_MAX; | ||
while ((room = find_path(anthil, max_depth))) | ||
{ | ||
if (!anthil->paths) | ||
anthil->paths = create_path_vec(10); | ||
current = create_room_vec(10); | ||
collect_path(anthil, ¤t, room); | ||
print_path(current); | ||
if ((path = add_path(&anthil->paths))) | ||
*path = (struct s_path) { | ||
.ants = 0, | ||
.path = current | ||
}; | ||
distribute_ants(anthil); | ||
if (!path->ants) | ||
break ; | ||
while (room != anthil->start) | ||
{ | ||
if (!room->broken) | ||
room->old_prev = 1; | ||
room->old_prev = room->prev; | ||
room = room->prev; | ||
} | ||
i = 0; | ||
anthil->paths = create_path_vec(10); | ||
while (i < anthil->end->links->len) | ||
{ | ||
if (anthil->end->links->rooms[i].ptr->old_prev) | ||
{ | ||
current = create_room_vec(10); | ||
collect_path(anthil, ¤t, anthil->end->links->rooms[i].ptr); | ||
add_room(¤t)->ptr = anthil->end; | ||
print_path(current); | ||
if ((path = add_path(&anthil->paths))) | ||
*path = (struct s_path) { | ||
.ants = 0, | ||
.path = current | ||
}; | ||
} | ||
i++; | ||
} | ||
max_depth = distribute_ants(anthil); | ||
unmark(anthil->start); | ||
} | ||
} |