Skip to content

Commit

Permalink
Fix broken rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaEvo committed May 12, 2019
1 parent 16741bd commit e20486c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -54,6 +54,6 @@ fclean: clean

re: fclean $(NAME)

include $(wildcard $(DEP_DIR)/**/*.d)
include $(wildcard $(DEP_DIR)/*.d)

.PHONY: all clean fclean re
5 changes: 3 additions & 2 deletions include/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions include/lem_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};

Expand Down
6 changes: 3 additions & 3 deletions src/lem_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down
88 changes: 61 additions & 27 deletions src/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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--;
Expand All @@ -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, &current, 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, &current, anthil->end->links->rooms[i].ptr);
add_room(&current)->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);
}
}

0 comments on commit e20486c

Please sign in to comment.