Files
adventofcode2022/day22/monkeymap.c
dobiadi 0dc96bc943 Day22
2022-12-23 23:17:08 +01:00

242 lines
8.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUFFER_SIZE 16*1024
#define MAP_MAX_WIDTH 1024
#define MAP_MAX_HEIGHT 1024
#define MAX_PATH BUFFER_SIZE
enum tile {
VOID,
OPEN,
WALL
};
enum direction {
RIGHT,
DOWN,
LEFT,
UP
};
int moves[4][2] = {
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
};
int main() {
char buf[BUFFER_SIZE], *p, c;
memset(buf, 0, BUFFER_SIZE);
p = buf;
int map_width = 0, map_height = 0;
int map[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
for (int i = 0; i < MAP_MAX_WIDTH; i++) {
memset(map[i], VOID, MAP_MAX_HEIGHT * sizeof(int));
}
int isPath = 0;
char path[MAX_PATH];
int path_len;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
if (!isPath && map_width < (p - buf - 1)) {
map_width = p - buf - 1;
}
if (buf[0] == '\n') {
// Skip empty line
isPath = 1;
} else {
if (!isPath) {
p = buf;
while (*p != '\n') {
map[p-buf][map_height] = *p == ' ' ? VOID : (*p == '.' ? OPEN : WALL);
p++;
}
map_height++;
} else {
memcpy(path, buf, (p - buf - 1));
path_len = (p - buf - 1);
path[path_len] = 0;
}
}
memset(buf, 0, BUFFER_SIZE);
p = buf;
}
}
// printf("%i,%i\n", map_width, map_height);
int startPos[2], found = 0, currPos[2];
enum direction dir = RIGHT;
for (int i = 0; i < map_height; i++) {
for (int j = 0; j < map_width; j++) {
if (map[j][i] == 1) {
startPos[0] = j;
startPos[1] = i;
found = 1;
break;
}
}
if (found) break;
}
p = path;
currPos[0] = startPos[0];
currPos[1] = startPos[1];
while (*p != 0) {
// Rotate
if (*p >= 'A' && *p <= 'Z') {
switch (*p) {
case 'R':
dir = dir == UP ? RIGHT : dir+1;
break;
case 'L':
dir = dir == RIGHT ? UP : dir-1;
break;
}
p++;
} else {
// Move in direction
int count = 0;
sscanf(p, "%i", &count);
int bkp[2];
for (int i = 0; i < count; i++) {
bkp[0] = currPos[0];
bkp[1] = currPos[1];
currPos[0] += moves[dir][0];
currPos[1] += moves[dir][1];
if (currPos[0] < 0 || currPos[1] < 0 || map[currPos[0]][currPos[1]] == VOID) {
do {
currPos[0] -= moves[dir][0];
currPos[1] -= moves[dir][1];
} while (currPos[0] >= 0 && currPos[1] >= 0 && map[currPos[0]][currPos[1]] != VOID);
currPos[0] += moves[dir][0];
currPos[1] += moves[dir][1];
}
if (map[currPos[0]][currPos[1]] == WALL) {
currPos[0] = bkp[0];
currPos[1] = bkp[1];
break;
}
}
while (*p >= '0' && *p <= '9') { p++; }
}
}
printf("%i\n", (currPos[1]+1)*1000+(currPos[0]+1)*4+dir);
// Redo with cubes
// I just statically coded the cube sides
p = path;
currPos[0] = startPos[0];
currPos[1] = startPos[1];
dir = RIGHT;
while (*p != 0) {
// Rotate
if (*p >= 'A' && *p <= 'Z') {
switch (*p) {
case 'R':
dir = dir == UP ? RIGHT : dir+1;
break;
case 'L':
dir = dir == RIGHT ? UP : dir-1;
break;
}
p++;
} else {
// Move in direction
int count = 0;
sscanf(p, "%i", &count);
int bkp[2];
for (int i = 0; i < count; i++) {
bkp[0] = currPos[0];
bkp[1] = currPos[1];
enum direction bkpdir = dir;
currPos[0] += moves[dir][0];
currPos[1] += moves[dir][1];
if (currPos[0] < 0 || currPos[1] < 0 || map[currPos[0]][currPos[1]] == VOID) {
// Edge
// Blue
if (currPos[0] == 100 && currPos[1] < 100 && currPos[1] >= 50 && dir == RIGHT) {
currPos[0] = currPos[1] + 50;
currPos[1] = 49;
dir = UP;
} else if (currPos[1] == 50 && currPos[0] < 150 && currPos[0] >= 100 && dir == DOWN) {
currPos[1] = currPos[0] - 50;
currPos[0] = 99;
dir = LEFT;
// Orange
} else if (currPos[0] == 49 && currPos[1] < 100 && currPos[1] >= 50 && dir == LEFT) {
currPos[0] = currPos[1] - 50;
currPos[1] = 100;
dir = DOWN;
} else if (currPos[1] == 99 && currPos[0] < 50 && currPos[0] >= 0 && dir == UP) {
currPos[1] = currPos[0] + 50;
currPos[0] = 50;
dir = RIGHT;
// Green
} else if (currPos[0] == 50 && currPos[1] < 200 && currPos[1] >= 150 && dir == RIGHT) {
currPos[0] = currPos[1] - 100;
currPos[1] = 149;
dir = UP;
} else if (currPos[1] == 150 && currPos[0] < 100 && currPos[0] >= 50 && dir == DOWN) {
currPos[1] = currPos[0] + 100;
currPos[0] = 49;
dir = LEFT;
// Yellow
} else if (currPos[0] == 49 && currPos[1] < 50 && currPos[1] >= 0) {
currPos[1] = 149 - currPos[1];
currPos[0] = 0;
dir = RIGHT;
} else if (currPos[0] == -1 && currPos[1] < 150 && currPos[1] >= 100) {
currPos[1] = 149 - currPos[1];
currPos[0] = 50;
dir = RIGHT;
// Purple
} else if (currPos[0] == 100 && currPos[1] < 150 && currPos[1] >= 100) {
currPos[1] = 149 - currPos[1];
currPos[0] = 149;
dir = LEFT;
} else if (currPos[0] == 150 && currPos[1] < 50 && currPos[1] >= 0) {
currPos[1] = 149 - currPos[1];
currPos[0] = 99;
dir = LEFT;
// Brown
} else if (currPos[0] == -1 && currPos[1] < 200 && currPos[1] >= 150) {
currPos[0] = currPos[1] - 100;
currPos[1] = 0;
dir = DOWN;
} else if (currPos[1] == -1 && currPos[0] < 100 && currPos[0] >= 50) {
currPos[1] = currPos[0] + 100;
currPos[0] = 0;
dir = RIGHT;
// Cyan
} else if (currPos[1] == 200 && currPos[0] < 50 && currPos[0] >= 0) {
currPos[0] = currPos[0] + 100;
currPos[1] = 0;
dir = DOWN;
} else if (currPos[1] == -1 && currPos[0] < 150 && currPos[0] >= 100) {
currPos[0] = currPos[0] - 100;
currPos[1] = 199;
dir = UP;
}
}
if (map[currPos[0]][currPos[1]] == WALL) {
currPos[0] = bkp[0];
currPos[1] = bkp[1];
dir = bkpdir;
break;
}
}
while (*p >= '0' && *p <= '9') { p++; }
}
}
printf("%i\n", (currPos[1]+1)*1000+(currPos[0]+1)*4+dir);
}