finish 15 part 2
This commit is contained in:
parent
75eb2904b9
commit
7df1319416
68
15/part2.c
68
15/part2.c
|
@ -8,6 +8,36 @@ void display_map(char **map) {
|
||||||
|
|
||||||
int *pos_robot;
|
int *pos_robot;
|
||||||
|
|
||||||
|
int is_good(char **map, int pos[], int move) {
|
||||||
|
int add_x = move == '<' ? -1 : move == '>' ? 1 : 0;
|
||||||
|
int add_y = move == '^' ? -1 : move == 'v' ? 1 : 0;
|
||||||
|
int new_pos[2] = {pos[0] + add_y, pos[1] + add_x};
|
||||||
|
char tile = map[pos[0]][pos[1]];
|
||||||
|
char next_tile = map[new_pos[0]][new_pos[1]];
|
||||||
|
|
||||||
|
// check wall
|
||||||
|
if (next_tile == '#')
|
||||||
|
return 1;
|
||||||
|
if (tile == '[')
|
||||||
|
if (map[new_pos[0]][new_pos[1] + 1] == '#')
|
||||||
|
return 1;
|
||||||
|
if (tile == ']')
|
||||||
|
if (map[new_pos[0]][new_pos[1] - 1] == '#')
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
int left_right = next_tile == ']' ? -1 : next_tile == '[' ? 1 : 0;
|
||||||
|
if (left_right != 0) {
|
||||||
|
if (is_good(map, new_pos, move) == 1)
|
||||||
|
return 1;
|
||||||
|
if (add_y != 0) {
|
||||||
|
int second_new_pos[2] = {new_pos[0], new_pos[1] + left_right};
|
||||||
|
if (is_good(map, second_new_pos, move) == 1)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int move_object(char ***map_t, int pos[], int move) {
|
int move_object(char ***map_t, int pos[], int move) {
|
||||||
char **map = *map_t;
|
char **map = *map_t;
|
||||||
|
|
||||||
|
@ -27,6 +57,9 @@ int move_object(char ***map_t, int pos[], int move) {
|
||||||
if (map[new_pos[0]][new_pos[1] - 1] == '#')
|
if (map[new_pos[0]][new_pos[1] - 1] == '#')
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
if (is_good(map, pos, move) == 1)
|
||||||
|
return 1;
|
||||||
|
|
||||||
int left_right = next_tile == ']' ? -1 : next_tile == '[' ? 1 : 0;
|
int left_right = next_tile == ']' ? -1 : next_tile == '[' ? 1 : 0;
|
||||||
if (left_right != 0) {
|
if (left_right != 0) {
|
||||||
if (move_object(map_t, new_pos, move) == 1)
|
if (move_object(map_t, new_pos, move) == 1)
|
||||||
|
@ -36,29 +69,10 @@ int move_object(char ***map_t, int pos[], int move) {
|
||||||
if (move_object(map_t, second_new_pos, move) == 1)
|
if (move_object(map_t, second_new_pos, move) == 1)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
map[new_pos[0]][new_pos[1]] = map[pos[0]][pos[1]];
|
map[new_pos[0]][new_pos[1]] = map[pos[0]][pos[1]];
|
||||||
map[pos[0]][pos[1]] = '.';
|
map[pos[0]][pos[1]] = '.';
|
||||||
}
|
|
||||||
else {
|
|
||||||
}
|
|
||||||
|
|
||||||
/*if (add_y != 0 && second == 0) {
|
|
||||||
if (map[new_pos[0]][new_pos[1]] == '[') {
|
|
||||||
int *new_pos = calloc(2, sizeof(int));
|
|
||||||
new_pos[0] = pos[0];
|
|
||||||
new_pos[1] = pos[1] + 1;
|
|
||||||
if (move_object(map_t, &new_pos, move, 1) == 1)
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (map[new_pos[0]][new_pos[1]] == ']') {
|
|
||||||
int *new_pos = calloc(2, sizeof(int));
|
|
||||||
new_pos[0] = pos[0];
|
|
||||||
new_pos[1] = pos[1] - 1;
|
|
||||||
if (move_object(map_t, &new_pos, move, 1) == 1)
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if (tile == '@') {
|
if (tile == '@') {
|
||||||
pos_robot[0] = pos[0] + add_y;
|
pos_robot[0] = pos[0] + add_y;
|
||||||
|
@ -122,20 +136,12 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//display_map(map);
|
|
||||||
|
|
||||||
for (int i = 0; move_map[i] != NULL; i++) {
|
for (int i = 0; move_map[i] != NULL; i++)
|
||||||
for (int j = 0; move_map[i][j] != '\0'; j++) {
|
for (int j = 0; move_map[i][j] != '\0'; j++)
|
||||||
move_object(&map, pos_robot, move_map[i][j]);
|
move_object(&map, pos_robot, move_map[i][j]);
|
||||||
if (check(map) == 1) {
|
|
||||||
printf("i: %d, j: %d\n", i, j);
|
|
||||||
display_map(map);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
display_map(map);
|
//display_map(map);
|
||||||
long sum = sum_boxe(map);
|
long sum = sum_boxe(map);
|
||||||
|
|
||||||
printf("sum: %ld\n", sum);
|
printf("sum: %ld\n", sum);
|
||||||
|
|
Loading…
Reference in a new issue